qt下载
http://c.biancheng.net/view/3851.html
安装
http://c.biancheng.net/view/3858.html
建工程
工程结构
.pro 是项目管理文件,包括一些对项目的设置项。
Headers 分组,该节点下是项目内的所有头文件(.h)
Sources 分组:该节点下是项目内的所有 C++源文件(.cpp)
Forms 分组:该节点下是项目内的所有界面文件(.ui)。
第一个程序HelloWorld
#include <QApplication>
#include <QLabel>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QLabel *label = new QLabel("Hello, world");
label->show();
return app.exec();
}
引入QApplication
以及QLabel
这两个类,main()
函数中第一句是创建一个QApplication类
的实例。对于 Qt 程序来说,main()函数一般以创建 application 对象(GUI 程序是QApplication,非 GUI 程序是QCoreApplication。QApplication实际上是QCoreApplication的子类。)开始,后面才是实际业务的代码。
这个对象用于管理 Qt 程序的生命周期,开启事件循环,这一切都是必不可少的。在我们创建了QApplication
对象之后,直接创建一个QLabel
对象,构造函数赋值“Hello, world”
,当然就是能够在QLabel
上面显示这行文本。最后调用QLabel
的show()
函数将其显示出来。main()函数最后,调用app.exec()
,开启事件循环。我们现在可以简单地将事件循环理解成一段无限循环。正因为如此,我们在栈上构建了QLabel
对象,却能够一直显示在那里(试想,如果不是无限循环,main()函数立刻会退出,QLabel
对象当然也就直接析构了)。
报错记录:Error while building/deploying project HelloWorld (kit: Desktop Qt 5.9.0 MinGW 32bit) When executing step "qmake"
解决办法
1.https://www.cnblogs.com/zhangjunwu/p/7417566.html
上面办法不行检查一下路径是不是有中文,不能有中文
2.信号槽
信号槽是 Qt 框架引以为豪的机制之一。熟练使用和理解信号槽,能够设计出解耦的非常漂亮的程序,有利于增强我们的技术设计能力。
所谓信号槽,实际就是观察者模式。当某个事件发生之后,比如,按钮检测到自己被点击了一下,它就会发出一个信号(signal)。这种发出是没有目的的,类似广播。如果有对象对这个信号感兴趣,它就会使用连接(connect)函数,意思是,用自己的一个函数(成为槽(slot))来处理这个信号。
这让我想到之前玩过的一个游戏,机器人A发出信号,B会检测信号,然后执行指令,原理和这个差不多
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton button("Quit");
QObject::connect(&button, &QPushButton::clicked, &QApplication::quit);
button.show();
return app.exec();
}
QApplication app(argc, argv);开启事件循环
QPushButton button(“Quit”);创建一个按钮对象
QObject::connect(&button, &QPushButton::clicked, &QApplication::quit);
button对象,检测clicked按下事件,quit退出
button.show();显示按钮
connect函数有五个重载
QMetaObject::Connection connect(const QObject *, const char *,
const QObject *, const char *,
Qt::ConnectionType);
QMetaObject::Connection connect(const QObject *, const QMetaMethod &,
const QObject *, const QMetaMethod &,
Qt::ConnectionType);
QMetaObject::Connection connect(const QObject *, const char *,
const char *,
Qt::ConnectionType) const;
QMetaObject::Connection connect(const QObject *, PointerToMemberFunction,
const QObject *, PointerToMemberFunction,
Qt::ConnectionType)
QMetaObject::Connection connect(const QObject *, PointerToMemberFunction,
Functor);
connect函数一般形式
connect(sender, signal ,receiver, slot);
第一个是发出信号的对象,第二个是发送对象发出的信号,第三个是接收信号的对象,第四个是接收对象在接收到信号之后所需要调用的函数。
第二个程序
#include "mainwindow.h"
#include <QApplication>
#include <QPushButton>
#include <QDebug>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton button("quit");
QObject::connect(&button,&QPushButton::clicked,[](bool){
qDebug()<<"you click me";
});
button.show();
return app.exec();
}
pro文件上添上QMAKE_CXXFLAGS += -std=c++0x
才可以使用
qDebug()<<“you click me”;
3.自定义信号槽
//!!! Qt5
#include <QObject>
////////// newspaper.h
class Newspaper : public QObject
{
Q_OBJECT
public:
Newspaper(const QString & name) :
m_name(name)
{
}
void send()
{
emit newPaper(m_name);
}
signals:
void newPaper(const QString &name);
private:
QString m_name;
};
////////// reader.h
#include <QObject>
#include <QDebug>
class Reader : public QObject
{
Q_OBJECT
public:
Reader() {}
void receiveNewspaper(const QString & name)
{
qDebug() << "Receives Newspaper: " << name;
}
};
////////// main.cpp
#include <QCoreApplication>
#include "newspaper.h"
#include "reader.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
Newspaper newspaper("Newspaper A");
Reader reader;
QObject::connect(&newspaper, &Newspaper::newPaper,
&reader, &Reader::receiveNewspaper);
newspaper.send();
return app.exec();
}
QT模块介绍
Qt 5 模块分为 Essentials Modules 和 Add-on Modules 两部分。前者是基础模块,在所有平台上都可用;后者是扩展模块,建立在基础模块的基础之上,在能够运行 Qt 的平台之上可以酌情引入。