- VC++
- Qt Design Studio
Qt Visual Studio Tools
Find “Qt” in “Tools->Options” of Visual Studio and set the qmake path
Widget工程
- QtHello
“User Interface(.ui)file” is only available if you install the QtDesignStudio
UI
- Open the UI file using QT
Push Button
拖拽“Push Button”,并修改属性:
- ObjectName
btnHello
- Text
Click Me
Code Implementation
- QtHello.h
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_QtHello.h"
class QtHello : public QMainWindow
{
Q_OBJECT
public:
QtHello(QWidget *parent = Q_NULLPTR);
private:
Ui::QtHelloClass ui;
// add code // Declarative methods
private slots:
void helloQT();
};
- QtHello.cpp
#include "QtHello.h"
#include <QtWidgets/QMessageBox>
QtHello::QtHello(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
// add code // Association 3-Elements: Ui-Event-Method
connect(ui.btnHello, &QPushButton::clicked, this, &QtHello::helloQT);
// The other way to connect a signal to a slot is to use QObject::connect() and the SIGNAL and SLOT macros.
// connect(ui.btnHello, SIGNAL(clicked()), this, SLOT(helloQT()));
}
// add code // Implementation
void QtHello::helloQT()
{
QMessageBox msgBox;
msgBox.setText("Hello Qt!");
msgBox.exec();
}
Signals and Slots
- slot
The fun() declaration is placed in the slot.
In this case, it is helloQT().
connect
connect a signal to a slot:
- Pointer
需要关心信号的源头: 从哪里发出的信号 - Macro
只需要知道发出的是什么信号