对登录页面进行进一步优化,发挥信号和槽的作用
1. .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>800</width>
<height>600</height>
</rect>
</property>
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="windowTitle">
<string>QQ</string>
</property>
<property name="windowIcon">
<iconset>
<normaloff>R-C.jpg</normaloff>R-C.jpg</iconset>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>250</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(19, 255, 78);</string>
</property>
<property name="text">
<string/>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
<property name="openExternalLinks">
<bool>false</bool>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>220</x>
<y>320</y>
<width>40</width>
<height>40</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap>R-C.jpg</pixmap>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>220</x>
<y>380</y>
<width>40</width>
<height>40</height>
</rect>
</property>
<property name="text">
<string>密码</string>
</property>
<property name="scaledContents">
<bool>false</bool>
</property>
<property name="wordWrap">
<bool>false</bool>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit">
<property name="geometry">
<rect>
<x>290</x>
<y>320</y>
<width>250</width>
<height>40</height>
</rect>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>290</x>
<y>380</y>
<width>250</width>
<height>40</height>
</rect>
</property>
<property name="autoFillBackground">
<bool>false</bool>
</property>
<property name="inputMask">
<string/>
</property>
<property name="text">
<string notr="true"/>
</property>
<property name="frame">
<bool>true</bool>
</property>
<property name="echoMode">
<enum>QLineEdit::Normal</enum>
</property>
<property name="dragEnabled">
<bool>false</bool>
</property>
<property name="readOnly">
<bool>false</bool>
</property>
<property name="placeholderText">
<string/>
</property>
<property name="clearButtonEnabled">
<bool>false</bool>
</property>
</widget>
<widget class="QLabel" name="label_4">
<property name="geometry">
<rect>
<x>330</x>
<y>550</y>
<width>120</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string/>
</property>
</widget>
<widget class="QLabel" name="label_5">
<property name="enabled">
<bool>false</bool>
</property>
<property name="geometry">
<rect>
<x>0</x>
<y>250</y>
<width>800</width>
<height>350</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="scaledContents">
<bool>true</bool>
</property>
</widget>
<zorder>label_5</zorder>
<zorder>label</zorder>
<zorder>label_2</zorder>
<zorder>label_3</zorder>
<zorder>lineEdit</zorder>
<zorder>lineEdit_2</zorder>
<zorder>label_4</zorder>
</widget>
<resources/>
<connections/>
</ui>
2.头函数
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
#include <QMovie>
#include <cstring>
#include <QString>
#include <QDebug>
#include <ctime>
#include <thread>
#include <chrono>
#include <QThread>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
void abc();
class Widget : public QWidget
{
Q_OBJECT
friend void abc();
public:
Widget(QWidget *parent = nullptr);
~Widget();
signals:
void on_sig();
void on_si();
public slots:
void my_slot();
void my_slot2();
void my_slot3();
private:
Ui::Widget *ui;
QPushButton *p1;
};
class WorkerThread : public QThread
{
Q_OBJECT
public:
void run() override {
std::this_thread::sleep_for(std::chrono::seconds(10));
emit resultReady();
}
signals:
void resultReady();
};
#endif // WIDGET_H
3.功能函数
#include "widget.h"
#include "ui_widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
,p1(new QPushButton("登录",this))
{
connect(p1,SIGNAL(clicked()),this,SLOT(my_slot()));
ui->setupUi(this);
this->setWindowIcon(QIcon(":/R-C.jpg"));
QMovie *mv2 = new QMovie(":/OIP2.gif");
ui->label_5->setMovie(mv2);
mv2->start();
ui->label_5->setScaledContents(true);
QMovie *mv1 = new QMovie(":/R-C.gif");
ui->label->setMovie(mv1);
mv1->start();
ui->label->setScaledContents(true);
ui->label_2->setPixmap(QPixmap(":/R-C.jpg"));
ui->label_2->setScaledContents(true);
ui->label_3->setPixmap(QPixmap(":/OIP-C.jpg"));
ui->label_3->setScaledContents(true);
ui->lineEdit->setPlaceholderText("请输入QQ号\\手机号");
ui->lineEdit_2->setPlaceholderText("密码");
ui->lineEdit_2->setEchoMode(QLineEdit::Password);
p1->raise();
p1->resize(300,40);
p1->move(250,430);
p1->setStyleSheet("background-color:rgb(31,200,253);color:white;\
border-radius:5px");
}
Widget::~Widget()
{
delete ui;
}
void Widget::my_slot()
{
char s1[10]="123456";
char s2[10]="123456";
if(ui->lineEdit->text()==s1&&ui->lineEdit_2->text()==s2)
{
qDebug() << "登陆成功";
p1->setText("登录成功");
WorkerThread *worker = new WorkerThread();//分离式线程时间延迟
worker->start();
connect(worker, &WorkerThread::resultReady,this,&Widget::my_slot2);
}
else
{
p1->setText("登陆失败、请重新登录");
p1->setStyleSheet("background-color:rgb(51,200,253)");
ui->lineEdit->clear();
ui->lineEdit_2->clear();
WorkerThread *worker = new WorkerThread();
worker->start();
connect(worker, &WorkerThread::resultReady,this,&Widget::my_slot3);
}
}
void Widget::my_slot2()
{
qDebug() << "关闭";
close();
}
void Widget::my_slot3()
{
p1->setText("登录");
p1->setStyleSheet("background-color:rgb(31,200,253);color:white;\
border-radius:5px");
}
登录失败效果
登录成功效果