Results 1 to 3 of 3

Thread: Is it possible to pass parameters to a slot that were not emitted by a signal?

  1. #1
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Is it possible to pass parameters to a slot that were not emitted by a signal?

    Hi. I have numerous checkboxes that are each linked with a respective Spinbox. What I am trying to accomplish, with minimal code, is to append a suffix to the spinbox when the checkbox is checked. I have 8 sets of checkboxes/spinboxes and I am trying to make them utilize a single slot to accomplish my needs. I have tried the following by it seems a slot will not take a parameter unless it is emited by the accompanying signal. I was told briefly that QSignalMapper may be able to fit my needs but I am unsure. Any guidance would be appreciated! Thanks.

    Connection:
    Qt Code:
    1. QCheckBox* check_[8];
    2. QDoubleSpinBox* grade_SpinBox_[8];
    3.  
    4.  
    5. for (int i = 1; i <= 8; i++)
    6. {
    7. connect(checkbox_[i], SIGNAL(toggled(bool)), this, SLOT(append_suffix(bool, [i])));
    8. }
    To copy to clipboard, switch view to plain text mode 

    Slot:
    Qt Code:
    1. void MainWindow::append_suffix(bool clicked, int checkboxnum)
    2. {
    3. if (clicked == false)
    4. {
    5. grade_SpinBox_[checkboxnum]->setSuffix(QApplication::translate("MainWindow", "%", 0, QApplication::UnicodeUTF8));
    6. }
    7. else
    8. {
    9. ass_grade_SpinBox_[checkboxnum]->setSuffix(QApplication::translate("MainWindow", "", 0, QApplication::UnicodeUTF8));
    10. }
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    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: Is it possible to pass parameters to a slot that were not emitted by a signal?

    Yes, you could use QSignalMapper like this:
    Qt Code:
    1. // QCheckBox* check_[8];
    2.  
    3. {
    4. QSignalMapper* mapper = new QSignalMapper(this);
    5. for (int i = 0; i < 8; ++i)
    6. {
    7. mapper->setMapping(check_[i], i);
    8. connect(check_[i], SIGNAL(toggled(bool)), mapper, SLOT(map()));
    9. }
    10. connect(mapper, SIGNAL(mapped(int)), this, SLOT(append_suffix(int)));
    11. }
    12.  
    13. void append_suffix(int i)
    14. {
    15. if (check_[i]->isChecked())
    16. {
    17. // ...
    18. }
    19. else
    20. {
    21. // ...
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

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

    kramed (16th August 2007)

  4. #3
    Join Date
    Aug 2007
    Location
    Ontario, Canada
    Posts
    22
    Thanks
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Is it possible to pass parameters to a slot that were not emitted by a signal?

    Man, jpn, you are great. Two days in a row you have helped me greatly. TT should hire you to help with the documentation, your examples are much easier to follow. Thank you!

Similar Threads

  1. signal slot conection using a string, not a SLOT
    By rianquinn in forum Qt Programming
    Replies: 6
    Last Post: 5th February 2006, 18:52

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.