Qt元对象QMetaObject的indexOfSlot等函数获取类方法注意问题
生活随笔
收集整理的這篇文章主要介紹了
Qt元对象QMetaObject的indexOfSlot等函数获取类方法注意问题
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- indexOfSlot返回從QObject派生類中槽函數的索引,如果存在指定的槽函數,則返回非-1的值,如果不存在,則返回-1。
-
indexOfMethod返回從QObject派生類任意一個函數的索引,如果存在指定的函數,則返回非-1的值,如果不存在,則返回-1。
-
indexOfSignal返回從QObject派生類任意一個信號的索引,如果存在指定的信號,則返回非-1的值,如果不存在,則返回-1。
如下為QtWidgetsApplication2.h:
#pragma once#include <QtWidgets/QWidget> #include "ui_QtWidgetsApplication2.h" #include"test.h" class QtWidgetsApplication2 : public QWidget {Q_OBJECT signals:void setValueSignal(int);void testSignal(int&); public slots:int setMyAge(int&);int setValue(int nValue);int setValue(int* pValue);void setColor(const QColor& clr);void setTest(const CTest& test); public:QtWidgetsApplication2(QWidget *parent = Q_NULLPTR); private:Ui::QtWidgetsApplication2Class ui;};QtWidgetsApplication2.cpp文件代碼略。如下為test.h
#pragma once class CTest { public:CTest() {};~CTest() {}; };如下為main:
#include "QtWidgetsApplication2.h" #include <QtWidgets/QApplication> int main(int argc, char *argv[]) {QApplication a(argc, argv);QtWidgetsApplication2 w;auto nIndex1 = w.metaObject()->indexOfSlot("setValue(int)");auto nIndex2 = w.metaObject()->indexOfSlot("setMyAge(int&)");auto nIndex3 = w.metaObject()->indexOfSlot("setValue(int*)");auto nIndex4 = w.metaObject()->indexOfSlot("setColor(QColor)");auto nIndex5 = w.metaObject()->indexOfSlot("setTest(CTest)");qDebug() << "nIndex1:" << nIndex1 << "nIndex2:" << nIndex2<< "nIndex3:" << nIndex3 << "nIndex4:" << nIndex4<< "nIndex5:" << nIndex5 << "\r\n";auto nIndex6 = w.metaObject()->indexOfSlot("int setValue(int)");auto nIndex7 = w.metaObject()->indexOfSlot(" setValue(int)");auto nIndex8 = w.metaObject()->indexOfSlot("setValue(int )");auto nIndex9 = w.metaObject()->indexOfSlot("setValue(int value)");qDebug() << "nIndex6:" << nIndex6 << "nIndex7:" << nIndex7<< "nIndex8:" << nIndex8 << "nIndex9:" << nIndex9 << "\r\n";auto nIndex10 = w.metaObject()->indexOfSlot("setColor(const QColor)");auto nIndex11 = w.metaObject()->indexOfSlot("setColor(QColor&)");auto nIndex12 = w.metaObject()->indexOfSlot("setColor(const QColor&)");auto nIndex13 = w.metaObject()->indexOfMethod("setValue(int)");auto nIndex14 = w.metaObject()->indexOfMethod("dansir(int)");qDebug() << "nIndex10:" << nIndex10 << "nIndex11:" << nIndex11<< "nIndex12:" << nIndex12 << "nIndex13:" << nIndex13<< "nIndex14:" << nIndex14 << "\r\n";auto nIndex15 = w.metaObject()->indexOfSignal("setValueSignal(int)");auto nIndex16 = w.metaObject()->indexOfSignal("setValueSignal(int )");auto nIndex17 = w.metaObject()->indexOfSignal("testSignal(int&)");qDebug() << "nIndex15:" << nIndex15 << "nIndex16:" << nIndex16<< "nIndex17:" << nIndex17 << "\r\n";w.show();return a.exec(); }在我本機輸入結果如下:
從上面的輸入結果,總結如下:
- 這幾個函數的參數傳入的是要搜索的函數或信號,不包括函數返回值或信號的返回值,否則會返回-1,如:第18行所示。
- 這幾個函數的參數傳入的是要搜索的函數或信號,且函數名和和雙引號之間不能有空格,否則會返回-1,如:第19行所示。
- 這幾個函數的參數傳入的是要搜索的函數或信號,如果有參數類型,則參數類型和括號之間不能有空格,如:第20行所示。
- 這幾個函數的參數傳入的是要搜索的函數或信號, 可以帶參數類型,但不能帶具體的參數名,如:第21行所示。
- 這幾個函數的參數傳入的是要搜索的函數或信號,如果是參數是引用且參數類型不是int、double之類的基本數據類型,則要將引用符號去掉,如:setColor(QColor&)會返回-1,需要將setColor(QColor&)改為setColor(QColor),如果參數有const,則也會返回-1,需要將const去掉,即setColor(const QColor&)應改為setColor(QColor)。
綜上:這三個indexOf開頭的函數的參數為:
- 要搜索的函數或信號名。
- 函數或信號名不能帶返回值。
- 函數或信號名如果有參數,只能帶參數類型,不能帶具體的參數名。
- 函數或信號名如果有參數,參數類型和大括號之間不能有空格。
- 函數或信號名和雙引號之間不能有空格。
- 非基本數據類型的const或引用要去掉。
總結
以上是生活随笔為你收集整理的Qt元对象QMetaObject的indexOfSlot等函数获取类方法注意问题的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 不涨了 3月3日汽、柴油价格不作调整:2
- 下一篇: 无理打压!美商务部再对20多个中国实体实