登陆界面
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//设置固定大小
this->setFixedSize(400,300);
//设置窗口标题
this->setWindowTitle("HUAQI");
//设置窗口图标
this->setWindowIcon(QIcon("D:\\Qt\\icon\\icon\\wodepeizhenshi.png"));
//构建两个按钮
QPushButton *btn1=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\login.png"),"登录",this);
btn1->resize(100,40);
btn1->move(100,250);
QPushButton *btn2=new QPushButton(QIcon("D:\\Qt\\icon\\icon\\cancel.png"),"取消",this);
btn2->resize(btn1->size());
btn2->move(btn1->x()+150,btn1->y());
//构建两个行编辑器
QLineEdit *edit1=new QLineEdit(this);
edit1->resize(200,30);
edit1->setEchoMode(QLineEdit::Password);
edit1->setText("123456");
edit1->move(125,btn1->y()-50);
QLineEdit *edit2=new QLineEdit(this);
edit2->resize(200,30);
edit2->setText("admin");
edit2->move(125,edit1->y()-50);
//构建三个标签
QLabel *lab1=new QLabel(this);
lab1->resize(30,30);
lab1->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\passwd.jpg"));
lab1->setScaledContents(true);
lab1->move(edit1->x()-40,edit1->y());
QLabel *lab2=new QLabel(this);
lab2->resize(lab1->size());
lab2->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\userName.jpg"));
lab2->setScaledContents(true);
lab2->move(edit2->x()-40,edit2->y());
QLabel *lab3=new QLabel(this);
lab3->resize(400,120);
lab3->setPixmap(QPixmap("D:\\Qt\\icon\\icon\\logo.png"));
lab3->setScaledContents(true);
}
MainWindow::~MainWindow()
{
delete ui;
}
将工程文件进行注释
1.工程管理文件
QT += core gui
#引入QT所需要的库,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 \
mywnd.cpp
#对头文件进行管理
HEADERS += \
mywnd.h
#对UI文件进行管理
FORMS += \
mywnd.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
2.头文件
#ifndef MYWND_H
#define MYWND_H
//防止头文件重复包含
//父类的头文件
#include <QWidget>
QT_BEGIN_NAMESPACE
//声明UI文件对应的命名空间
namespace Ui { class MyWnd; }
QT_END_NAMESPACE
//公共继承QWidget
class MyWnd : public QWidget
{
//信号和槽的元对象,没有元对象,信号和槽就不能使用
Q_OBJECT
public:
//构造函数,带有一个默认变量
MyWnd(QWidget *parent = nullptr);
~MyWnd();
private:
//可以使用UI指针调用Ui界面中的组件
Ui::MyWnd *ui;
};
#endif // MYWND_H
3.源文件
#include "mywnd.h"
#include "ui_mywnd.h"
MyWnd::MyWnd(QWidget *parent)
//先行调用父类有参构造函数
: QWidget(parent)
//给自己类中的Ui指针舒适化空间
, ui(new Ui::MyWnd)
{
//调用Ui
ui->setupUi(this);
}
MyWnd::~MyWnd()
{
//释放指针空间
delete ui;
}
4.主程序文件
#include "mywnd.h"
//引入应用程序的头文件
#include <QApplication>
int main(int argc, char *argv[])
{
//实例化一个应用程序对象
QApplication a(argc, argv);
//实例化自定义的图形化界面的类
MyWnd w;
//展示界面
w.show();
//阻塞等待应用程序,等待用户操作
return a.exec();
}