目录
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里写代码,因为在执行构造函数的时候会执行我们写的代码。