QT界面制作
作业
1> 创建一个新项目,将默认提供的程序都注释上意义
pro文件
QT += core gui
#引入类库 core,gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#超过版本4会加上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 \
logon.cpp
#头文件管理
HEADERS += \
logon.h
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
.h文件
#ifndef LOGON_H
#define LOGON_H
//防止头文件重复包含
#include <QWidget>
//自定义类,公共继承QWidget:QWidget
class Logon : public QWidget
{
Q_OBJECT
public:
Logon(QWidget *parent = nullptr); //构造函数的声明,默认参数
~Logon();//继承的虚析构函数
};
#endif // LOGON_H
源文件
#include "logon.h"
//构造函数定义
Logon::Logon(QWidget *parent)
: QWidget(parent)
{
}
//析构函数定义
Logon::~Logon()
{
}
主程序
#include "logon.h"
//头文件包含
#include <QApplication>
//应用程序头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //实例化对象,调用有参构造
Logon w; //自定义类调用无参构造构造界面
w.show(); //调用展示函数
return a.exec();//轮询等待界面指令
}
2> 使用代码的形式实现登录框
头文件
#ifndef LOGON_H
#define LOGON_H
//防止头文件重复包含
#include <QWidget>
#include <QPushButton>
#include<QLineEdit>
#include<QLabel>
#include <QMovie>
//自定义类,公共继承QWidget:QWidget
class Logon : public QWidget
{
Q_OBJECT
public:
Logon(QWidget *parent = nullptr); //构造函数的声明,默认参数
~Logon();//继承的虚析构函数
};
#endif // LOGON_H
源文件
#include "logon.h"
//构造函数定义
Logon::Logon(QWidget *parent)
: QWidget(parent)
{
this->setWindowTitle("QQ");
this->setWindowIcon(QIcon("D:/shixun/Qt/icon_QQ.png"));
this->setFixedSize(375,667);
QPushButton *btn1 = new QPushButton("登录",this);
btn1->resize(80,40);
btn1->move(147,450);
btn1->setStyleSheet("color:white;background-color:rgba(135,206,235,0.5);border-radius:10px");
QLineEdit *edit1 = new QLineEdit;
edit1->setParent(this);
edit1->resize(200,30);
edit1->move(87,380);
edit1->setAlignment(Qt::AlignCenter);
edit1->setPlaceholderText("输入密码");
edit1->setStyleSheet("background-color:rgba(255,255,255,0.5)");
edit1->setEchoMode(QLineEdit::Password);
QLineEdit *edit2 = new QLineEdit;
edit2->setParent(this);
edit2->resize(200,30);
edit2->move(edit1->x(),330);
edit2->setAlignment(Qt::AlignCenter);
edit2->setPlaceholderText("输入账号");
edit2->setStyleSheet("background-color:rgba(255,255,255,0.5)");
QLabel *lab1 = new QLabel(this);
lab1->resize(375,667);
lab1->setPixmap(QPixmap("D:\\shixun\\Qt\\background.jpg"));
lab1->setScaledContents(true);
QLabel *lab2 = new QLabel(this);
lab2->resize(120,90);
lab2->move(127,220);
QMovie *movie = new QMovie("D:/XH/bs.gif");
lab2->setMovie(movie);
movie->start();
lab2->setScaledContents(true);
lab1->lower();
}
//析构函数定义
Logon::~Logon()
{
}
主程序
#include "logon.h"
//头文件包含
#include <QApplication>
//应用程序头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //实例化对象,调用有参构造
Logon w; //自定义类调用无参构造构造界面
w.show(); //调用展示函数
return a.exec();//轮询等待界面指令
}