I have searched numerous help discussions and read multiple tutorials, but I still don't understand the correct syntax for Qt signals and slots, using Qt Core 5.0 I created a very simple program with two objects to try and understand this Syntax. (Shown below). Every time I compile this code, I get the following error:
expected primary-expression before 'int'
Please help me with the following answers:
What is the problem with the code I wrote?
Does the Qt connect function expect pointers for the object references (&mySig) instead of the objects directly?
When I use slots and signals that include parameters, in the connection function, do I need to supply variables for those parameters, or merely state the data type as shown in my code below?
Eventually, I want to use slots and signals to pass data between objects in a program I'm writing. Do slots and signals allow me to pass other objects, which are derived from QObject? Or do I need to do something extra?
I see many references to a format of the connect statement which uses
QObject::contect(&mySig, SIGNAL(sig_1(int)), &mySlot, SLOT(slot1(int)));
Is this format still valid under the Qt 5.0 Core?
Many thanks for all the help! Code of simple program follows below.
#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();
}
QObject::connect(&mySig, &testSig::sig_1, &b, &testSlot::slot1);
– jodag