Results 1 to 6 of 6

Thread: QSignalMapper

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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.