Results 1 to 6 of 6

Thread: QSignalMapper question: SIGNAL 2 int's

  1. #1
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default QSignalMapper question: SIGNAL 2 int's

    Is it possible to use QSignalMapper to send 2 int's from a QAction? Here's my code:
    Qt Code:
    1. QPushButton *pButton = new QPushButton;
    2. QSignalMapper *mapper = new QSignalMapper(pButton2);
    3. mapper->setMapping(Action1, 3);
    4. connect(Action1, SIGNAL(triggered()), mapper, SLOT(map()));
    5. connect(mapper, SIGNAL(mapped(int)), coinWidget, SLOT(cTexture(int)));
    To copy to clipboard, switch view to plain text mode 

    I'd like to send two or more int's to cTexture(), but apparently the mapped() signal can only take one parameter.

  2. #2
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper question: SIGNAL 2 int's

    i thought of a way around it: my int values are between 0-999. If the two ints I want to send are 7 and 3, for example, i could send a string, 007003, and parse/convert to ints in my cTexture() function. I suspect there's a much better way, however.

  3. #3
    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: QSignalMapper question: SIGNAL 2 int's

    I think it might prove faster to have a controller slot that checks which action was triggered and emits another signal with arguments that depend on the action. It's exactly the same what the signal mapper does, only that it's better fit to your application.

  4. #4
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper question: SIGNAL 2 int's

    Quote Originally Posted by wysota View Post
    I think it might prove faster to have a controller slot that checks which action was triggered and emits another signal with arguments that depend on the action. It's exactly the same what the signal mapper does, only that it's better fit to your application.
    gotcha, thanks. I just set it up and it works.

    Qt Code:
    1. pB_terrain = new QPushButton(tr("&Terrain"));
    2. pB_terrain->setCheckable(true);
    3. pB_terrain>setChecked(true);
    4. connect(pB_terrain, SIGNAL(clicked()), this, SLOT(terrain_sl()));
    5. connect(this, SIGNAL(terrain_sig(int, int)), coinWidget, SLOT(terrain_sl(int, int)));
    6. .
    7. .
    8. .
    9.  
    10. void Window::terrain_sl()
    11. {
    12. QPushButton *action = (QPushButton *)sender();
    13. emit terrain_sig(7,3);
    14. }
    To copy to clipboard, switch view to plain text mode 

    my question is: will I need a separate slot function like terrain_sl() for each of the QPushButtons (and QActions), or is there a way that I can evaluate which button/Action sent the signal, and call the appropriate emit function depending on that.

  5. #5
    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: QSignalMapper question: SIGNAL 2 int's

    I think the code you pasted (especially line #12) already answers that question...

  6. #6
    Join Date
    Apr 2007
    Location
    Rakovnik, Czech Republic
    Posts
    175
    Thanks
    43
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSignalMapper question: SIGNAL 2 int's

    Quote Originally Posted by wysota View Post
    I think the code you pasted (especially line #12) already answers that question...
    ok. I see sender() "returns a pointer to the object that sent the signal"... This is what I did (with QSignalMapper objects instead of QPushButtons...):

    Qt Code:
    1. //
    2. lyImgAcMapper[0] = new QSignalMapper(button1);
    3. lyImgAcMapper[1] = new QSignalMapper(button2);
    4. lyImgAcMapper[2] = new QSignalMapper(button3);
    5.  
    6. // i set their mapping/etc. then connect them to a custom SLOT
    7.  
    8. connect(lyImgAcMapper[0], SIGNAL(mapped(int)), this, SLOT(lyImgSL(int)));
    9. connect(lyImgAcMapper[1], SIGNAL(mapped(int)), this, SLOT(lyImgSL(int)));
    10. connect(lyImgAcMapper[2], SIGNAL(mapped(int)), this, SLOT(lyImgSL(int)));
    11.  
    12. // the lyImgSL(int) slot looks like this
    13.  
    14. void Window::lyImgSL(int value)
    15. {
    16. QSignalMapper *sigmap = qobject_cast<QSignalMapper *>(sender()); //from Rajesh's post on another thread
    17. if(sigmap==lyImgAcMapper[0])
    18. {
    19. emit lyImgSG(0,value);
    20. }
    21. else if(sigmap==lyImgAcMapper[1])
    22. {
    23. emit lyImgSG(1,value);
    24. }
    25. else
    26. {
    27. emit lyImgSG(2,value);
    28. }
    29. }
    30.  
    31. // lyImgSG is then connected to another slot
    To copy to clipboard, switch view to plain text mode 

    ...um, this is working for me so far, although the truth is I only have one lyImgAcMapper[] array at the moment. Does this seem proper/efficient?
    Last edited by vonCZ; 20th July 2007 at 11:52. Reason: accidentally posted before trying it; it works

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.