实例代码:
.pro
1 QT += core gui
2
3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
4
5 CONFIG += c++11
6
7 # The following define makes your compiler emit warnings if you use
8 # any Qt feature that has been marked deprecated (the exact warnings
9 # depend on your compiler). Please consult the documentation of the
10 # deprecated API in order to know how to port your code away from it.
11 DEFINES += QT_DEPRECATED_WARNINGS
12
13 # You can also make your code fail to compile if it uses deprecated APIs.
14 # In order to do so, uncomment the following line.
15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
17
18 SOURCES += \
19 main.cpp \
20 mainwindow.cpp
21
22 HEADERS += \
23 mainwindow.h
24
25 FORMS += \
26 mainwindow.ui
27
28 # Default rules for deployment.
29 qnx: target.path = /tmp/$${TARGET}/bin
30 else: unix:!android: target.path = /opt/$${TARGET}/bin
31 !isEmpty(target.path): INSTALLS += target
View Code
main.cpp
1 #include "mainwindow.h"
2
3 #include <QApplication>
4
5 int main(int argc, char *argv[])
6 {
7 QApplication a(argc, argv);
8 MainWindow w;
9 w.show();
10 return a.exec();
11 }
View Code
mainwindow.h
1 #ifndef MAINWINDOW_H
2 #define MAINWINDOW_H
3
4 #include <QMainWindow>
5 #include <QDebug>
6
7 QT_BEGIN_NAMESPACE
8 namespace Ui { class MainWindow; }
9 QT_END_NAMESPACE
10
11 typedef QString(*callBack)(QString str);// 函数指针
12
13 // ******************调用类********************* //
14 class CallbackA
15 {
16 public:
17 void setCallBack(callBack callBackFunc);// 设置回调函数
18 callBack m_pCallBackFunc;// 回调函数成员
19 };
20
21 // ******************被调类********************* //
22 class CallbackB
23 {
24 public:
25 static QString callBack(QString str);// 声明回调函数
26 };
27
28 // ******************使用类********************* //
29 class MainWindow : public QMainWindow
30 {
31 Q_OBJECT
32
33 public:
34 MainWindow(QWidget *parent = nullptr);
35 ~MainWindow();
36
37 private:
38 Ui::MainWindow *ui;
39 };
40 #endif // MAINWINDOW_H
View Code
mainwindow.cpp
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3
4 MainWindow::MainWindow(QWidget *parent)
5 : QMainWindow(parent)
6 , ui(new Ui::MainWindow)
7 {
8 ui->setupUi(this);
9
10 CallbackA *oCallbackAo = new CallbackA();
11 oCallbackAo->setCallBack(&CallbackB::callBack);// 运行后输出“CallbackB:callBack: "CallbackA"”
12 }
13
14 MainWindow::~MainWindow()
15 {
16 delete ui;
17 }
18
19
20 QString CallbackB::callBack(QString str)
21 {
22 qDebug() << "CallbackB:callBack: " << str;
23 return "CallbackB:callBack: ";
24 }
25
26 void CallbackA::setCallBack(callBack callBackFunc)
27 {
28 m_pCallBackFunc = callBackFunc;
29 m_pCallBackFunc("CallbackA");
30 }
View Code
mainwindow.ui
1 <?xml version="1.0" encoding="UTF-8"?>
2 <ui version="4.0">
3 <class>MainWindow</class>
4 <widget class="QMainWindow" name="MainWindow">
5 <property name="geometry">
6 <rect>
7 <x>0</x>
8 <y>0</y>
9 <width>358</width>
10 <height>169</height>
11 </rect>
12 </property>
13 <property name="windowTitle">
14 <string>MainWindow</string>
15 </property>
16 <widget class="QWidget" name="centralwidget"/>
17 </widget>
18 <resources/>
19 <connections/>
20 </ui>
View Code