Qt基本框架介绍(一)

1.先新建一个工程,

Qt基本框架介绍(一)_第1张图片

2.这里会看到三个选项MainWindow、QWidget、QDialog,首先,这三个都是基类,由于QWidget里面东西相对于QMainWindow里纯净,我就选择QWidget。

Qt基本框架介绍(一)_第2张图片

3.初学的话,咱就吧那个生产.ui文件的选项给取消。

Qt基本框架介绍(一)_第3张图片

4.选择对应的编译器,我就用msvc2015,需要在电脑上安装对应版本vs。

Qt基本框架介绍(一)_第4张图片

5.一切准备就绪会得到以下框架

Qt基本框架介绍(一)_第5张图片

6.咱们对应代码,逐个解释

.pro

QT += core gui 
#添加Qt支持的模块,一个是core基础代码模块,包含运算的,gui是图形界面库。
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#如果QT主版本大于4(也就是说当前使用的是Qt5或者更高版本),则需要添加widgets模块。就是保证Qt4的兼容性。
CONFIG += c++11
#这一行代码QT中增加C++11编译,这个的加入相当的方便,后面我会介绍,例Lambda表达式
# 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
#定义编译选项,示当Qt的某些功能被标记为过时的,那么编译器会发出警告。
# 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 \
    mywidget.cpp
#头文件
HEADERS += \
    mywidget.h

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

mywidget.h 要搞懂继承是什么,你建立的窗口基类是啥

#ifndef MYWIDGET_H 
#define MYWIDGET_H

#include 

class MyWidget : public QWidget//继承
{
    Q_OBJECT

public:
    MyWidget(QWidget *parent = nullptr);
    ~MyWidget();
};

#endif // MYWIDGET_H

mywidget.cpp

#include "mywidget.h" 
//指定父对象
MyWidget::MyWidget(QWidget *parent)
    : QWidget(parent)
{
}
MyWidget::~MyWidget()
{
}

main.cpp

#include "mywidget.h" //首先引用头文件
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);//应用程序对象,(a)有且仅有一个
    MyWidget w;  //定义窗口对象
    w.show(); //显示窗口,窗口默认是不显示,所以要show()
    return a.exec(); //如果注释了,窗口一闪而逝,这个就是一个循环

}

你可能感兴趣的:(Qt)