Second.h
- #ifndef SECOND_H
- #define SECOND_H
-
- #include
-
- namespace Ui {
- class Second;
- }
-
- class Second : public QWidget
- {
- Q_OBJECT
-
- public slots:
- void jump_slot(); //接收跳转信号的槽函数
- public:
- explicit Second(QWidget *parent = nullptr);
- ~Second();
-
- private:
- Ui::Second *ui;
- };
-
- #endif // SECOND_H
- #ifndef WIDGET_H
- #define WIDGET_H
-
- #include
- #include
- #include
- #include
- #include
- #include
- #include
- #include"second.h"
-
- QT_BEGIN_NAMESPACE
- namespace Ui { class Widget; }
- QT_END_NAMESPACE
-
- class Widget : public QWidget
- {
- Q_OBJECT
-
- signals: //该权限下定义属于自己的信号
- void my_signal(); //自定义一个无参无返回值的信号函数
- void jump(); //自定义跳转的信号函数;
-
- private slots:
- void my_slot(); //自定义无参无返回值的槽函数
- void my_slot1();
- public:
- Widget(QWidget *parent = nullptr);
- ~Widget();
-
- private:
- Ui::Widget *ui;
- QLabel* lab1;
- QLabel* lab2;
- QLabel* lab3;
- QLineEdit* edit1;
- QLineEdit* edit2;
- QPushButton *btn1;
- QPushButton *btn2;
- void on_quesBtn_clicked();
- void on_critBtn_clicked();
- void on_infoBtn_clicked();
- };
- #endif // WIDGET_H
- #include "widget.h"
- #include"second.h"
-
- #include
-
- int main(int argc, char *argv[])
- {
- QApplication a(argc, argv);
- Widget w;
- w.show();
- Second s; //定义第二个界面
- QObject::connect(&w,&Widget::jump,&s,&Second::jump_slot);
- return a.exec();
- }
second.cpp
- #include "second.h"
- #include "ui_second.h"
-
- Second::Second(QWidget *parent) :
- QWidget(parent),
- ui(new Ui::Second)
- {
- ui->setupUi(this);
- }
-
- Second::~Second()
- {
- delete ui;
- }
-
- //接收跳转信号对应的槽函数
- void Second::jump_slot()
- {
- 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->setFixedSize(800,600); //设置固定尺寸
- this->setWindowTitle("Widget"); //设置窗口标题
- this->setWindowIcon(QIcon(":/wodepeizhenshi.png")); //设置窗口图标
-
- //实例化标签
- lab1=new QLabel(this);
- lab1->setPixmap(QPixmap("D:\\qt23062\\day8\\logo.png")); //设置图片
- lab1->setScaledContents(true); //设置图片自动适应大小
- lab1->resize(800,270); //重新设置尺寸
- //标签2
- lab2=new QLabel(this);
- lab2->setPixmap(QPixmap(":/userName.jpg")); //设置图片
- lab2->setScaledContents(true); //设置图片自动适应大小
- lab2->resize(60,60); //重新设置尺寸
- lab2->move(200,300);
-
- //标签3
- lab3=new QLabel(this);
- lab3->setPixmap(QPixmap(":/passwd.jpg")); //设置图片
- lab3->setScaledContents(true); //设置图片自动适应大小
- lab3->resize(60,60); //重新设置尺寸
- lab3->move(lab2->x(),lab2->y()+90);
-
- //行编辑器
- edit1=new QLineEdit("",this);
- edit1->setPlaceholderText("账号");
- edit1->move(lab2->x()+75,lab2->y());
- edit1->resize(360,60);
-
- edit2=new QLineEdit("",this);
- edit2->setPlaceholderText("密码");
- edit2->move(lab3->x()+75,lab3->y());
- edit2->resize(360,60);
- edit2->setEchoMode(QLineEdit::Password); //设置回显模式
-
- //按钮
- btn1=new QPushButton(QIcon(":/login.png"),"登录",this);
- btn1->move(edit2->x()+50,edit2->y()+100);
- btn1->resize(110,70); //重新设置尺寸
-
- QFont font = btn1->font();
- font.setPointSize(16); // 设置字体大小为16像素
- btn1->setFont(font);
-
- QPushButton *btn2=new QPushButton(QIcon(":/cancel.png"),"取消",this);
- btn2->move(btn1->x()+150,btn1->y());
- btn2->resize(110,70); //重新设置尺寸
-
- QFont font1 = btn2->font();
- font1.setPointSize(16); // 设置字体大小为16像素
- btn2->setFont(font1);
-
- connect(btn1, &QPushButton::clicked, this, &Widget::my_slot);
- connect(btn2, &QPushButton::clicked, this, &Widget::my_slot1);
- connect(this,&Widget::my_signal,this,&Widget::close);
-
- }
-
- Widget::~Widget()
- {
- delete ui;
- }
-
- //槽函数
- void Widget::my_slot()
- {
- if(edit1->text()=="admin"&&edit2->text()=="123456")
- {
- //信息对话框
- on_infoBtn_clicked();
- }else
- {
- //错误对话框
- on_critBtn_clicked();
- }
- }
-
- void Widget::my_slot1()
- {
- //问题对话框
- on_quesBtn_clicked();
- }
-
- //问题对话框
- void Widget::on_quesBtn_clicked()
- {
- //1.调用构造函数实例化对象
- QMessageBox box(QMessageBox::Question, //图标
- "问题对话框", //对话框标题
- "是否确定要退出登入", //对话框文本内容
- QMessageBox::Yes|QMessageBox::No, //提供的按钮
- this); //父组件
- //box.setDetailedText("有上好的飞天茅台"); //提供详细文本内容
- //box.setDefaultButton(QMessageBox::No); //将no设置成默认按钮
-
- //2.调用exec函数运行对话框
- int ret=box.exec();
-
- //3.对结果进行判断
- if(ret==QMessageBox::Yes)
- {
- emit my_signal();
-
- }else if(ret==QMessageBox::No)
- {
-
- }
- }
-
- //信息对话框
- void Widget::on_infoBtn_clicked()
- {
- //直接调用静态成员函数完成对话框的实现
- int ret=QMessageBox::information(this, //父组件
- "信息对话框", //对话框标题
- "登入成功", //对话框文本内容
- QMessageBox::Ok, //对话框提供的按钮
- QMessageBox::Ok); //默认选中的按钮
- if (ret==QMessageBox::Ok)
- {
- emit my_signal();
- emit jump();
- }
- }
-
- //错误对话框
- void Widget::on_critBtn_clicked()
- {
- //1.调用构造函数实例化对象
- QMessageBox box(QMessageBox::Critical, //图标
- "错误对话框", //对话框标题
- "账号密码不匹配,是否重新登入", //对话框文本内容
- QMessageBox::Yes|QMessageBox::No, //提供的按钮
- this); //父组件
- //box.setDetailedText("账号密码不匹配,是否重新登入"); //提供详细文本内容
- //box.setDefaultButton(QMessageBox::No); //将no设置成默认按钮
- box.setButtonText(QMessageBox::Yes,"OK");
- box.setButtonText(QMessageBox::No,"cancel");
-
- //2.调用exec函数运行对话框
- int ret=box.exec();
-
- //3.对结果进行判断
- if(ret==QMessageBox::Yes)
- {
- edit2->clear();
-
- }else if(ret==QMessageBox::No)
- {
- emit my_signal();
- }
- }
-
-
-
-
-
-
-
-
-
-
-
-