.pro:
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 \
mainwindow.cpp
#管理头文件
HEADERS += \
mainwindow.h
#管理.ui文件
FORMS += \
mainwindow.ui
# 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 MAINWINDOW_H
#define MAINWINDOW_H //头文件的定义格式
#include <QMainWindow> //父类的头文件
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; } //声明ui界面对应头文件的命名空间
QT_END_NAMESPACE
//MainWindow为自己定义的类,公共继承自QMainWindow
class MainWindow : public QMainWindow
{
Q_OBJECT //信号与槽的元对象,没有的话,不能使用信号与槽
public:
//声明构造函数,并且带有一个默认参数
MainWindow(QWidget *parent = nullptr);
//析构函数的声明
~MainWindow();
private:
Ui::MainWindow *ui; //定义这个后,可以通过该指针调用UI界面上拖拽出来的组件
};
#endif // MAINWINDOW_H
源文件:
#include "mainwindow.h"
#include "ui_mainwindow.h" //俩头文件
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) //必须显性调用父类的有参构造
, ui(new Ui::MainWindow) //给自己定义的类中的成员申请空间,也就是那个UI指针
{
ui->setupUi(this); //调用UI里面的成员函数,给里面的组件实例化空间
}
MainWindow::~MainWindow()
{
delete ui; //释放申请的空间
}
main.cpp:
#include "mainwindow.h"
#include <QApplication> //俩头文件
int main(int argc, char *argv[])
{
QApplication a(argc, argv); //使用应用程序类,实例化一个应用程序的对象
MainWindow w; //用自定义的类实例化一个对象
w.show(); //调用实例化的对象,展示组件,该函数由父类提供,但也可以展示子类的组件
return a.exec(); //阻塞作用,等待用户的操作,等待信号与槽,等待事件发生,防止直接结束程序
}
#include "first.h"
first::first(QWidget *parent)
: QWidget(parent)
{
this->setFixedSize(650,550); //设置窗口尺寸
this->setWindowTitle("原神"); //设置窗口标题
this->setWindowIcon(QIcon("D:\\huaqin_c++\\yuansen.png")); //设置图标
//创建一个标签栏,logo
QLabel *l1 = new QLabel(" ",this);
l1->resize(650,200); //大小
l1->setPixmap(QPixmap("D:\\huaqin_c++\\jiemian.png")); //导入图片
l1->setScaledContents(true);
//创建一个标签栏,下半背景图
QLabel *l4 = new QLabel("",this);
l4->resize(650,345); //大小
l4->move(0,200); //定位
l4->setPixmap(QPixmap("D:\\huaqin_c++\\111.png")); //导入图片
l4->setScaledContents(true);
l4->setWindowOpacity(0.8); //透明度
//创建一个标签栏,账号的图标
QLabel *l2 = new QLabel(" ",this);
l2->resize(50,50); //大小
l2->setPixmap(QPixmap("D:\\huaqin_c++\\zanhao.png")); //导入图片
l2->setScaledContents(true);
l2->move(170,250); //定位
l2->setWindowOpacity(0.2); //透明度
//创建一个标签栏,密码的图标
QLabel *l3 = new QLabel(" ",this);
l3->setPixmap(QPixmap("D:\\huaqin_c++\\passwd.jpg")); //导入图片
l3->resize(50,50); //大小
l3->setScaledContents(true);
l3->move(170,320); //定位
l3->setWindowOpacity(0.2); //透明度
//账号的行编辑器
QLineEdit *q1 = new QLineEdit(this);
q1->resize(250,50); //大小
q1->move(250,250); //定位
q1->setPlaceholderText("账号"); //占位文本
//密码的行编辑器
QLineEdit *q2 = new QLineEdit(this);
q2->resize(250,50); //大小
q2->move(250,320); //定位
q2->setPlaceholderText("密码"); //占位文本
q2->setEchoMode(QLineEdit::Password); //设置不回显
q2->setMaxLength(8); //设置可输入最大长度
q2->setWindowOpacity(0.2); //透明度
//登录按钮
QPushButton *b1 = new QPushButton(this);
b1->setText("登录"); //按钮上的文本
b1->resize(80,50); //大小
b1->move(250,400); //定位
b1->setIcon(QIcon("D:\\huaqin_c++\\login.png")); //导入图片
//取消按钮
QPushButton *b2 = new QPushButton("取消",this);
b2->resize(80,50); //大小
b2->move(420,400); //定位
b2->setIcon(QIcon("D:\\huaqin_c++\\cancel.png")); //导入图片
}
first::~first(){}
面试题 - GitMind
在线思维导图 - GitMind