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 的平臺之上可以酌情引入。