PDA

View Full Version : Issues with connecting signal slot in case of multiple inheritance



dpatel
13th July 2010, 13:56
Hi,

I am trying to map signal slot using QSignalMapper. The object I pass to the signalmapper is publicly derived from both QPushButton and CTest (CTest is not derived from any other class). CTest has only one int variable.
so the declaration of my class will be

class CClassy : public QPushButton,public CTest
{
}

Now when I set the signal slot using signalmapper and pass the object of CClassy, it doesn't work. But if I pass an object of QPushButton, the same setup works fine.

Is it because of Multiple inheritance?

Thanks

Lykurg
13th July 2010, 14:08
Don't forget the Q_OBJECT macro in your class!

dpatel
13th July 2010, 14:10
I have Q_OBJECT macro in my class....

Lykurg
13th July 2010, 14:20
#include <QtGui>

class Base
{
public:
int i;
};

class MyButton : public QPushButton, public Base
{
Q_OBJECT
public:
int i;
};

int main(int argc, char* argv[])
{
QApplication app(argc, argv);

MyButton b;
b.setText("Click to close!");
QObject::connect(&b, SIGNAL(clicked()), &app, SLOT(quit()));
b.show();

return app.exec();
}

#include "main.moc"
works fine for me. Your error must be somewhere else.

Zlatomir
13th July 2010, 14:21
Didn't you forgot the Q_OBJECT macro: (my guess is that you did, because there is no other reason for that)


class CClassy : public QPushButton, public CTest
{
Q_OBJECT
//... rest of the class definition
}


EDIT: My answer was too late... sorry...

dpatel
13th July 2010, 14:35
Ya, this code works for me as well. So I guess the issue is with my code. Thanks for you time.

agathiyaa
13th July 2010, 14:47
Didn't the above example from Lykurg's work ?? Do you cast from one type to another in your code somewhere ???

dpatel
13th July 2010, 15:03
Ahh, I got the root cause of the issue. I was overriding mousepressevent() on my button class, but after doing my stuff, was not calling QPushButton::mousePressEvent(e). After adding that code, everything works fine.

Thanks everyone.

Lykurg
13th July 2010, 15:08
Ahh, I got the root cause of the issue. I was overriding mousepressevent() on my button class, but after doing my stuff, was not calling QPushButton::mousePressEvent(e).
:D I "love" that kind of errors, they drive you crazy and you search everywhere but can't find anything. With this kind of errors I wast houres week after week!