CoutPotato
CoutPotato

Reputation: 535

Passing a class through a signal/slot setup in Qt

I'm trying to get the information of several of a class' member variables on the receiving end of a slot/signal setup, so I'd like to pass the entire class through. Unfortunately, after the class has been passed, the member variables seem to be empty. Here's some code snippets:

This sets up the signal to pass the class

signals:
    void selected(const ControlIcon *controlIcon);

this is the slot/signal connection

connect(controllerList->serialController, SIGNAL(selected(const ControlIcon*)),
        infoView, SLOT(serialControllerSelected(const ControlIcon*)));

I emit the signal from within the class to be passed

emit selected(this);

Here's the code to call on the class' member data

QLabel *ASCIIStringHolder = new QLabel;
ASCIIStringHolder->setText(controlIcon->m_ASCIIString);

Nothing shows up in the label, and when I set a breakpoint, I can see that there's nothing inside m_ASCIIString.

I looked to make sure that it was being assigned some text in the first place, and that's not the problem. I also tried the signal/slot setup with and without const.

Any help would be appreciated.

Upvotes: 3

Views: 5634

Answers (3)

CoutPotato
CoutPotato

Reputation: 535

The signal can't be declared to be passing a class and then actually pass the child of that class. I changed the signal, slot, and connect() to be SerialController (the child of ControllerIcon), and everything worked fine.

Upvotes: -1

Sergei Tachenov
Sergei Tachenov

Reputation: 24869

First, since you are using an auto connection, do both sender and receiver live in the same thread? If not, it could happen that the call is queued and when it arrives, the data in the sender was already modified. You could try to use a direct connection just to make sure this isn't the problem.

Second, just for the fun of it, did you try to access the sender by using qobject_cast<ControlIcon*>(sender()) within the slot? This is how it is usually done if the signal doesn't pass this as an argument. Like this:

QLabel *ASCIIStringHolder = new QLabel;
// this is instead of the argument to the slot:
ControlIcon *controlIcon = qobject_cast<ControlIcon*>(sender());
ASCIIStringHolder->setText(controlIcon->m_ASCIIString); 

Upvotes: 0

Nekuromento
Nekuromento

Reputation: 2235

Qt signal/slot mechanism needs metainformation about your custom types, to be able to send them in emitted signals. To achieve that, register your type with qRegisterMetaType<MyDataType>("MyDataType");

Consult official QMetaType documentation for more information about this.

Upvotes: 6

Related Questions