Results 1 to 10 of 10

Thread: Slot with a return value?

  1. #1
    Join Date
    Oct 2009
    Location
    Craiova, Romania
    Posts
    46
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Slot with a return value?

    Hello!

    May I make a slot with a return value, like:

    Qt Code:
    1. public slots:
    2. bool saveAs();
    3. ....
    4. connect(savePB, SIGNAL(clicked()), this, SLOT(saveAs());
    To copy to clipboard, switch view to plain text mode 

    Practically, it works, but is this documented in Qt or allowed?

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Slot with a return value?

    Since slots are normal C++ functions you can!

  3. #3
    Join Date
    Jan 2007
    Posts
    29
    Thanks
    3

    Default Re: Slot with a return value?

    The return value of a slot is only retrieved if you call it like a regular function.

  4. #4
    Join Date
    Oct 2009
    Location
    Craiova, Romania
    Posts
    46
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Slot with a return value?

    Thank you for your answers! I know that practically it is allowed. I even saw the moc_file.cpp code and made an idea of what this is like. But what I searched for was a Qt documentation about that. Which I searched also on the internet and in some books, but could not find it. I only found a sample program on the internet, which seemed to be from Qt officials, and it contained a non-void slot, and the author said is "non-standard" or something like that.

    It is very easy to skip this problem: make a void slot which calls the non-void function. I don't want to take any risks in my software, so that is why I did this trick.

  5. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Slot with a return value?

    But there is no risk. In Qt documentation it is said that (almost - I think method with variable parameters count - '...' can't) every member function can be a slot. Just place it in public/private/protected slots section in class declaration. So making void returning slots to call non-void returning methods is waste of one more function call, of course unless you don't need returned value.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  6. #6
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot with a return value?

    It's non-standard because a non-void slot will have it's return code ignored. If you wish to call that function normally too, then it makes sense. It's better than having a void slot calling a non-void slot when you are going to ignore the return value anyway - thats just a waste.

  7. #7
    Join Date
    Oct 2009
    Location
    Craiova, Romania
    Posts
    46
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Slot with a return value?

    Detailed explanation: I have the function bool fileSave(), which saves the file, and returns true or false (success or not). The user can press button "Save", which is connected to this fileSave. For this, I don't need the bool return value. But I need it for this: when the user wants to Quit my application, I show him a question: "Do you want to save current file". He says "Yes". Then, he may save or hit cancel in the Save file dialog. I need the return code of fileSave(), if it is false, I don't close my application (closeEvent->ignore()).

    Yes, you are right that there is an additional function call if I implement a slot void saveFileSlot(), which calls bool fileSave() and ignores the bool return. But remember the 10-90 rule (I mean, you need to only optimize critical code). With current speed, it really does not matter an additional function call for a button press.

    I searched the Qt sources and could not find a non-void slot.

    Yes, I know current code works. But there is also some other risk: this type of code (non-void slot) may not be tested thoroughly by Qt team.

  8. #8
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Slot with a return value?

    In the end, its upto you. It's your application.

    However, since we know how slots are called, there is really no risk to having a non-void slot.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Slot with a return value?

    I sometimes use slots that return values in my code. It's nothing special. As for non-void slots in Qt - I'm almost sure there are some. But they are called using QMetaObject::invokeMethod() because their value is interesting to the caller so it needs to be retrieved.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  10. #10
    Join Date
    Oct 2009
    Location
    Craiova, Romania
    Posts
    46
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Slot with a return value?

    OK, thank you, I'll think about it.

Similar Threads

  1. slot return type
    By adamatic in forum Qt Programming
    Replies: 13
    Last Post: 4th October 2014, 21:01
  2. error with QList with a class
    By john_god in forum Newbie
    Replies: 7
    Last Post: 12th January 2009, 21:48
  3. QTableView performances
    By miraks in forum Qt Programming
    Replies: 18
    Last Post: 1st December 2008, 10:25
  4. Once more : QAbstractItemModel woes
    By Valheru in forum Qt Programming
    Replies: 10
    Last Post: 15th January 2008, 10:44
  5. QTableView Repaint/Refresh
    By millsks in forum Newbie
    Replies: 9
    Last Post: 10th January 2008, 17:18

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.