Results 1 to 7 of 7

Thread: Problem with signals and slots

  1. #1
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Problem with signals and slots

    Hi, I am writing a Blackberry app have this problem and would greatly appreciate if someone could help.
    The situation is as follows.

    I have code:


    Qt Code:
    1. void MYNetworkClass::requestFinished(QNetworkReply* reply)
    2. {
    3.  
    4. // Check the network reply for errors
    5. if (reply->error() == QNetworkReply::NoError)
    6. {
    7. // read response
    8. const QByteArray response(reply->readAll());
    9. qDebug() <<"Response: "<< response;
    10.  
    11. JsonDataAccess jda;
    12. QVariantMap results = jda.loadFromBuffer(response).toMap();
    13.  
    14. QVariantMap data = results.value("SomeData").toMap();
    15.  
    16. emit signalSuccess(data); // I reach here ....
    17.  
    18. // however actually when I click "Step Over" in debug mode, and try to exit the function
    19. // afterwards I get such "error" saying: No source available for "QMetaObject::activate() at 0xb9668da7" - highlighted in red.
    20.  
    21.  
    22.  
    23. }
    24. else
    25. {
    26. qDebug() << "\n Problem with the network";
    27. qDebug() << "\n" << reply->errorString();
    28. }
    To copy to clipboard, switch view to plain text mode 


    In MyNetwork class I have also added the signal definition in the header file:

    Qt Code:
    1. signals:
    2.  
    3. void signalSuccess(QVariantMap result);
    To copy to clipboard, switch view to plain text mode 

    Now, elsewhere when someone calls a method of MyNetworkClass object which invokes requestFinished,

    I want to catch the signal that is emitted by the requestFinished - as shown above.



    Qt Code:
    1. MyNetworkClass *network = new MyNetworkClass();
    2.  
    3. QMap<QString, QString> params;
    4. params.insert("username", userEmail);
    5.  
    6. bool res = QObject::connect(network, SIGNAL(signalSuccess(QVariantMap)), this, SLOT(SomeSlotForSignal(QVariantMap)));
    7.  
    8. Q_ASSERT(res);
    9. Q_UNUSED(res);
    10.  
    11. network->makePostRequest("Login");
    To copy to clipboard, switch view to plain text mode 

    My problem is that the slot SomeSlotForSignal never gets called.. ( even though the signal is emitted form the requestFinished method and also I have declared SomeSlotForSignal as a slot in the header file ... What can be problem??? Any help??? Thanks.


    Added after 18 minutes:


    I have solved the problem.

    The code I wrote above was contained in some method Say MyMethod of class B.
    Qt Code:
    1. void ClassB::MyMethod(int param1, int param 2)
    2. {
    3. MyNetworkClass *network = new MyNetworkClass();
    4.  
    5. QMap<QString, QString> params;
    6. params.insert("username", userEmail);
    7.  
    8. bool res = QObject::connect(network, SIGNAL(signalSuccess(QVariantMap)), this, SLOT(SomeSlotForSignal(QVariantMap)));
    9.  
    10. Q_ASSERT(res);
    11. Q_UNUSED(res);
    12.  
    13. network->makePostRequest("Login");
    To copy to clipboard, switch view to plain text mode 
    }
    and looked like this:
    Qt Code:
    1. ClassB *object = new ClassB();
    2. object->MyMethod(param1,param2);
    To copy to clipboard, switch view to plain text mode 
    before I was not using pointers and heap allocation, I just had:

    Qt Code:
    1. ClassB object;
    2. object.MyMethod(param1,param2);
    To copy to clipboard, switch view to plain text mode 
    and I think when the requestFinished returned, the above object was already destroyed. ?????
    I think/hope so.. Do you think it is correct reasoning???
    Last edited by ggdev001; 19th February 2013 at 12:08.

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem with signals and slots

    1. Are there any errors/warning in the Application output?
    2. What is MYNetworkClass derived from?
    3. Are you having the Q_OBJECT macro in the MYNetworkClass definition

    ...even though the signal is emitted form the requestFinished method ...
    How can you tell the signal is emitted? (I mean when the slot connected is not called)
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  3. #3
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Problem with signals and slots

    can you please look at my updated reply??.. Thanks.

    ps. I meant that the program was reaching that point in Debug, that is why I said the signal is emitted...

  4. #4
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem with signals and slots

    and I think when the requestFinished returned, the above object was already destroyed. ?????
    I think/hope so.. Do you think it is correct reasoning???
    This will the reason if "network->makePostRequest("Login");" does not emit the signal.
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  5. #5
    Join Date
    Feb 2013
    Posts
    50
    Thanks
    5

    Default Re: Problem with signals and slots

    Hi, no the problem was here:
    Qt Code:
    1. ClassB object;
    2. object.MyMethod(param1,param2);
    To copy to clipboard, switch view to plain text mode 
    This way it was not working, because as I said I think the object was destroyed from the stack when
    the server returned. Once I changed the above code to:
    Qt Code:
    1. ClassB *object = new ClassB();
    2. object->MyMethod(param1,param2);
    To copy to clipboard, switch view to plain text mode 

    It started to work.


    But I have another question now. What are the ways to free the memory I allocated above??
    If I call
    Qt Code:
    1. delete
    To copy to clipboard, switch view to plain text mode 
    before server returns, I will have same issues as before.
    How to avoid this issue? What is the best way to free the memory and where in the code?? Thank you.

  6. #6
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: Problem with signals and slots

    One way it to assign a parent (QObject) when you create the object, so that it will deleted when the parent is deleted.
    or
    you could write a slot for deletion
    or
    using deleteLater() slot
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

  7. #7
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem with signals and slots

    Quote Originally Posted by Santosh Reddy View Post
    using deleteLater() slot
    I would go for this one.
    Make sure there is a signal emitted for both success and error case and connect them to the object's deleteLater() slot.
    Or call deleteLater() in the slots connected to those signal(s).

    Cheers,
    _

Similar Threads

  1. Problem with Signals and Slots
    By Nayrb in forum Qt Programming
    Replies: 1
    Last Post: 17th April 2012, 08:45
  2. problem with signals/slots
    By vaas in forum Newbie
    Replies: 3
    Last Post: 2nd April 2010, 14:24
  3. Problem with signals & slots
    By Palmik in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2009, 22:45
  4. signals and slots problem
    By sonuani in forum Newbie
    Replies: 6
    Last Post: 26th November 2008, 17:00
  5. Signals And Slots problem
    By ldiamond in forum Newbie
    Replies: 7
    Last Post: 23rd March 2008, 00:11

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.