QT分文件的各个部分的解析

工程

QT       += core gui network
#引入QT工程所需的类库core是核心库,gui是图形开发相关类库
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#超过4.0版本自动加上widgets类库
CONFIG += c++11
#支持C++后的版本
# 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

头文件

#ifndef MYWND_H
#define MYWND_H//防止文件重复包含

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

QT_BEGIN_NAMESPACE
namespace Ui { class mywnd; }//声名ui界面对应头文件的命名空间
QT_END_NAMESPACE
//自定义自己的界面类,公共继承自QWidge,父类中重写了绘制事件处理函数
class mywnd : public QWidget
{
    Q_OBJECT//信号与槽的元对象,没有这个对象,信号槽就不能使用

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

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

源文件

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

主函数

#include "mywnd.h"//引入自定义图形化界面类的头文件

#include <QApplication>//引热议应用程序的头文件

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//使用应用程序类,实例化一个应用程序的对象
    mywnd w;//用自定义的图形化界面实例化界面类实例化一个对象
    w.show();//调用show函数展示界面,父类提供的,可以展示自己的组件,以自己作为父类组件的所有子组件也会被展示出来
    return a.exec();//阻塞等待应用程序,防止应用程序结束,等待用户的操作,等待信号与槽,等待时间发生
}

实现的功能

代码

#include "mainwindow.h"
#include<QDebug>//信息调试类,用于输出数据使用
MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    this->setFixedSize(500,400);
    this->setWindowTitle("Widget");//设置窗口标题
    this->setWindowIcon(QIcon("C:\\Users\\74257\\Desktop\\icon\\icon\\ren.png"));//设置窗口图表
    QPushButton *button = new QPushButton;
    QPushButton *button1 = new  QPushButton(this);
    button->setParent(this);
    button->setText("登录");
    button1->setText("取消");
    button->resize(100,50);
    button1->resize(button->size());
    button->setIcon(QIcon("C:\\Users\\74257\\Desktop\\icon\\icon\\login.png"));
    button1->setIcon(QIcon("C:\\Users\\74257\\Desktop\\icon\\icon\\cancel.png"));
    button->move(100,300);
    button1->move(button->x()+200,button->y());

    //===============================================//
    QLineEdit *edit = new QLineEdit(this);
    edit->resize(200,40);
    edit->move(200,230);
    edit->setEchoMode(QLineEdit::Password);//设置回显模式
    edit->setPlaceholderText("密码");
    QLineEdit *edit1 = new QLineEdit(this);
    edit1->resize(edit->size());
    edit1->move(edit->x(),edit->y()-50);
    edit1->setPlaceholderText("QQ账号/手机/邮箱");
    //===================================================//
    QLabel *lab = new QLabel(this);
    lab->resize(40,40);
    // qla->setStyleSheet("background-color:yellow;");//填充颜色
    // qla->setAlignment(Qt::AlignCenter);//居中对齐
    lab->move(edit->x()-60,edit->y());
    lab->setPixmap(QPixmap("C:\\Users\\74257\\Desktop\\icon\\icon\\passwd.jpg"));
    lab->setScaledContents(true);
    QLabel *lab1 = new  QLabel(this);
    lab1->resize(lab->size());
    lab1->move(edit1->x()-60,edit1->y());
    lab1->setPixmap(QPixmap("C:\\Users\\74257\\Desktop\\icon\\icon\\userName.jpg"));
    lab1->setScaledContents(true);//将图片适应大小
    QLabel *lab2 = new QLabel(this);
    lab2->resize(500,150);
    lab2->setPixmap(QPixmap("C:\\Users\\74257\\Desktop\\icon\\icon\\logo.png"));
    lab2->setScaledContents(true);//将图片适应大小

}

MainWindow::~MainWindow()
{
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值