目錄
1、創建項目
接下來要選擇基類,Mainwindow是帶菜單欄、工具欄的,QWidget基本窗口類;類名可以自己更改,繼承於Qwidget,頭文件源文件自動更改;暫時不需要創建界面。
然後直接點完成即可。這樣項目就創建完成了。
2、項目文件介紹
.pro
#-------------------------------------------------
#
# Project created by QtCreator 2018-09-16T13:04:03
#
#-------------------------------------------------
#模塊添加的地方,要添加頭文件是不夠的,還需要添加模塊,在頭文件地方按F1查看
QT += core gui
#高於Qt4版本,添加 QT +=widgets 兼容Qt4
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
#應用程序的名字
TARGET = mypro
#指定Mmakefile的類型,app可執行程序
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as 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 you use 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
#後面使用lambda表達式添加
CONFIG += C++11
.h
#ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>
class MyWidget : public QWidget
{
Q_OBJECT//信號和槽需要
public:
MyWidget(QWidget *parent = 0);
~MyWidget();
};
#endif // MYWIDGET_H
main.cpp
#include "mywidget.h"
//QApplication應用程序類
//Qt頭文件沒有.h
//頭文件和類名一樣
#include <QApplication>
int main(int argc, char *argv[])
{
//有且只有一個應用程序類的對象
QApplication a(argc, argv);
//MyWidget繼承於QWidget,QWidget是基本窗口類
//w就是一個窗口
MyWidget w;
//窗口創建默認是隱藏的,需要人爲顯示
w.show();
//讓程序一直執行,等待用戶操作
return a.exec();
}
mywidget.cpp
#include "mywidget.h"
MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
}
MyWidget::~MyWidget()
{
}
接下來會在mywidget.cpp裏寫代碼,因爲在執行構造函數的時候會執行我們寫的代碼。