【发布时间】:2013-09-27 19:04:18
【问题描述】:
我搜索了许多帮助讨论并阅读了多个教程,但我仍然不了解 Qt 信号和插槽的正确语法,使用 Qt Core 5.0 我创建了一个非常简单的程序,其中包含两个对象来尝试理解这个语法。 (如下所示)。每次我编译这段代码时,我都会收到以下错误:
“int”之前的预期主表达式
请帮我回答以下问题:
我写的代码有什么问题?
Qt 连接函数是否需要指向对象引用 (&mySig) 而不是直接指向对象的指针?
-
当我在连接函数中使用包含参数的槽和信号时,我是否需要为这些参数提供变量,或者只说明数据类型,如下面的代码所示?
李> 最后,我想使用槽和信号在我正在编写的程序中的对象之间传递数据。插槽和信号是否允许我传递从 QObject 派生的其他对象?还是我需要做一些额外的事情?
-
我看到很多对连接语句格式的引用
QObject::contect(&mySig, SIGNAL(sig_1(int)), &mySlot, SLOT(slot1(int)));
这种格式在 Qt 5.0 Core 下仍然有效吗?
非常感谢您的帮助!简单程序代码如下。
#include <QCoreApplication>
#include <QObject>
#include <iostream>
using namespace std;
//================================================================================
class testSig : public QObject
{
Q_OBJECT
public:
explicit testSig(QObject *parent = 0) :
QObject(parent)
{
}
void getNum()
{
int t;
cout << endl << endl << "Please Enter Number: ";
cin >> t;
emit sig_1(t);
}
signals:
void sig_1(int i );
};
//================================================================================
class testSlot : public QObject
{
Q_OBJECT
public:
explicit testSlot(QObject *parent = 0) :
QObject(parent)
{
}
public slots:
void slot1(int i)
{
cout << "New Value is: " << i << endl;
}
};
//=================================================================================
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
testSig mySig;
testSlot mySlot;
QObject::connect(&mySig, testSig::sig_1(int), &mySlot, testSlot::slot1(int));
for( ; ; )
{
mySig.getNum();
}
return a.exec();
}
【问题讨论】:
-
抱歉在发帖后删除了我的答案,但没有看到这是 qt5。连接应该被称为
QObject::connect(&mySig, &testSig::sig_1, &b, &testSlot::slot1);
标签: c++ qt syntax signals slot