当前位置:首页 > 编程笔记 > 正文
已解决

QT-day2

来自网友在路上 170870提问 提问时间:2023-09-23 09:52:40阅读次数: 70

最佳答案 问答题库708位专家为你答疑解惑

1、完善登录框

头文件

widget.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QMessageBox>      //消息对话框类头文件
#include <QDebug>
#include <QPushButton>
#include "second.h"QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTsignals:void my_signal();       //定义一个无参无返回值的信号函数void jump();            //自定义跳转信号函数
private slots:void my_slot();         //自定义无参无返回值槽函数public:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_dengBtn_clicked();void on_quBtn_clicked();private:Ui::Widget *ui;second *s1;
};
#endif // WIDGET_H

second.h

#ifndef SECOND_H
#define SECOND_H#include <QWidget>namespace Ui {
class second;
}class second : public QWidget
{Q_OBJECTpublic slots:void jump_slot();           //接收跳转信号的槽函数public:explicit second(QWidget *parent = nullptr);~second();private:Ui::second *ui;
};#endif // SECOND_H

源文件

widget.cpp

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);s1 = new second;//使用qt5版本,把登录、取消按钮连接connect(ui->dengBtn, &QPushButton::clicked, this, &Widget::my_slot);connect(ui->quBtn, &QPushButton::clicked, this, &Widget::my_slot);connect(this, &Widget::jump, s1, &second::jump_slot);
}Widget::~Widget()
{delete ui;
}//登录按钮对应的槽函数
void Widget::on_dengBtn_clicked()
{//传入账号密码QString zh = ui->lineEdit1->text();QString mm = ui->lineEdit2->text();if("admin"!=zh || "123456"!=mm){//1、调用构造函数实例化对象QMessageBox box(QMessageBox::Critical,               //图标"错误",                                  //对话框标题"账号密码不匹配,是否重新登录",                             //对话框文本内容QMessageBox::Ok | QMessageBox::Cancel,     //提供的按钮this);                                  //父组件//box.setDetailedText("");               //提供详细文本内容box.setDefaultButton(QMessageBox::Ok);         //将yes设置成默认按钮//2、调用exec函数运行对话框int ret = box.exec();//3、对结果进行判断if(ret == QMessageBox::Ok){ui->lineEdit1->clear();ui->lineEdit2->clear();qDebug()<<"继续登录";}else if(ret == QMessageBox::Cancel){close();}}else if("admin"==zh && "123456"==mm){//1、调用构造函数实例化对象QMessageBox box(QMessageBox::Information,               //图标"信息",                                 //对话框标题"登陆成功",                       //对话框文本内容QMessageBox::Ok,               //提供的按钮this);                          //父组件//box.setDetailedText("");                    //提供详细文本内容box.setDefaultButton(QMessageBox::Ok);         //将Ok设置成默认按钮//2、调用exec函数运行对话框int ret = box.exec();//3、对结果进行判断if(ret == QMessageBox::Ok){emit jump();}}}//取消
void Widget::on_quBtn_clicked()
{//1、调用静态函数实例化对象int ret = QMessageBox::information(this,              //父组件"问题",              //对话框标题"是否确定退出登录",            //对话框文本内容QMessageBox::Yes | QMessageBox::No,    //对话框提供的按钮QMessageBox::No);          //默认选中的按钮//3、对结果进行判断if(ret == QMessageBox::Yes){close();}
}

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();
}

主程序

#include "widget.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();return a.exec();
}

2、思维导图

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"QT-day2":http://eshow365.cn/6-12031-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!