1、工程管理文件
QT += core gui
#工程项目所包含的类库 core核心库 gui图形化界面库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#4.0版本后,自动添加widgets库
CONFIG += c++11
#支持c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
#管理文件
SOURCES += \
main.cpp \
firstwindow.cpp
#管理头文件
HEADERS += \
firstwindow.h
#管理ui界面文件
FORMS += \
firstwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
RESOURCES += \
icon.qrc
2、头文件
#ifndef FIRSTWINDOW_H
#define FIRSTWINDOW_H
//防止重复定义
#include <QWidget> //父类的头文件
#include <QDebug>
#include <QPushButton>
#include <QLabel>
#include <QLineEdit>
QT_BEGIN_NAMESPACE
namespace Ui { class FirstWindow; } //命名空间的声明,ui界面生成的头文件的命名空间
QT_END_NAMESPACE
class FirstWindow : public QWidget //自定义的类,继承自QMainWind类
{
Q_OBJECT //处理信号与槽的元对象
signals:
void my_signal();
public:
FirstWindow(QWidget *parent = nullptr); //构造函数的声明
~FirstWindow(); //析构函数的声明
QPushButton *btn1 = new QPushButton(this);
QPushButton *btn2 = new QPushButton(this);
QLineEdit *edit1 = new QLineEdit(this);
QLineEdit *edit2 = new QLineEdit(this);
QLabel *lab1 = new QLabel(this);
QLabel *lab2 = new QLabel(this);
QLabel *lab3 = new QLabel(this);
private slots:
void my_slot();
void my_slot1();
private:
Ui::FirstWindow *ui; //指向ui界面的指针,如果使用拖拽后的组件,需要使用该指针找到
};
#endif // FIRSTWINDOW_H
3、源文件
#include "firstwindow.h" //包含自定义的头文件
#include "ui_firstwindow.h" //包含ui界面的头文件
FirstWindow::FirstWindow(QWidget *parent) //构造函数的实现
: QWidget(parent) //调用父类的构造函数
, ui(new Ui::FirstWindow) //给指针成员初始化空间
{
ui->setupUi(this); //调用ui界面中的设置界面函数
//设置窗口固定大小
this->setFixedSize(this->size());
//设置窗口标题
this->setWindowTitle("myQQ");
//设置窗口图标
this->setWindowIcon(QIcon(":/icon/QQ.png"));
btn1->setText("登录");
btn1->resize(100,50);
btn1->move(400,500);
btn1->setIcon(QIcon(":/icon/denglu.png"));
btn2->setText("取消");
btn2->resize(100,50);
btn2->move(550,500);
btn2->setIcon(QIcon(":/icon/quxiao.png"));
//行编辑器
edit1->resize(500,50);
edit1->move(200,340);
edit1->setPlaceholderText("账号");
edit2->resize(500,50);
edit2->move(200,400);
edit2->setPlaceholderText("密码");
edit2->setEchoMode(QLineEdit::Password);
lab1->resize(40,40);
lab1->move(150,340);
lab1->setScaledContents(true);
lab1->setPixmap(QPixmap(":/icon/zhanghao.png"));
lab2->resize(40,40);
lab2->move(150,400);
lab2->setScaledContents(true);
lab2->setPixmap(QPixmap(":/icon/denglumima.png"));
lab3->resize(800,300);
lab3->setScaledContents(true);
lab3->setPixmap(QPixmap(":/icon/mmexport1682301765090.jpg"));
connect(btn1,SIGNAL(clicked()),this,SLOT(my_slot()));
connect(btn2,SIGNAL(clicked()),this,SLOT(my_slot1()));
}
FirstWindow::~FirstWindow() //析构函数的定义
{
delete ui;
}
//自定义槽函数
void FirstWindow::my_slot()
{
if(edit1->text() == "admin" && edit2->text() == "123456")
{
qDebug()<<"登录成功";
close();
}else {
qDebug()<<"账号和密码错误";
edit2->clear();
}
}
//自定义槽函数
void FirstWindow::my_slot1()
{
close();
}
4、测试文件
#include "firstwindow.h" //引入自定义头文件
#include <QApplication> //引入应用程序的对象
int main(int argc, char *argv[]) //主函数
{
QApplication a(argc, argv); //实例化应用程序的对象
FirstWindow w; //使用自定义对象在栈区实例化一个对象
w.show(); //调用show函数将界面显示出来
return a.exec(); //阻塞等待界面处理:等待信号与槽、等待用户操作界面、等待事件发生
}