?一、繼續(xù)完善登錄框,當(dāng)?shù)顷懗晒r(shí),關(guān)閉登陸頁(yè)面,跳轉(zhuǎn)到新的界面中
源碼:
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QDebug> //用于打印輸出
#include <QIcon> //圖標(biāo)頭文件
#include <QPushButton> //按鈕類頭文件
#include <QLineEdit> //行編輯器類
#include <QLabel> //標(biāo)簽文件
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
//標(biāo)簽類
QLabel *lab1;
QLabel *lab2;
QLabel *lab3;
//行編輯器類
QLineEdit *edit1;
QLineEdit *edit2;
//按鈕類
QPushButton *btn1;
QPushButton *btn2;
//自定義信號(hào)
signals:
void jump(); //跳轉(zhuǎn)信號(hào)
public slots:
void cancel_slot();
void login_slot();
};
#endif // WIDGET_H
second.h
#ifndef SECOND_H
#define SECOND_H
#include <QWidget>
#include <QDebug> //用于打印輸出
#include <QIcon> //圖標(biāo)頭文件
#include <QPushButton> //按鈕類頭文件
#include <QLineEdit> //行編輯器類
#include <QLabel> //標(biāo)簽文件
namespace Ui {
class Second;
}
class Second : public QWidget
{
Q_OBJECT
public:
explicit Second(QWidget *parent = nullptr);
~Second();
//標(biāo)簽類
QLabel *lab1;
public slots:
void jump_slot();
private:
Ui::Second *ui;
};
#endif // SECOND_H
widget.cpp
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
//設(shè)置整體大小、標(biāo)題及圖標(biāo)
this->setFixedSize(700, 600);
this->setWindowTitle("IKUN真愛小屋?");
this->setWindowIcon(QIcon(":/icon/1.jpeg"));
this->setWindowOpacity(0.95); //設(shè)置透明度
//設(shè)置Logo標(biāo)簽
lab1 = new QLabel(this);
lab1->resize(700,200);
lab1->setPixmap(QPixmap(":/icon/2.JPG"));
lab1->setScaledContents(true); //設(shè)置內(nèi)容自適應(yīng)
//設(shè)置用戶和密碼所圖標(biāo)
lab2 = new QLabel(this);
lab2->resize(50,50);
lab2->setPixmap(QPixmap(":/icon/username.png"));
lab2->setScaledContents(true);
lab2->move(150,260);
lab3 = new QLabel(this);
lab3->resize(50,50);
lab3->setPixmap(QPixmap(":/icon/passwd.jpg"));
lab3->setScaledContents(true);
lab3->move(150,360);
//設(shè)置行編輯器
edit1 = new QLineEdit(this);
edit1->resize(190, 40);
edit1->move(280, 265);
edit1->setStyleSheet("border : none; "
"border-bottom: 2px solid grey;");
edit1->setPlaceholderText("賬號(hào):");
edit2 = new QLineEdit(this);
edit2->resize(190, 40);
edit2->move(280, 365);
edit2->setStyleSheet("border : none; "
"border-bottom: 2px solid grey;");
edit2->setPlaceholderText("密碼:");
edit2->setEchoMode(QLineEdit::Password);
//設(shè)置按鈕 登錄
btn1 = new QPushButton(this);
btn1->setText("登錄");
btn1->resize(130,40);
btn1->move(150, 490);
btn1->setIcon(QIcon(":/icon/login.png"));
//手動(dòng)將登錄與自定義的槽函數(shù)進(jìn)行連接,該連接是友好的連接qt-5
connect(btn1, &QPushButton::clicked, this, &Widget::login_slot);
//設(shè)置按鈕 取消
btn2 = new QPushButton(this);
btn2->setText("取消");
btn2->resize(130,40);
btn2->move(440, 490);
btn2->setIcon(QIcon(":/icon/cancel.png"));
//手動(dòng)將取消按鈕的clicked信號(hào)與自定義的槽函數(shù)進(jìn)行連接-qt4
connect(btn2, SIGNAL(clicked()), this, SLOT(cancel_slot()));
}
Widget::~Widget()
{
}
//自定義槽函數(shù)的實(shí)現(xiàn)
void Widget::cancel_slot(){
this->close();
}
//自定義槽函數(shù)的實(shí)現(xiàn)
void Widget::login_slot(){
if(this->edit1->text() == "admin" && this->edit2->text() == "123456"){
qDebug() << "登陸成功!";
emit jump(); //發(fā)射跳轉(zhuǎn)函數(shù)信號(hào)
this->close();
}else{
qDebug() << "登陸失敗,請(qǐng)重新登錄!";
this->edit2->clear();
}
}
second.cpp
#include "second.h"
#include "ui_second.h"
Second::Second(QWidget *parent) :
QWidget(parent),
ui(new Ui::Second)
{
ui->setupUi(this);
//整體設(shè)置
this->setWindowTitle("登錄成功提示");
this->setWindowIcon(QIcon(":/icon/3.jpeg"));
//設(shè)置跳轉(zhuǎn)標(biāo)簽
lab1 = new QLabel(this);
lab1->setText("登陸成功!");
lab1->resize(390,300);
lab1->setAlignment(Qt::AlignCenter);
}
Second::~Second()
{
delete ui;
}
//跳轉(zhuǎn)槽信號(hào)
void Second::jump_slot()
{
this->show();
}
main.cpp
#include "widget.h"
#include "second.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
Second s; //實(shí)例化第二個(gè)界面
//將第一個(gè)界面發(fā)射信號(hào)與第二個(gè)界面的展示槽函數(shù)連接
QObject::connect(&w, &Widget::jump, &s, &Second::jump_slot);
return a.exec();
}
二、新建一個(gè)工程文件,將默認(rèn)提供的代碼注釋
三、思維導(dǎo)圖
?文章來源地址http://www.zghlxwxcb.cn/news/detail-609523.html文章來源:http://www.zghlxwxcb.cn/news/detail-609523.html
?
到了這里,關(guān)于嵌入式:QT Day2的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!