Qt信号与槽及QSS界面美化

火影  金牌会员 | 2024-12-29 22:24:56 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 804|帖子 804|积分 2412




login2_0.pro
  1. QT       += core gui
  2. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  3. CONFIG += c++11
  4. # The following define makes your compiler emit warnings if you use
  5. # any Qt feature that has been marked deprecated (the exact warnings
  6. # depend on your compiler). Please consult the documentation of the
  7. # deprecated API in order to know how to port your code away from it.
  8. DEFINES += QT_DEPRECATED_WARNINGS
  9. # You can also make your code fail to compile if it uses deprecated APIs.
  10. # In order to do so, uncomment the following line.
  11. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  12. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
  13. SOURCES += \
  14.     main.cpp \
  15.     user.cpp \
  16.     widget.cpp
  17. HEADERS += \
  18.     user.h \
  19.     widget.h
  20. FORMS += \
  21.     user.ui \
  22.     widget.ui
  23. # Default rules for deployment.
  24. qnx: target.path = /tmp/$${TARGET}/bin
  25. else: unix:!android: target.path = /opt/$${TARGET}/bin
  26. !isEmpty(target.path): INSTALLS += target
  27. RESOURCES += \
  28.     sor.qrc
复制代码
user.h
  1. #ifndef USER_H
  2. #define USER_H
  3. #include <QWidget>
  4. namespace Ui {
  5. class User;
  6. }
  7. class User : public QWidget
  8. {
  9.     Q_OBJECT
  10. public:
  11.     explicit User(QWidget *parent = nullptr);
  12.     ~User();
  13. public slots:
  14.     void login();
  15. private:
  16.     Ui::User *ui;
  17. };
  18. #endif // USER_H
复制代码
widget.h
  1. #ifndef WIDGET_H
  2. #define WIDGET_H
  3. #include <QWidget>
  4. #include <QTimer>
  5. QT_BEGIN_NAMESPACE
  6. namespace Ui { class Widget; }
  7. QT_END_NAMESPACE
  8. class Widget : public QWidget
  9. {
  10.     Q_OBJECT
  11. public:
  12.     Widget(QWidget *parent = nullptr);
  13.     ~Widget();
  14. signals:
  15.     void log_sig();
  16. public slots:
  17.     void btn_closed();
  18.     void btn_hide();
  19. private slots:
  20.     void on_pushButton_clicked();
  21. private:
  22.     Ui::Widget *ui;
  23. };
  24. #endif // WIDGET_H
复制代码
main.cpp
  1. #include "widget.h"
  2. #include "user.h"
  3. #include <QApplication>
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     Widget w;
  8.     w.show();
  9.     User u;
  10.     QObject::connect(&w,&Widget::log_sig,&u,&User::login);
  11.     return a.exec();
  12. }
复制代码
user.cpp
  1. #include "user.h"
  2. #include "ui_user.h"
  3. User::User(QWidget *parent) :
  4.     QWidget(parent),
  5.     ui(new Ui::User)
  6. {
  7.     ui->setupUi(this);
  8. }
  9. User::~User()
  10. {
  11.     delete ui;
  12. }
  13. void User::login()
  14. {
  15.     this->show();
  16. }
