Results 1 to 6 of 6

Thread: QSignalMapper

  1. #1
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default QSignalMapper

    Hello,
    I'm trying to make a menu with items that are checkable and send them to one slot:
    This seemed to work but crashes the dialog when it's closed.
    Qt Code:
    1. Diagram::Diagram(QWidget *parent) :
    2. QDialog(parent)
    3. {
    4. menuBar = new QMenuBar(this);
    5. planetsMenu = menuBar->addMenu("Planets");
    6. QSignalMapper *mapper = new QSignalMapper(this);
    7. for (int i = 0; i < 16; i++)
    8. {
    9. planet1Act[i] = new QAction(planetname[i], this);
    10. planet1Act[i]->setCheckable(true);
    11. mapper->setMapping(planet1Act[i], i);
    12. connect(planet1Act[i], SIGNAL(toggled(bool)), mapper, SLOT(map()));
    13. planetsMenu->addAction(planet1Act[i]);
    14. }
    15. connect(mapper, SIGNAL(mapped(int)), this, SLOT(toggleplanet(int)));
    16. }
    17. //---------------------------------------------
    18. void Diagram::toggleplanet(int n)
    19. {
    20. QDebug()<< planet1Act[n]->isChecked()<<n;
    21. }
    22. //---------------------------------------------
    To copy to clipboard, switch view to plain text mode 

    This works but crashes afterwards, what am I doing wrong?

    If I don't use an array for the actions:
    Qt Code:
    1. for (int i = 0; i < 16; i++)
    2. {
    3. planet1Act = new QAction(planetname[i], this);
    4. planet1Act->setCheckable(true);
    5. mapper->setMapping(planet1Act, i);
    6. connect(planet1Act, SIGNAL(toggled(bool)), mapper, SLOT(map()));
    7. planetsMenu->addAction(planet1Act);
    8. }
    9. connect(mapper, SIGNAL(mapped(int)), this, SLOT(toggleplanet(int)));
    10. }
    11. //---------------------------------------------
    12. void Diagram::toggleplanet(int n)
    13. {
    14. // how to get to the checked state of qaction number n ??
    15. }
    To copy to clipboard, switch view to plain text mode 

    This works fine but then I don't know how I can see if the action is checked or not in toggleplanet() since the slot can only have one int or object etc. I don't know how to get to the corresponding action from that one int.

    If it were checkboxes or something, I could get to them in toggelplanet() but how do I get to the actions?

    Thanks for helping.
    Last edited by Cremers; 24th July 2013 at 13:37.

  2. #2
    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: QSignalMapper

    Do you get any indication on the crash of your first code?

    Anyway, one other option you have is to connect all actions to one slot directly and use QObject::sender() in the slot to get at the action which emitted the signal.

    Cheers,
    _

  3. #3
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QSignalMapper

    Quote Originally Posted by anda_skoa View Post
    Do you get any indication on the crash of your first code?
    None.
    Anyway, one other option you have is to connect all actions to one slot directly and use QObject::sender() in the slot to get at the action which emitted the signal.
    I have a workaround, but surely there must be a better way.
    Qt Code:
    1. for (int i = 0; i < PLANETS; i++)
    2. {
    3. planet1Act = new QAction(planetname[i], this);
    4. planet1Act->setCheckable(true);
    5. planet1Act->setWhatsThis(QString("%1").arg(i)); // put the number of the action into this qstring
    6. .. etc
    7.  
    8. void Diagram::toggleplanet(QObject *w)
    9. {
    10. QAction *action = qobject_cast<QAction*>(w);
    11. QDebug()<<action->whatsThis()<<action->isChecked();
    12. }
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    May 2012
    Posts
    57
    Thanks
    11
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: QSignalMapper

    Quote Originally Posted by Cremers View Post
    Qt Code:
    1. void Diagram::toggleplanet(int n)
    2. {
    3. // how to get to the checked state of qaction number n ??
    4. }
    To copy to clipboard, switch view to plain text mode 
    Solution is QMenu::actions().at(n)->isChecked();

  5. #5
    Join Date
    Oct 2009
    Posts
    483
    Thanked 97 Times in 94 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QSignalMapper

    Quote Originally Posted by Cremers View Post
    None.


    I have a workaround, but surely there must be a better way.
    Qt Code:
    1. for (int i = 0; i < PLANETS; i++)
    2. {
    3. planet1Act = new QAction(planetname[i], this);
    4. planet1Act->setCheckable(true);
    5. planet1Act->setWhatsThis(QString("%1").arg(i)); // put the number of the action into this qstring
    6. .. etc
    7.  
    8. void Diagram::toggleplanet(QObject *w)
    9. {
    10. QAction *action = qobject_cast<QAction*>(w);
    11. QDebug()<<action->whatsThis()<<action->isChecked();
    12. }
    To copy to clipboard, switch view to plain text mode 
    Please don't do that. There are better ways. You could subclass QAction to add an integer field that carries the information you need, but your original solution using QSignalMapper is quite elegant. We cannot help you more without seeing your whole program. What is the lifetime of the planet1Act array? Does the QDebug() give the expected output?

  6. #6
    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: QSignalMapper

    Quote Originally Posted by yeye_olive View Post
    You could subclass QAction to add an integer field that carries the information you need
    QAction has setData() for that

    Cheers,
    _

Similar Threads

  1. QSignalMapper
    By Ali Reza in forum Newbie
    Replies: 35
    Last Post: 30th November 2012, 09:12
  2. QSignalMapper question
    By Arend in forum Newbie
    Replies: 4
    Last Post: 26th November 2012, 13:43
  3. qSignalMapper gets no output
    By saman_artorious in forum Qt Programming
    Replies: 10
    Last Post: 31st July 2012, 09:00
  4. QSignalMapper
    By axisdj in forum Newbie
    Replies: 6
    Last Post: 16th September 2010, 01:52
  5. ? about QSignalMapper
    By JimDaniel in forum Qt Programming
    Replies: 1
    Last Post: 13th January 2008, 21:21

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.