Qt-Qt之Invokemethhod使用方法与详解

资料:
https://www.jianshu.com/p/c77e28a81708 Qt invokeMethod 异步调用
https://download.csdn.net/download/zhujianqiangqq/92466941 csdn下载代码包
注意项:
Qt::DirectConnection,则会立即调用该成员。(同步调用)
Qt::QueuedConnection,则会发送一个QEvent,并在应用程序进入主事件循环后立即调用该成员。(异步调用)
Qt::BlockingQueuedConnection,则将以与Qt :: QueuedConnection相同的方式调用该方法,除了当前线程将阻塞直到事件被传递。使用此连接类型在同一线程中的对象之间进行通信将导致死锁。(异步调用)
Qt::AutoConnection,则如果obj与调用者位于同一个线程中,则会同步调用该成员; 否则它将异步调用该成员。
注:
1.必须引入Q_OBJECT机制。
2.每个对外的方法必须写Q_INVOKABLE进行定义。
3.必须要使用Q_RETURN_ARG()宏来封装函数返回值、Q_ARG()宏来封装函数参数。
代码实例:
.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 TestInvokemethhodWidget.cpp \ 20 main.cpp \ 21 mainwindow.cpp \ 22 ttestwidget.cpp 23 24 HEADERS += \ 25 TestInvokemethhodWidget.h \ 26 mainwindow.h \ 27 ttestwidget.h 28 29 FORMS += \ 30 mainwindow.ui 31 32 # Default rules for deployment. 33 qnx: target.path = /tmp/$${TARGET}/bin 34 else: unix:!android: target.path = /opt/$${TARGET}/bin 35 !isEmpty(target.path): INSTALLS += target
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 }
mainwindow.h
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 #include <QDebug> 6 7 #include "TestInvokemethhodWidget.h" 8 #include "ttestwidget.h" 9 10 QT_BEGIN_NAMESPACE 11 namespace Ui { class MainWindow; } 12 QT_END_NAMESPACE 13 14 class MainWindow : public QMainWindow 15 { 16 Q_OBJECT 17 18 public: 19 MainWindow(QWidget *parent = nullptr); 20 ~MainWindow(); 21 22 private slots: 23 void on_pushButton_clicked(); 24 25 void on_pushButton_2_clicked(); 26 27 void on_pushButton_3_clicked(); 28 29 void on_pushButton_4_clicked(); 30 void on_ShowText(QString value); 31 void on_pushButton_5_clicked(); 32 33 void on_pushButton_6_clicked(); 34 35 void on_pushButton_7_clicked(); 36 37 private: 38 Ui::MainWindow *ui; 39 TTestInvokemethhodWidget *mTestInvokemethhodWidget = nullptr; 40 TTestWidget *mTestWidget = nullptr; 41 QLabel *mLabel = nullptr; 42 }; 43 #endif // MAINWINDOW_H
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 this->setWindowTitle(QStringLiteral("Qt之Invokemethhod使用方法与详解")); 10 11 mLabel = new QLabel(QStringLiteral("主界面"), this); 12 mLabel->move(10, 300); 13 14 mTestInvokemethhodWidget = new TTestInvokemethhodWidget(this); 15 mTestInvokemethhodWidget->move(0, 0); 16 connect(mTestInvokemethhodWidget, &TTestInvokemethhodWidget::do_Text, this, &MainWindow::on_ShowText); 17 18 mTestWidget = new TTestWidget(this); 19 mTestWidget->move(200, 0); 20 connect(mTestWidget, &TTestWidget::do_Text, this, &MainWindow::on_ShowText); 21 } 22 23 MainWindow::~MainWindow() 24 { 25 delete ui; 26 } 27 28 29 void MainWindow::on_pushButton_clicked() 30 { 31 if(mTestInvokemethhodWidget) mTestInvokemethhodWidget->TestInvokemethhod(); 32 } 33 34 void MainWindow::on_pushButton_2_clicked() 35 { 36 37 } 38 39 void MainWindow::on_pushButton_3_clicked() 40 { 41 bool bReturn = false; 42 QString s = "bReturn:%1"; 43 if(mTestInvokemethhodWidget) bReturn = mTestInvokemethhodWidget->TestInvokemethhodReturn(); 44 ui->textEdit->append(s.arg(bReturn)); 45 } 46 47 void MainWindow::on_pushButton_4_clicked() 48 { 49 QLabel *oLabel = nullptr; 50 QString s = "oLabel:%1"; 51 if(mTestInvokemethhodWidget) oLabel = mTestInvokemethhodWidget->TestInvokemethhodObject(); 52 if(oLabel) ui->textEdit->append(s.arg(oLabel->text())); 53 } 54 55 void MainWindow::on_ShowText(QString value) 56 { 57 ui->textEdit->append(value); 58 } 59 60 void MainWindow::on_pushButton_5_clicked() 61 { 62 if(mTestInvokemethhodWidget) mTestInvokemethhodWidget->setInvokemethhod(9); 63 } 64 65 void MainWindow::on_pushButton_6_clicked() 66 { 67 if(mTestInvokemethhodWidget) mTestInvokemethhodWidget->setInvokemethhodObject(mLabel); 68 } 69 70 void MainWindow::on_pushButton_7_clicked() 71 { 72 int n = 0; 73 QString s = "AndOut:%1"; 74 if(mTestInvokemethhodWidget) n = mTestInvokemethhodWidget->setInvokemethhodInAndOut(6); 75 ui->textEdit->append(s.arg(n)); 76 }
TestInvokemethhodWidget.h
1 #ifndef TTESTINVOKEMETHHOD_H 2 #define TTESTINVOKEMETHHOD_H 3 4 #include <QObject> 5 #include <QWidget> 6 #include <QVBoxLayout> 7 #include <QPushButton> 8 #include <QMessageBox> 9 #include <QLabel> 10 11 class TTestInvokemethhodWidget : public QWidget 12 { 13 Q_OBJECT // 必须写Q_OBJECT 14 public: 15 TTestInvokemethhodWidget(QWidget* parent = nullptr); 16 Q_INVOKABLE void TestInvokemethhod();// 必须写Q_INVOKABLE 17 Q_INVOKABLE bool TestInvokemethhodReturn(); 18 Q_INVOKABLE QLabel* TestInvokemethhodObject(); 19 20 Q_INVOKABLE void setInvokemethhod(int value); 21 Q_INVOKABLE void setInvokemethhodObject(QLabel *label); 22 Q_INVOKABLE int setInvokemethhodInAndOut(int value); 23 QLabel *mL = nullptr; 24 private slots: 25 signals: 26 void do_Text(QString value); 27 }; 28 29 #endif // TTESTINVOKEMETHHOD_H
TestInvokemethhodWidget.cpp
1 #include "TestInvokemethhodWidget.h" 2 3 TTestInvokemethhodWidget::TTestInvokemethhodWidget(QWidget *parent) 4 :QWidget(parent) 5 { 6 this->setObjectName("TTestInvokemethhodWidget"); 7 this->resize(100, 280); 8 9 mL = new QLabel(QStringLiteral("子界面-被调用界面"), this); 10 } 11 12 void TTestInvokemethhodWidget::TestInvokemethhod() 13 { 14 QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("TestInvokemethhod 调用")); 15 } 16 17 bool TTestInvokemethhodWidget::TestInvokemethhodReturn() 18 { 19 QMessageBox::information(nullptr, QStringLiteral("提示"), QStringLiteral("TestInvokemethhodReturn 调用")); 20 return true; 21 } 22 23 QLabel *TTestInvokemethhodWidget::TestInvokemethhodObject() 24 { 25 return mL; 26 } 27 28 void TTestInvokemethhodWidget::setInvokemethhod(int value) 29 { 30 QString s = "TTestInvokemethhodWidget::setInvokemethhod:%1"; 31 emit do_Text(s.arg(value)); 32 } 33 34 void TTestInvokemethhodWidget::setInvokemethhodObject(QLabel *label) 35 { 36 if(nullptr != label) 37 { 38 emit do_Text(label->text()); 39 } 40 } 41 42 int TTestInvokemethhodWidget::setInvokemethhodInAndOut(int value) 43 { 44 int n = 0; 45 n = 15 + value; 46 return n; 47 }
ttestwidget.h
1 #ifndef TTESTWIDGET_H 2 #define TTESTWIDGET_H 3 4 #include <QObject> 5 #include <QWidget> 6 #include <QVBoxLayout> 7 #include <QPushButton> 8 #include <QMessageBox> 9 #include <QLabel> 10 #include <QDebug> 11 12 class TTestWidget : public QWidget 13 { 14 Q_OBJECT 15 public: 16 TTestWidget(QWidget* parent = nullptr); 17 private: 18 QLabel *mLabel = nullptr; 19 20 QPushButton *mB1 = nullptr; 21 QPushButton *mB2 = nullptr; 22 QPushButton *mB3 = nullptr; 23 24 QPushButton *mB4 = nullptr; 25 QPushButton *mB5 = nullptr; 26 QPushButton *mB6 = nullptr; 27 private slots: 28 void on_B1(); 29 void on_B2(); 30 void on_B3(); 31 void on_B4(); 32 void on_B5(); 33 void on_B6(); 34 signals: 35 void do_Text(QString value); 36 }; 37 38 #endif // TTESTWIDGET_H
ttestwidget.cpp
1 #include "ttestwidget.h" 2 3 TTestWidget::TTestWidget(QWidget *parent) 4 :QWidget(parent) 5 { 6 this->resize(400, 300); 7 8 mLabel = new QLabel(QStringLiteral("子界面-调用界面"), this); 9 10 mB1 = new QPushButton(QStringLiteral("运行时调用TestInvokemethhod"), this); 11 mB1->move(20, 20); 12 mB1->resize(250, 25); 13 connect(mB1, &QPushButton::clicked, this, &TTestWidget::on_B1); 14 15 mB2 = new QPushButton(QStringLiteral("运行时调用TestInvokemethhodReturn"), this); 16 mB2->move(mB1->x(), mB1->y() + mB1->height() + 10); 17 mB2->resize(250, 25); 18 connect(mB2, &QPushButton::clicked, this, &TTestWidget::on_B2); 19 20 mB3 = new QPushButton(QStringLiteral("运行时调用TestInvokemethhodObject"), this); 21 mB3->move(mB2->x(), mB2->y() + mB2->height() + 10); 22 mB3->resize(250, 25); 23 connect(mB3, &QPushButton::clicked, this, &TTestWidget::on_B3); 24 25 mB4 = new QPushButton(QStringLiteral("运行时调用setInvokemethhod"), this); 26 mB4->move(mB3->x(), mB3->y() + mB3->height() + 30); 27 mB4->resize(250, 25); 28 connect(mB4, &QPushButton::clicked, this, &TTestWidget::on_B4); 29 30 mB5 = new QPushButton(QStringLiteral("运行时调用setInvokemethhodObject"), this); 31 mB5->move(mB4->x(), mB4->y() + mB4->height() + 10); 32 mB5->resize(250, 25); 33 connect(mB5, &QPushButton::clicked, this, &TTestWidget::on_B5); 34 35 mB6 = new QPushButton(QStringLiteral("运行时调用setInvokemethhodInAndOut"), this); 36 mB6->move(mB5->x(), mB5->y() + mB5->height() + 10); 37 mB6->resize(250, 25); 38 connect(mB6, &QPushButton::clicked, this, &TTestWidget::on_B6); 39 } 40 41 void TTestWidget::on_B1() 42 { 43 QWidget *oTest = parent()->findChild<QWidget*>("TTestInvokemethhodWidget"); 44 if (nullptr != oTest) 45 { 46 QMetaObject::invokeMethod(oTest, "TestInvokemethhod"); // Qt::DirectConnection 47 } 48 } 49 50 void TTestWidget::on_B2() 51 { 52 bool bReturn = false; 53 QString s = "TTestWidget::bReturn:%1"; 54 QWidget *oTest = parent()->findChild<QWidget*>("TTestInvokemethhodWidget"); 55 if (nullptr != oTest) 56 { 57 QMetaObject::invokeMethod(oTest, "TestInvokemethhodReturn", Qt::DirectConnection, Q_RETURN_ARG(bool, bReturn)); 58 } 59 do_Text(s.arg(bReturn)); 60 } 61 62 void TTestWidget::on_B3() 63 { 64 QLabel* oLabel; 65 QString s = "TTestWidget::oLabel:%1"; 66 QWidget *oTest = parent()->findChild<QWidget*>("TTestInvokemethhodWidget"); 67 if (nullptr != oTest) 68 { 69 QMetaObject::invokeMethod(oTest, "TestInvokemethhodObject", Qt::DirectConnection, Q_RETURN_ARG(QLabel*, oLabel)); 70 } 71 emit do_Text(s.arg(oLabel->text())); 72 } 73 74 void TTestWidget::on_B4() 75 { 76 QWidget *oTest = parent()->findChild<QWidget*>("TTestInvokemethhodWidget"); 77 if (nullptr != oTest) 78 { 79 QMetaObject::invokeMethod(oTest, "setInvokemethhod", Qt::DirectConnection, Q_ARG(int, 8)); 80 } 81 } 82 83 void TTestWidget::on_B5() 84 { 85 QWidget *oTest = parent()->findChild<QWidget*>("TTestInvokemethhodWidget"); 86 if (nullptr != oTest) 87 { 88 QMetaObject::invokeMethod(oTest, "setInvokemethhodObject", Qt::DirectConnection, Q_ARG(QLabel*, mLabel)); 89 } 90 } 91 92 void TTestWidget::on_B6() 93 { 94 int n = 0; 95 QString s = "TTestWidget::AndOut:%1"; 96 QWidget *oTest = parent()->findChild<QWidget*>("TTestInvokemethhodWidget"); 97 if (nullptr != oTest) 98 { 99 QMetaObject::invokeMethod(oTest, "setInvokemethhodInAndOut", Qt::DirectConnection, Q_RETURN_ARG(int, n), Q_ARG(int, 3)); 100 } 101 emit do_Text(s.arg(n)); 102 }
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我
浙公网安备 33010602011771号