实现登录框中,当登录成功时,关闭登录界面,并跳转到其他界面。
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QIcon>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowIcon(QIcon("D:/QTProj/C++/icon/QQ.png"));
setWindowTitle("Login");
setFixedSize(500,400);
QLabel *labLogo = new QLabel(this);
labLogo->resize(500,150);
labLogo->setPixmap(QPixmap("D:/QTProj/C++/icon/logo.jpg"));
labLogo->setScaledContents(true);
QLabel *labAccount = new QLabel(this);
labAccount->resize(50,50);
labAccount->setPixmap(QPixmap("D:/QTProj/C++/icon/denglu.png"));
labAccount->setScaledContents(true);
labAccount->move(100,200);
QLabel *labPassword = new QLabel(this);
labPassword->resize(50,50);
labPassword->setPixmap(QPixmap("D:/QTProj/C++/icon/denglumima.png"));
labPassword->setScaledContents(true);
labPassword->move(100,280);
QLineEdit *editAccount = new QLineEdit(this);
editAccount->resize(200,50);
editAccount->move(180,200);
editAccount->setPlaceholderText("账号");
QLineEdit *editPassword = new QLineEdit(this);
editPassword->resize(200,50);
editPassword->move(180,280);
editPassword->setPlaceholderText("密码");
editPassword->setEchoMode(QLineEdit::Password);
QPushButton* btnLogin = new QPushButton("登录",this);
btnLogin->setIcon(QIcon("D:/QTProj/C++/icon/denglu_1.png"));
btnLogin->resize(100,50);
btnLogin->move(250,340);
QPushButton* btnCancel = new QPushButton("取消",this);
btnCancel->setIcon(QIcon("D:/QTProj/C++/icon/quxiao.png"));
btnCancel->resize(100,50);
btnCancel->move(380,340);
connect(btnLogin,&QPushButton::clicked,[=](){
if(editAccount->text()=="admin"&&editPassword->text()=="123456"){
emit toMainWindow();
close();
}else {
qDebug()<<"登录失败";
}
});
}
Widget::~Widget()
{
delete ui;
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QWidget>
namespace Ui {
class MainWindow;
}
class MainWindow : public QWidget
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
public slots:
void mainWindow_slot();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp文章来源:https://www.toymoban.com/news/detail-536039.html
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QWidget(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::mainWindow_slot()
{
show();
}
Main.cpp文章来源地址https://www.toymoban.com/news/detail-536039.html
#include "widget.h"
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
MainWindow mw;
QObject::connect(&w,&Widget::toMainWindow,&mw,&MainWindow::mainWindow_slot);
return a.exec();
}
到了这里,关于Qt day2的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!