0
votes

OK, I'm a total beginner but I'm missing something here. Been all over the Qt documentation/examples and everything I can dig up through Google. All similar information is slightly different in context...

I'm just starting out with Qt SIGNALS and SLOTS, I'm successful with gui examples and within a class. Now I want to connect a SIGNAL in a child class with a SLOT in a sibling class with the Connect defined in the parent main. Ultimately my aim is to receive iamges in a class handling a QTcpSocket and emit the data as char* to be handled (saved or displayed) by another class.

For now I've just created the most basic version of the arrangement in a Console app as a learning exercise. I've got a sender class...

sender.h

#ifndef SENDER_H
#define SENDER_H

#include <QObject>

class sender : public QObject
{
    Q_OBJECT

public:
    sender(QObject *parent = 0);
    ~sender();
signals:
    void output(int data);
public slots:
    void test(int data);
private:    
};

#endif // SENDER_H

sender.cpp

#include <iostream>
#include "sender.h"

sender::sender(QObject *parent)
    : QObject(parent)
{
    std::cout << "Created sender" << std::endl;
    int stuff = 47;
    std::cout << "stuff = " << stuff << std::endl;

    connect(this, SIGNAL(output(int)), this, SLOT(test(int)));

    emit output(stuff);
}

void sender::test(int data)
{
    std::cout << "Got to test, data = " << data << std::endl;
}

sender::~sender()
{
    std::cout << "Destroying sender" << std::endl;
}

...and a receiver class...

receiver.h

#ifndef RECEIVER_H
#define RECEIVER_H

#include <QObject>

class receiver : public QObject
{
    Q_OBJECT

public:
    receiver(QObject *parent = 0);
    ~receiver();
public slots:
    void input (int data);
private:
};

#endif // RECEIVER_H

receiver.cpp

#include <iostream>
#include "receiver.h"

receiver::receiver(QObject *parent)
    : QObject(parent)
{
    std::cout << "Created receiver" << std::endl;
}

void receiver::input(int data)
{
    std::cout << "Got data as = " << data << std::endl;
}

receiver::~receiver()
{
    std::cout << "Destroying receiver" << std::endl;
}

My main looks like this...

main.cpp

#include <QtCore/QCoreApplication>
#include <iostream>
#include "sender.h"
#include "receiver.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    receiver myReceiver;
    sender mySender;

    if (QObject::connect(&mySender, SIGNAL(output(int)), 
        &myReceiver, SLOT(input(int))))
    {
        std::cout << "Got here so connect returned true" << std::endl;
    }

    return a.exec();
}

I've added the cout outputs and the sender::test function to try and figure out what's happening.

For me, this compiles cleanly and runs without any warnings or errors but while the sender::test SLOT gets called the receiver::input SLOT doesn't. The test on the connect in main returns true and neither sender or receiver are destroyed prematurely. Console output is...

Created receiver
Created sender
stuff = 47
Got to test, data = 47
Got here so connect returned true

So the SIGNAL is emitted, the SIGNAL and SLOT parameters match, I've got the Q_OBJECT macros in both sender.h and receiver.h, and both inherit from and #include QObject.

What's wrong???

P.S. I'm running 4.8.3 and IDE is VS2010 with Qt plugin.

2

2 Answers

0
votes

The answer is quite simple: you are sending signal before you've connected it to the receiver and after you've connected it to itself. So your output is absolutely correct.

0
votes

Do all emits only after everything has been connected. Here the instantiation of sender happens before sender and receiver are connected, and that's where the emit was done.