Results 1 to 9 of 9

Thread: create multiple buttons with signal and slots

  1. #1
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Talking create multiple buttons with signal and slots

    hi
    i want to show multiple pushbuttons in the form dynamically!!! that is the number of pushbuttons i am showing ,will vary at runtime
    .So i want to connect each button with signal and slot.i have created a function(i.e slot)
    which will be called when any of the button is clicked..
    Qt Code:
    1. for(int i=0;i<count;i++)
    2. {
    3. QPushButton *labelbed =new QPushButton(roomgroup);
    4. labelbed->setObjectName(bednumbersunderroom.at(i));
    5.  
    6. connect(labelbed,SIGNAL(clicked()),signalMapper,SLOT(map()));
    7. signalMapper->setMapping(labelbed,labelbed); connect(signalMapper,SIGNAL(mapped(QWidget*)),this,SLOT(onbedclicked(QWidget*)));
    8. }
    9. void Widget::onbedclicked(QWidget* label)
    10. {
    11.  
    12.  
    13. QMessageBox *msg=new QMessageBox(this);
    14. msg->setText(label->objectName());
    15. msg->show();
    16. }
    To copy to clipboard, switch view to plain text mode 
    when any push button is clicked it displays message box but it displays as many messageboxes as push buttons...
    for eg:if there are 3 push buttons,then if i click any one push button it displays 3 messagebox.But it should show only one messagebox..so how to do the signal slots
    so that it will show one messagebox for each click!!!

    thanks and regards
    any help is highly appreciated

  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: create multiple buttons with signal and slots

    Qt Code:
    1. connect(signalMapper,SIGNAL(mapped(QWidget*)),this,SLOT(onbedclicked(QWidget*)));
    To copy to clipboard, switch view to plain text mode 

    must be outside your loop! because then you conntect the signal mapper n-times.

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

    mohanakrishnan (14th November 2009)

  4. #3
    Join Date
    Oct 2009
    Location
    chennai,india
    Posts
    66
    Thanks
    18
    Qt products
    Qt4
    Platforms
    Windows

    Smile

    hi
    i ll check it and come back
    thanks for ur help


    hi Lykurg
    it works well thanks for ur help
    Last edited by wysota; 14th November 2009 at 09:11.

  5. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: create multiple buttons with signal and slots

    place that statement outside of your loop:
    Qt Code:
    1. connect(signalMapper,SIGNAL(mapped(QWidget*)),this,SLOT(onbedclicked(QWidget*)));
    To copy to clipboard, switch view to plain text mode 

  6. #5
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: create multiple buttons with signal and slots

    Lykurg has already indicated that in 3 post
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #6
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: create multiple buttons with signal and slots

    Of course he has; when I replied that answer was not there for me to see. (Otherwise I would not have posted anymore.)
    Happens now and then. Sorry (not my fault, though).

  8. #7
    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: create multiple buttons with signal and slots

    Quote Originally Posted by spirit View Post
    Lykurg has already indicated that in 3 post
    Wow, do you see my post 3times??? I'm here in the diaspora, in the middle of woods with a bad wlan...

  9. #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: create multiple buttons with signal and slots

    [ Retracted ]
    Last edited by squidge; 14th November 2009 at 12:42. Reason: retracted

  10. #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: create multiple buttons with signal and slots

    Guys, this is a thread that is a merge of two threads. Each of them replied in a different thread. Everything is ok (apart from the original author of the thread multi-threading).
    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.


Similar Threads

  1. Queuing problem with signal and slots
    By montylee in forum Qt Programming
    Replies: 4
    Last Post: 20th November 2009, 06:11
  2. filter signal to limited slots
    By sriky27 in forum Qt Programming
    Replies: 17
    Last Post: 26th March 2009, 16:59
  3. How to Implement signal and slots for QAccessibleWidget
    By Rakesh_Kumar in forum Qt Programming
    Replies: 1
    Last Post: 30th January 2009, 06:57
  4. Signal and slots
    By villy in forum Qt Programming
    Replies: 1
    Last Post: 12th January 2007, 10:10
  5. Problem with Signal and Slots
    By Kapil in forum Installation and Deployment
    Replies: 2
    Last Post: 10th February 2006, 08:51

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.