2
votes

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:

  1. What is the problem with the code I wrote?

  2. Does the Qt connect function expect pointers for the object references (&mySig) instead of the objects directly?

  3. 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?

  4. 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?

  5. 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();
}
1
Sorry deleted my answer after posting without seeing that this was qt5. The connect should be called as QObject::connect(&mySig, &testSig::sig_1, &b, &testSlot::slot1);jodag

1 Answers

5
votes

1. What is the problem with the code I wrote?

This line:

QObject::connect(&mySig, testSig::sig_1(int), &mySlot, testSlot::slot1(int));

This is illegal C++. If you want to use the new signal/slot syntax, you need to take the pointers of the signals and slots functions:

QObject::connect(&mySig, &testSig::sig_1, &mySlot, &testSlot::slot1);

2. Does the Qt connect function expect pointers for the object references (&mySig) instead of the objects directly?

Yes, see the docs.

3. 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?

With the "old" syntax, yes. With the new syntax, no (see above: no arguments specified). But you can find yourself in trouble if your signal or slot have overloads.

4. 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?

Of course, you can pass any data type. Restrictions apply if you use queued connections, but let's leave that as an advanced topic.

5. 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?

Yes, absolutely. We do want old code to compile!