Qt信号与槽及QSS界面美化

发布于:2025-02-11 ⋅ 阅读:(61) ⋅ 点赞:(0)

login2_0.pro

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    user.cpp \
    widget.cpp

HEADERS += \
    user.h \
    widget.h

FORMS += \
    user.ui \
    widget.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    sor.qrc

user.h

#ifndef USER_H
#define USER_H

#include <QWidget>

namespace Ui {
class User;
}

class User : public QWidget
{
    Q_OBJECT

public:
    explicit User(QWidget *parent = nullptr);
    ~User();

public slots:
    void login();
private:
    Ui::User *ui;
};

#endif // USER_H

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QTimer>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
signals:
    void log_sig();
public slots:
    void btn_closed();
    void btn_hide();
private slots:
    void on_pushButton_clicked();

private:
    Ui::Widget *ui;
};
#endif // WIDGET_H

main.cpp

#include "widget.h"
#include "user.h"
#include <QApplication>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();
    User u;
    QObject::connect(&w,&Widget::log_sig,&u,&User::login);
    return a.exec();
}

user.cpp

#include "user.h"
#include "ui_user.h"

User::User(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::User)
{
    ui->setupUi(this);
}

User::~User()
{
    delete ui;
}

void User::login()
{
    this->show();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowFlag(Qt::FramelessWindowHint);
    this->setAttribute(Qt::WA_TranslucentBackground);
    QObject::connect(ui->pushButton_2,SIGNAL(clicked()),this,SLOT(btn_closed()));
    QObject::connect(ui->pushButton_3,SIGNAL(clicked()),this,SLOT(btn_hide()));
}

Widget::~Widget()
{
    delete ui;
}

void Widget::btn_closed()
{
    this->close();
}
void Widget::btn_hide()
{
    this->showMinimized();
}
void Widget::on_pushButton_clicked()
{
    if(ui->lineEdit->text() == "admin" && ui->lineEdit_2->text() == "12345")
    {
        ui->label_3->setStyleSheet(QString("background-color:blue;color:white"));
        ui->label_3->setText("登陆成功");
        emit log_sig();
        // 使用 QTimer 延时关闭
        QTimer::singleShot(600, this, [this]()
        {
            ui->label_3->setStyleSheet(QString("background:transparent;"));

            this->close();
        });
    }
    else
    {
        ui->label_3->setText("登陆失败");
         ui->label_3->setStyleSheet(QString("background-color:red;color:black"));
        // 使用 QTimer 延时清除文本
        QTimer::singleShot(600, this, [this]()
        {
            ui->label_3->setStyleSheet(QString("background:transparent;"));
            ui->label_3->setText("");
            ui->lineEdit_2->setText("");
        });
    }
}

user.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>User</class>
 <widget class="QWidget" name="User">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>400</width>
    <height>300</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Form</string>
  </property>
  <widget class="QLabel" name="label">
   <property name="geometry">
    <rect>
     <x>80</x>
     <y>60</y>
     <width>211</width>
     <height>151</height>
    </rect>
   </property>
   <property name="text">
    <string>用户界面</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>

widget.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Widget</class>
 <widget class="QWidget" name="Widget">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>510</width>
    <height>630</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Widget</string>
  </property>
  <property name="styleSheet">
   <string notr="true">*{
	background-color: rgb(85, 85, 127);
}
QFrame#frame{
	border-image: url(:/Logo/shanChuan.jpg);
	border-radius:30px;
}

QFrame#frame_2{
	background-color: rgba(116, 116, 116, 100);
	border-radius:30px;
}

