Results 1 to 5 of 5

Thread: QObject signal/slot not working

  1. #1
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default QObject signal/slot not working

    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:

    Qt Code:
    1. class A : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. A();
    6. private slots;
    7. void buttonClicked();
    8. private:
    9. QPushButton *button;
    10. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. class B : public QObject
    2. {
    3. Q_OBJECT
    4. public:
    5. B();
    6. private slots;
    7. void performFunction();
    8. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. A::A()
    2. {
    3. ...
    4. connect(button,SIGNAL(clicked()),this,SLOT(buttonClicked()));
    5.  
    6. void A::buttonClicked()
    7. {
    8. B objectB;
    9. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. B::B()
    2. {
    3. ...
    4. connect(http,SIGNAL(done(bool)),this,SLOT(performFunction()));
    5. }
    To copy to clipboard, switch view to plain text mode 

    Could anyone point out what I have done wrong.
    Last edited by Msnforum; 24th January 2009 at 22:02.

  2. #2
    Join Date
    Jan 2009
    Location
    The Netherlands and Spain
    Posts
    150
    Thanks
    6
    Thanked 18 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QObject signal/slot not working

    Wild guess...
    Does the http object already exist when this is executed:
    connect(http,SIGNAL(done(bool)),this,SLOT(performF unction()));

  3. #3
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject signal/slot not working

    Yes, I have created the object.

    Qt Code:
    1. QHttp *http = new QHttp(this);
    To copy to clipboard, switch view to plain text mode 

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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: QObject signal/slot not working

    Quote Originally Posted by Msnforum View Post
    Qt Code:
    1. void A::buttonClicked()
    2. {
    3. B objectB;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Notice that "objectB" goes out of scope according to normal C++ rules and gets destructed immediately after its constructor has been executed.
    J-P Nurmi

  5. The following user says thank you to jpn for this useful post:

    Msnforum (24th January 2009)

  6. #5
    Join Date
    Jan 2009
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QObject signal/slot not working

    Thanks, got that out of the way and the function does execute now.

    Qt Code:
    1. B *objectB;
    2. objectB = new B;
    To copy to clipboard, switch view to plain text mode 



    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?
    Last edited by Msnforum; 25th January 2009 at 01:40.

Similar Threads

  1. QObject and copy Constructors
    By December in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 17:14

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.