QT-day1

实现华清远见登陆界面

cpp 复制代码
#include "mywnd.h"
#include <iostream>
#include <QDebug>
#include <QPushButton>
#include <QLineEdit>
#include <QLabel>

MyWnd::MyWnd(QWidget *parent)
    : QWidget(parent)
{
    //设置固定窗口大小长400,宽350
    this->setFixedSize(400,350);
    //设置窗口名Widget
    this->setWindowTitle("Widget");
    //导入图标
    this->setWindowIcon(QIcon("F:\\hqyj\\icon\\icon\\wodepeizhenshi.png"));
    //创建标签1华清远见
    QLabel *lab1 = new QLabel(this);
    lab1->resize(400,150);
    lab1->setPixmap(QPixmap("F:\\hqyj\\icon\\icon\\logo.png"));
    lab1->setScaledContents(true);
    //创建admin的标签
    QLabel *lab2 = new QLabel(this);
    lab2->resize(60,55);
    lab2->move(75,160);
    lab2->setPixmap(QPixmap("F:\\hqyj\\icon\\icon\\userName.jpg"));
    lab2->setScaledContents(true);
    //创建密码passwd的标签
    QLabel *lab3 = new QLabel(this);
    lab3->resize(60,55);
    lab3->move(lab2->x(),lab2->y()+65);
    lab3->setPixmap(QPixmap("F:\\hqyj\\icon\\icon\\passwd.jpg"));
    lab3->setScaledContents(true);
    //创建admin行编辑器
    QLineEdit *edit1 = new QLineEdit("admin",this);
    edit1->resize(200,55);
    edit1->move(lab2->x()+65,lab2->y());
    //创建密码passwd行编辑器
    QLineEdit *edit2 = new QLineEdit("123456",this);
    edit2->resize(200,55);
    edit2->move(lab3->x()+65,lab3->y());
    edit2->setEchoMode(QLineEdit::Password);
    //创建登录按钮
    QPushButton *bton1 = new QPushButton(QIcon("F:\\hqyj\\icon\\icon\\login.png"),"登录",this);
    bton1->resize(90,55);
    bton1->move(edit2->x()+20,edit2->y()+65);
    //创建取消按钮
    QPushButton *bton2 = new QPushButton(QIcon("F:\\hqyj\\icon\\icon\\cancel.png"),"登录",this);
    bton2->resize(90,55);
    bton2->move(bton1->x()+105,bton1->y());

}

MyWnd::~MyWnd()
{
}

测试结果

工程文件的注释

工程管理文件.pro

cpp 复制代码
#引入QT工程所需的类库core是核心库,gui是图形开发相关类库
QT       += core gui

#超过4.0版本自动加上widgets类库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

#支持C++11后的版本
CONFIG += 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 \
    mywind.cpp

#对头文件进行管理
HEADERS += \
    mywind.h

#对ui界面文件进行管理
FORMS += \
    mywind.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

头文件

cpp 复制代码
#ifndef MYWIND_H
#define MYWIND_H        //防止文件重复包含

#include <QMainWindow>  //父类的头文件

QT_BEGIN_NAMESPACE
namespace Ui { class MyWind; }
QT_END_NAMESPACE

//自定义自己的界面类,公共继承自QWidget,父类中重写了绘制事件处理函数
class MyWind : public QMainWindow
{
    Q_OBJECT            //信号与槽的元对象,没有这个对象,信号与槽就不能使用了

public:
    MyWind(QWidget *parent = nullptr); //构造函数的声明,并且有一个带默认参数的形参
    ~MyWind();                          //析构函数的声明

private:
    Ui::MyWind *ui;      //可以通过该指针调用ui界面上拖拽出来的组件
};
#endif // MYWIND_H

源文件

cpp 复制代码
#include "mywind.h"
#include "ui_mywind.h"

MyWind::MyWind(QWidget *parent)     //构造函数的定义
    : QMainWindow(parent)           //显性调用父类的有参构造完成对子类从父类继承下来成员的初始化工作
    , ui(new Ui::MyWind)            //给自己类中的指针成员初始化空间,ui界面中拖拽出来的组件,就在这个对象中
{
    ui->setupUi(this);              //调用Ui::MyWnd类里面的成员函数,给拖拽的组件实例化空间,并设置相关属性
}

MyWind::~MyWind()      // 定义析构函数
{
    delete ui;         //释放指针空间
}

主程序文件

cpp 复制代码
#include "mywind.h"         //引入自定义图形化界面类的头文件

#include <QApplication>     //引入应用程序类的头文件

int main(int argc, char *argv[])
{
    QApplication a(argc, argv); //使用应用程序类,实例化一个应用程序的对象
    MyWind w;           //用自定义的图形化界面类实例化一个对象
    w.show();           //调用show函数展示界面,父类提供的可以展示自己的组件,以自己作为父组件的所有子组件也会被展示出来
    return a.exec();    //阻塞等待应用程序,防止应用程序结束,等待用户操作、等待信号与槽、等待事件发生
}
相关推荐
ccddsdsdfsdf7 小时前
DBeaver怎么链接mongoDB
数据库·mongodb
AI玫瑰助手7 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车7 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋7 小时前
C++14特性
开发语言·c++·c++14特性
丷丩8 小时前
Postgresql基础实践教程(十一)各种Join
数据库·postgresql·join
读书札记20228 小时前
Qt界面卡死问题探讨及解决方法
qt
星夜夏空998 小时前
FreeRTOS学习(4)——内存映射
数据库·学习·mongodb
TheRouter9 小时前
AI Agent 记忆体系建设实战:短期、长期与工作记忆的工程实现
数据库·人工智能·oracle
Omics Pro9 小时前
首个!外源天然产物综合性代谢图谱
数据库·人工智能·算法·机器学习·r语言
JAVA社区9 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展