头文件1
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include<QPushButton>
#include<QLineEdit>
#include<QLabel>class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();int btn;int btn1;
signals://void my_signal();void jump();
public slots://void on_btn2_my_slot();
private slots: // 确保在这里声明槽函数void onLoginClicked();void onloginclicked1();private:QLineEdit *edit1; // 声明为类成员QLineEdit *edit2; // 声明为类成员
};
#endif // WIDGET_H
头文件2
#ifndef SECOND_H
#define SECOND_H#include <QWidget>namespace Ui {
class second;
}class second : public QWidget
{Q_OBJECTpublic:explicit second(QWidget *parent = nullptr);~second();
public slots:void jump_slot(){this->show();}private:Ui::second *ui;
};#endif // SECOND_H
主函数
#include "widget.h"
#include"second.h"#include <QApplication>int main(int argc, char *argv[])
{QApplication a(argc, argv);Widget w;w.show();second s;//s.show();QObject::connect(&w,&Widget::jump,&s,&second::jump_slot);return a.exec();
}
源文件1
#include "widget.h"
#include <QMessageBox> // 添加QMessageBox头文件Widget::Widget(QWidget *parent): QWidget(parent)
{this->setWindowTitle("QQ");this->setWindowIcon(QIcon("D:\\嵌入式学习\\墨镜.png"));this->setFixedSize(800, 600);QPushButton * btn1 = new QPushButton;btn1->setParent(this);btn1->setText("登录");btn1->setStyleSheet("color:white; background-color:skyblue; border-radius:10px;");btn1->resize(100, 50);btn1->move(270, 450);QPushButton * btn2 = new QPushButton("取消", this);btn2->setStyleSheet("color:white; background-color:skyblue; border-radius:10px;");btn2->resize(btn1->size());btn2->move(btn1->x() + btn1->width() + 20, btn1->y());connect(btn1, &QPushButton::clicked, this, &Widget::onLoginClicked); // 使用Qt5版本的信号与槽连接connect(btn2, &QPushButton::clicked, this, &Widget::onloginclicked1); // 使用Qt5版本的信号与槽连接QLabel * lab1 = new QLabel("密码", this);lab1->move(btn1->x() - 50, btn1->y() - 80);QLabel * lab2 = new QLabel("账号", this);lab2->move(lab1->x(), lab1->y() - 50);edit1 = new QLineEdit(this); // 将edit1声明为类成员edit1->move(lab1->x() + lab1->width() - 50, lab1->y() - 5);edit1->setStyleSheet("color:black; background-color:pink;");edit1->resize(300, 30);edit1->setPlaceholderText("密码");edit1->setEchoMode(QLineEdit::Password);edit1->setAlignment(Qt::AlignCenter);edit2 = new QLineEdit(this); // 将edit2声明为类成员edit2->move(edit1->x(), lab2->y() - 5);edit2->resize(300, 30);edit2->setStyleSheet("color:black; background-color:pink;");QLabel * lab3 = new QLabel("hhh", this);lab3->resize(800, 250);lab3->setScaledContents(true);lab3->setPixmap(QPixmap("C:\\Users\\许曾嘉\\Desktop\\许曾嘉\\许曾嘉\\微信图片_20240928195223"));
}void Widget::onLoginClicked() {QString username = edit2->text(); // 获取账号输入QString password = edit1->text(); // 获取密码输入// 设置正确的账号和密码if (username == "xzj" && password == "123456") {QMessageBox::information(this, "登录成功", "欢迎登录!");this->close();emit jump();} else {btn1=QMessageBox::warning(this, "登录失败", "账号或密码错误,是否重试。",QMessageBox::Yes|QMessageBox::No);if(btn1==QMessageBox::Yes){edit1->clear();}else if(btn1==QMessageBox::No){this->close();}}
}
void Widget::onloginclicked1()
{QMessageBox box(QMessageBox::Question,"问题","确定取消登录",QMessageBox::Yes|QMessageBox::No,this);btn=box.exec();if(btn==QMessageBox::Yes){this->close();}else if(btn==QMessageBox::No){box.close();}
源文件2
#include "second.h"
#include "ui_second.h"second::second(QWidget *parent) :QWidget(parent),ui(new Ui::second)
{ui->setupUi(this);this->setFixedSize(800,600);
}second::~second()
{delete ui;
}