不带SLOT宏的Qt连接
Qt connect without SLOT macro
假设我有一个包含"function()"的字符串,其中function()是类中的一个插槽,我想用任何信号连接该插槽,但要使用该字符串。两个
QString f="function()";
connect (randomobject, SIGNAL(randomsignal()), this, SLOT(f));
output: just says that slot f doesn't exist.
或
QString f="SLOT(function())";
//conversion to const char*
connect (randomobject, SIGNAL(randomsignal()), this, f);
output: Use the SLOT or SIGNAL macro to connect
工作。
有没有类似的方法?重要的是它是一个字符串而不是函数指针。
您可以在qobjectdefs.h:中查看SLOT的定义
#ifndef QT_NO_DEBUG
# define SLOT(a) qFlagLocation("1"#a QLOCATION)
# define SIGNAL(a) qFlagLocation("2"#a QLOCATION)
#else
# define SLOT(a) "1"#a
# define SIGNAL(a) "2"#a
#endif
这意味着SLOT("func()")在预处理后简单地转换为"1func(()"。所以你可以写:
Test *t = new Test; // 'Test' class has slot void func()
QString str = "func()";
QPushButton *b = new QPushButton("pressme");
QObject::connect(b, SIGNAL(clicked()), t, QString("1" + str).toLatin1()); // toLatin1 converts QString to QByteArray
当您显示并按下按钮时,将调用Test中的slot func()。
请注意,"connect"将"const char*"作为第二个和第四个参数类型,因此必须将QString转换为"const char*"或"QByteArray"(将转换为char指针)。
相关文章:
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 如何在qt中将信号和插槽与另一个对象连接 --解决了
- Qt将信号与另一个类方法连接
- 如何在Qt中连接来自不同窗口的两个对象?
- 在 ubuntu 18.04.3 LTS 上安装 Qt 和 qwt:如何连接它们?
- Qt:如何通知对象已建立涉及它的信号槽连接
- QT 连接没有要调用的匹配功能
- Qt如何将行编辑连接到进度条
- Qt中的包装连接方法隐藏了编译器所需的信息
- 连接QT中的多个.UI文件
- QObject::连接:QT上没有这样的插槽
- 无法使用连接 qt 中的插槽
- 无法连接Qt QObject::connect()函数
- 连接qt和matlab时出错
- 按钮连接Qt失败
- 重复的数据库连接qt
- 在派生的QObject构造函数中连接Qt信号和槽
- 连接Qt GUI与TCL应用程序
- 如何连接QT与VLC播放器
- 断开并稍后重新连接Qt信号