QLabel#label{
	background-color: rgba(0, 0, 0, 40);
	border-radius:30px;
}
#label_2{
	font: 75 18pt &quot;微软雅黑&quot;;
	background:transparent;/*完全透明*/
	color: rgba(193, 193, 193, 150);
}
#label_3{
	qproperty-alignment: AlignCenter;
	font: 75 20pt &quot;微软雅黑&quot;;
	background:transparent;/*完全透明*/
	color: rgba(255, 0, 0, 150);
}
QLineEdit{
	background-color: rgba(195, 195, 195, 0);
	border:none;/*无边框*/
	border-bottom:1px solid rgba(255, 255, 255, 150);
	color: rgba(193, 193, 193, 150);
}
QPushButton#pushButton{
	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));
	font: 75 16pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
}
QPushButton#pushButton:hover{
	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));
	font: 75 16pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
}
QPushButton#pushButton:pressed{
	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));
	font: 75 16pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
	padding-top:5px;
	padding-left:5px;
}
QPushButton#pushButton_2{
	
	background-color: rgba(214, 214, 214, 0);
	font: 75 16pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
}
QPushButton#pushButton_2:hover{
	background-color: red;
	font: 75 16pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
}
QPushButton#pushButton_2:pressed{
	background-color: rgba(214, 214, 214, 0);
	font: 75 16pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
	padding-top:5px;
	padding-left:5px;
}
QPushButton#pushButton_3{
	
	background-color: rgba(214, 214, 214, 0);
	font: 75 25pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
}
QPushButton#pushButton_3:hover{
	background-color: red;
	font: 75 25pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
}
QPushButton#pushButton_3:pressed{
	background-color: rgba(214, 214, 214, 0);
	font: 75 25pt &quot;微软雅黑&quot;;
	color: rgba(193, 193, 193, 150);
	border-radius:5px;
	padding-top:5px;
	padding-left:5px;
}


</string>
  </property>
  <widget class="QFrame" name="frame">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>90</y>
     <width>431</width>
     <height>501</height>
    </rect>
   </property>
   <property name="styleSheet">
    <string notr="true"/>
   </property>
   <property name="frameShape">
    <enum>QFrame::StyledPanel</enum>
   </property>
   <property name="frameShadow">
    <enum>QFrame::Raised</enum>
   </property>
   <widget class="QFrame" name="frame_2">
    <property name="geometry">
     <rect>
      <x>0</x>
      <y>0</y>
      <width>431</width>
      <height>501</height>
     </rect>
    </property>
    <property name="frameShape">
     <enum>QFrame::StyledPanel</enum>
    </property>
    <property name="frameShadow">
     <enum>QFrame::Raised</enum>
    </property>
    <widget class="QLabel" name="label">
     <property name="geometry">
      <rect>
       <x>40</x>
       <y>60</y>
       <width>350</width>
       <height>420</height>
      </rect>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
    <widget class="QLabel" name="label_2">
     <property name="geometry">
      <rect>
       <x>150</x>
       <y>100</y>
       <width>121</width>
       <height>31</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <family>微软雅黑</family>
       <pointsize>18</pointsize>
       <weight>9</weight>
       <italic>false</italic>
       <bold>false</bold>
      </font>
     </property>
     <property name="text">
      <string>登陆界面</string>
     </property>
    </widget>
    <widget class="QLineEdit" name="lineEdit">
     <property name="geometry">
      <rect>
       <x>80</x>
       <y>200</y>
       <width>261</width>
       <height>51</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>13</pointsize>
      </font>
     </property>
     <property name="placeholderText">
      <string>请输入账户:</string>
     </property>
    </widget>
    <widget class="QLineEdit" name="lineEdit_2">
     <property name="geometry">
      <rect>
       <x>80</x>
       <y>290</y>
       <width>261</width>
       <height>51</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <pointsize>13</pointsize>
      </font>
     </property>
     <property name="echoMode">
      <enum>QLineEdit::Password</enum>
     </property>
     <property name="placeholderText">
      <string>请输入密码:</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton">
     <property name="geometry">
      <rect>
       <x>80</x>
       <y>390</y>
       <width>261</width>
       <height>51</height>
      </rect>
     </property>
     <property name="text">
      <string>登陆</string>
     </property>
    </widget>
    <widget class="QLabel" name="label_3">
     <property name="geometry">
      <rect>
       <x>40</x>
       <y>10</y>
       <width>351</width>
       <height>51</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <family>微软雅黑</family>
       <pointsize>20</pointsize>
       <weight>9</weight>
       <italic>false</italic>
       <bold>false</bold>
      </font>
     </property>
     <property name="text">
      <string/>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_2">
     <property name="geometry">
      <rect>
       <x>370</x>
       <y>0</y>
       <width>51</width>
       <height>31</height>
      </rect>
     </property>
     <property name="text">
      <string>x</string>
     </property>
    </widget>
    <widget class="QPushButton" name="pushButton_3">
     <property name="geometry">
      <rect>
       <x>320</x>
       <y>0</y>
       <width>51</width>
       <height>31</height>
      </rect>
     </property>
     <property name="font">
      <font>
       <family>微软雅黑</family>
       <pointsize>25</pointsize>
       <weight>9</weight>
       <italic>false</italic>
       <bold>false</bold>
      </font>
     </property>
     <property name="text">
      <string>-</string>
     </property>
    </widget>
   </widget>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>