- 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或引用要去掉。