Qt 4 的connect()
函数与 Qt 5 最大的区别在于,Qt 4 的 signal 和 slot 只有const char *
这么一种形式。如果我们将上面的代码修改为 Qt 4 的,则应该是这样的:
// !!! Qt 4
#include <QApplication>
#include <QPushButton>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QPushButton button("Quit");
QObject::connect(&button, SIGNAL(clicked()),
&app, SLOT(quit()));
button.show();
return app.exec();
}
我们使用了SIGNAL
和SLOT
这两个宏,将两个函数名转换成了字符串。注意,即使quit()
是QApplication
的 static 函数,也必须传入一个对象指针。这也是 Qt 4 的信号槽语法的局限之处。另外,注意到connect()
函数的 signal 和 slot 都是接受字符串,因此,不能将全局函数或者 Lambda 表达式传入connect()
。