复制代码
widget.cpp
  1. #include "widget.h"
  2. #include "ui_widget.h"
  3. Widget::Widget(QWidget *parent)
  4.     : QWidget(parent)
  5.     , ui(new Ui::Widget)
  6. {
  7.     ui->setupUi(this);
  8.     this->setWindowFlag(Qt::FramelessWindowHint);
  9.     this->setAttribute(Qt::WA_TranslucentBackground);
  10.     QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(btn_closed()));
  11.     QObject::connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(btn_hide()));
  12. }
  13. Widget::~Widget()
  14. {
  15.     delete ui;
  16. }
  17. void Widget::btn_closed()
  18. {
  19.     this->close();
  20. }
  21. void Widget::btn_hide()
  22. {
  23.     this->showMinimized();
  24. }
  25. void Widget::on_pushButton_clicked()
  26. {
  27.     if(ui->lineEdit->text() == "admin" && ui->lineEdit_2->text() == "12345")
  28.     {
  29.         ui->label_3->setStyleSheet(QString("background-color:blue;color:white"));
  30.         ui->label_3->setText("登陆成功");
  31.         emit log_sig();
  32.         // 使用 QTimer 延时关闭
  33.         QTimer::singleShot(600, this, [this]()
  34.         {
  35.             ui->label_3->setStyleSheet(QString("background:transparent;"));
  36.             this->close();
  37.         });
  38.     }
  39.     else
  40.     {
  41.         ui->label_3->setText("登陆失败");
  42.          ui->label_3->setStyleSheet(QString("background-color:red;color:black"));
  43.         // 使用 QTimer 延时清除文本
  44.         QTimer::singleShot(600, this, [this]()
  45.         {
  46.             ui->label_3->setStyleSheet(QString("background:transparent;"));
  47.             ui->label_3->setText("");
  48.             ui->lineEdit_2->setText("");
  49.         });
  50.     }
  51. }
复制代码
user.ui
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>User</class>
  4. <widget class="QWidget" name="User">
  5.   <property name="geometry">
  6.    <rect>
  7.     <x>0</x>
  8.     <y>0</y>
  9.     <width>400</width>
  10.     <height>300</height>
  11.    </rect>
  12.   </property>
  13.   <property name="windowTitle">
  14.    <string>Form</string>
  15.   </property>
  16.   <widget class="QLabel" name="label">
  17.    <property name="geometry">
  18.     <rect>
  19.      <x>80</x>
  20.      <y>60</y>
  21.      <width>211</width>
  22.      <height>151</height>
  23.     </rect>
  24.    </property>
  25.    <property name="text">
  26.     <string>用户界面</string>
  27.    </property>
  28.   </widget>
  29. </widget>
  30. <resources/>
  31. <connections/>
  32. </ui>
