QMainWindow 是Qt框架带来的一个预定义好的主窗口类.按照建立HelloWorld程序建立工程,直接运行,或有一个空窗口.
main().cpp
#include "mainwindow.h" #include <QApplication> #include <QLabel> int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
窗口实际上包含几个部分:
主窗口的最上面是Window Title, 也就是标题栏,通常用于显示标题和控制按钮,比如最大化、最小化、关闭等。
(标题栏可自己绘制,称为DirectUI技术,也就是无句柄绘制。)
Menu Bar,也就是菜单栏,用于显示菜单。
Status Bar ,称为状态栏,位于窗口最底部.当我们通过鼠标划过某空间时,可以在状态栏显示某些信息.如浏览器显示网址.
中间矩形区域:
Tool Bar Area,位于最外层,用于工具条区域.Qt支持搓个工具条.
Dock widget Area ,停靠窗口显示区域.
Central widget,工作区域.
通常我们会继承QMainWindow,以便获得各种窗口便利的函数.
pro 文件分析.
#------------------------------------------------- # # Project created by QtCreator 2017-06-13T11:13:35 # #------------------------------------------------- QT += core gui greaterThan(QT_MAJOR_VERSION, 4): QT += widgets TARGET = HelloWorld TEMPLATE = app # The following define makes your compiler emit warnings if you use # any feature of Qt which as 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 \ mainwindow.cpp HEADERS += \ mainwindow.h FORMS += \ mainwindow.ui
首先我们定义了Qt,告诉编译器,需要使用哪些模块.通常我们需要添加 core 和 gui .
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 若Qt 主版本号 大于4,也就是Qt5,则需要添加widgets(Qt5所有组件都是在widgets模块定义的).
TARGET = HelloWorld target 是生成的程序名字.
TEMPLATE = app 是生成makefile所使用的模板,比如 app就是编译成一个可执行程序,而lib则是编译成一个链接库(默认是动态链接库).
SOURCE 和 HEADERS ,就是项目所需要的源代码文件和头文件.