PDA

View Full Version : QObject signal/slot not working



Msnforum
24th January 2009, 20:43
I have a class A that inherits QWidget and class B that inherits QObject.

When a button is clicked, class A creates an object of Class B. Class B is supposed to perform a http request and emit a done signal to perform the slot function.

The problem is that class B can initialize properly but it cannot execute the slot function when the done signal is emitted. As far as I know, I don't see debug errors and my actual code is a bit too long.

Example:


class A : public QWidget
{
Q_OBJECT
public:
A();
private slots;
void buttonClicked();
private:
QPushButton *button;
};


class B : public QObject
{
Q_OBJECT
public:
B();
private slots;
void performFunction();
};


A::A()
{
...
connect(button,SIGNAL(clicked()),this,SLOT(buttonC licked()));

void A::buttonClicked()
{
B objectB;
}


B::B()
{
...
connect(http,SIGNAL(done(bool)),this,SLOT(performF unction()));
}

Could anyone point out what I have done wrong.

boudie
24th January 2009, 21:09
Wild guess...
Does the http object already exist when this is executed:
connect(http,SIGNAL(done(bool)),this,SLOT(performF unction()));

Msnforum
24th January 2009, 21:13
Yes, I have created the object.


QHttp *http = new QHttp(this);

Oddly enough, when I try to create a class B object manually on the main function, the performFunction slot does execute normally.

jpn
24th January 2009, 22:00
void A::buttonClicked()
{
B objectB;
}


Notice that "objectB" goes out of scope according to normal C++ rules and gets destructed immediately after its constructor has been executed.

Msnforum
24th January 2009, 22:50
Thanks, got that out of the way and the function does execute now.


B *objectB;
objectB = new B;



I've got another question, how would i proceed if i want to change the text of the button in class A.

If I inherit QObject and class A to access the button, it says QObject is already a base class of class A.


Edit: Compile just fine after removing QObject from inheritance leaving behind single inheritance to class A. The QPushButton text doesn't refresh though. Am I doing it right?