复制代码
widget.ui
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <ui version="4.0">
  3. <class>Widget</class>
  4. <widget class="QWidget" name="Widget">
  5.   <property name="geometry">
  6.    <rect>
  7.     <x>0</x>
  8.     <y>0</y>
  9.     <width>510</width>
  10.     <height>630</height>
  11.    </rect>
  12.   </property>
  13.   <property name="windowTitle">
  14.    <string>Widget</string>
  15.   </property>
  16.   <property name="styleSheet">
  17.    <string notr="true">*{
  18.         background-color: rgb(85, 85, 127);
  19. }
  20. QFrame#frame{
  21.         border-image: url(:/Logo/shanChuan.jpg);
  22.         border-radius:30px;
  23. }
  24. QFrame#frame_2{
  25.         background-color: rgba(116, 116, 116, 100);
  26.         border-radius:30px;
  27. }
  28. QLabel#label{
  29.         background-color: rgba(0, 0, 0, 40);
  30.         border-radius:30px;
  31. }
  32. #label_2{
  33.         font: 75 18pt &quot;微软雅黑&quot;;
  34.         background:transparent;/*完全透明*/
  35.         color: rgba(193, 193, 193, 150);
  36. }
  37. #label_3{
  38.         qproperty-alignment: AlignCenter;
  39.         font: 75 20pt &quot;微软雅黑&quot;;
  40.         background:transparent;/*完全透明*/
  41.         color: rgba(255, 0, 0, 150);
  42. }
  43. QLineEdit{
  44.         background-color: rgba(195, 195, 195, 0);
  45.         border:none;/*无边框*/
  46.         border-bottom:1px solid rgba(255, 255, 255, 150);
  47.         color: rgba(193, 193, 193, 150);
  48. }
  49. QPushButton#pushButton{
  50.         background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0.6875 rgba(0, 47, 225, 255), stop:1 rgba(255, 255, 255, 255));
  51.         font: 75 16pt &quot;微软雅黑&quot;;
  52.         color: rgba(193, 193, 193, 150);
  53.         border-radius:5px;
  54. }
  55. QPushButton#pushButton:hover{
  56.         background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0.6875 rgba(50, 80, 225, 255), stop:1 rgba(255, 255, 255, 255));
  57.         font: 75 16pt &quot;微软雅黑&quot;;
  58.         color: rgba(193, 193, 193, 150);
  59.         border-radius:5px;
  60. }
  61. QPushButton#pushButton:pressed{
  62.         background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:1, stop:0.6875 rgba(80, 47, 225, 255), stop:1 rgba(255, 255, 255, 255));
  63.         font: 75 16pt &quot;微软雅黑&quot;;
  64.         color: rgba(193, 193, 193, 150);
  65.         border-radius:5px;
  66.         padding-top:5px;
  67.         padding-left:5px;
  68. }
  69. QPushButton#pushButton_2{
  70.        
  71.         background-color: rgba(214, 214, 214, 0);
  72.         font: 75 16pt &quot;微软雅黑&quot;;
  73.         color: rgba(193, 193, 193, 150);
  74.         border-radius:5px;
  75. }
  76. QPushButton#pushButton_2:hover{
  77.         background-color: red;
  78.         font: 75 16pt &quot;微软雅黑&quot;;
  79.         color: rgba(193, 193, 193, 150);
  80.         border-radius:5px;
  81. }
  82. QPushButton#pushButton_2:pressed{
  83.         background-color: rgba(214, 214, 214, 0);
  84.         font: 75 16pt &quot;微软雅黑&quot;;
  85.         color: rgba(193, 193, 193, 150);
  86.         border-radius:5px;
  87.         padding-top:5px;
  88.         padding-left:5px;
  89. }
  90. QPushButton#pushButton_3{
  91.        
  92.         background-color: rgba(214, 214, 214, 0);
  93.         font: 75 25pt &quot;微软雅黑&quot;;
  94.         color: rgba(193, 193, 193, 150);
  95.         border-radius:5px;
  96. }
  97. QPushButton#pushButton_3:hover{
  98.         background-color: red;
  99.         font: 75 25pt &quot;微软雅黑&quot;;
  100.         color: rgba(193, 193, 193, 150);
  101.         border-radius:5px;
  102. }
  103. QPushButton#pushButton_3:pressed{
  104.         background-color: rgba(214, 214, 214, 0);
  105.         font: 75 25pt &quot;微软雅黑&quot;;
  106.         color: rgba(193, 193, 193, 150);
  107.         border-radius:5px;
  108.         padding-top:5px;
  109.         padding-left:5px;
  110. }
  111. </string>
  112.   </property>
  113.   <widget class="QFrame" name="frame">
  114.    <property name="geometry">
  115.     <rect>
  116.      <x>40</x>
  117.      <y>90</y>
  118.      <width>431</width>
  119.      <height>501</height>
  120.     </rect>
  121.    </property>
  122.    <property name="styleSheet">
  123.     <string notr="true"/>
  124.    </property>
  125.    <property name="frameShape">
  126.     <enum>QFrame::StyledPanel</enum>
  127.    </property>
  128.    <property name="frameShadow">
  129.     <enum>QFrame::Raised</enum>
  130.    </property>
  131.    <widget class="QFrame" name="frame_2">
  132.     <property name="geometry">
  133.      <rect>
  134.       <x>0</x>
  135.       <y>0</y>
  136.       <width>431</width>
  137.       <height>501</height>
  138.      </rect>
  139.     </property>
  140.     <property name="frameShape">
  141.      <enum>QFrame::StyledPanel</enum>
  142.     </property>
  143.     <property name="frameShadow">
  144.      <enum>QFrame::Raised</enum>
  145.     </property>
  146.     <widget class="QLabel" name="label">
  147.      <property name="geometry">
  148.       <rect>
  149.        <x>40</x>
  150.        <y>60</y>
  151.        <width>350</width>
  152.        <height>420</height>
  153.       </rect>
  154.      </property>
  155.      <property name="text">
  156.       <string/>
  157.      </property>
  158.     </widget>
  159.     <widget class="QLabel" name="label_2">
  160.      <property name="geometry">
  161.       <rect>
  162.        <x>150</x>
  163.        <y>100</y>
  164.        <width>121</width>
  165.        <height>31</height>
  166.       </rect>
  167.      </property>
  168.      <property name="font">
  169.       <font>
  170.        <family>微软雅黑</family>
  171.        <pointsize>18</pointsize>
  172.        <weight>9</weight>
  173.        <italic>false</italic>
  174.        <bold>false</bold>
  175.       </font>
  176.      </property>
  177.      <property name="text">
  178.       <string>登陆界面</string>
  179.      </property>
  180.     </widget>
  181.     <widget class="QLineEdit" name="lineEdit">
  182.      <property name="geometry">
  183.       <rect>
  184.        <x>80</x>
  185.        <y>200</y>
  186.        <width>261</width>
  187.        <height>51</height>
  188.       </rect>
  189.      </property>
  190.      <property name="font">
  191.       <font>
  192.        <pointsize>13</pointsize>
  193.       </font>
  194.      </property>
  195.      <property name="placeholderText">
  196.       <string>请输入账户:</string>
  197.      </property>
  198.     </widget>
  199.     <widget class="QLineEdit" name="lineEdit_2">
  200.      <property name="geometry">
  201.       <rect>
  202.        <x>80</x>
  203.        <y>290</y>
  204.        <width>261</width>
  205.        <height>51</height>
  206.       </rect>
  207.      </property>
  208.      <property name="font">
  209.       <font>
  210.        <pointsize>13</pointsize>
  211.       </font>
  212.      </property>
  213.      <property name="echoMode">
  214.       <enum>QLineEdit::Password</enum>
  215.      </property>
  216.      <property name="placeholderText">
  217.       <string>请输入密码:</string>
  218.      </property>
  219.     </widget>
  220.     <widget class="QPushButton" name="pushButton">
  221.      <property name="geometry">
  222.       <rect>
  223.        <x>80</x>
  224.        <y>390</y>
  225.        <width>261</width>
  226.        <height>51</height>
  227.       </rect>
  228.      </property>
  229.      <property name="text">
  230.       <string>登陆</string>
  231.      </property>
  232.     </widget>
  233.     <widget class="QLabel" name="label_3">
  234.      <property name="geometry">
  235.       <rect>
  236.        <x>40</x>
  237.        <y>10</y>
  238.        <width>351</width>
  239.        <height>51</height>
  240.       </rect>
  241.      </property>
  242.      <property name="font">
  243.       <font>
  244.        <family>微软雅黑</family>
  245.        <pointsize>20</pointsize>
  246.        <weight>9</weight>
  247.        <italic>false</italic>
  248.        <bold>false</bold>
  249.       </font>
  250.      </property>
  251.      <property name="text">
  252.       <string/>
  253.      </property>
  254.     </widget>
  255.     <widget class="QPushButton" name="pushButton_2">
  256.      <property name="geometry">
  257.       <rect>
  258.        <x>370</x>
  259.        <y>0</y>
  260.        <width>51</width>
  261.        <height>31</height>
  262.       </rect>
  263.      </property>
  264.      <property name="text">
  265.       <string>x</string>
  266.      </property>
  267.     </widget>
  268.     <widget class="QPushButton" name="pushButton_3">
  269.      <property name="geometry">
  270.       <rect>
  271.        <x>320</x>
  272.        <y>0</y>
  273.        <width>51</width>
  274.        <height>31</height>
  275.       </rect>
  276.      </property>
  277.      <property name="font">
  278.       <font>
  279.        <family>微软雅黑</family>
  280.        <pointsize>25</pointsize>
  281.        <weight>9</weight>
  282.        <italic>false</italic>
  283.        <bold>false</bold>
  284.       </font>
  285.      </property>
  286.      <property name="text">
  287.       <string>-</string>
  288.      </property>
  289.     </widget>
  290.    </widget>
  291.   </widget>
  292. </widget>
  293. <resources/>
  294. <connections/>
  295. </ui>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

火影

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表