Results 1 to 14 of 14

Thread: Dynamically linking signals and slots for buttons

  1. #1
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Dynamically linking signals and slots for buttons

    I have created some dynamic buttons through XML in my code. I want to know how do i connect slots to this dynamically created buttons?

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically linking signals and slots for buttons

    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically linking signals and slots for buttons

    Thanks for the response.. i dint frame my question well.. i have some more queries on the same subject
    I have created 5 buttons from XML. and that is displayed on the screen. i want to know how to do the following things

    1. If i click on dynamically created button how can i identify which button i have clicked?
    2. How do i get the button name when i click on the button?
    3. How do i set an event for the button

    Any respone will be of great help

  4. #4
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically linking signals and slots for buttons

    inside a slot, you can get the sender by "QPushButton* button = qobject_cast<QPushButton*>sender()". If the cast fails, button is NULL.

    Please show us the code how you create the buttons.

  5. #5
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically linking signals and slots for buttons

    I find the number of child nodes and then get the count using the QDomNodeList and then use a for loop connect the signal with the slots like this,

    Qt Code:
    1. connect(Button1[i],SIGNAL(clicked()),this,SLOT(Display()));
    To copy to clipboard, switch view to plain text mode 

    I need to know which is the button that is clicked in the Display button and there is where i am stuck

    This is how i create my buttons.
    Qt Code:
    1. Button1[i] = new QPushButton( this);
    2. Button1[i]->setGeometry(QRect(x ,y,button_width,button_height));
    3. Button1[i]->setText(QApplication::translate("Widget", text.toStdString().c_str(), 0, QApplication::UnicodeUTF8));
    4. Button1[i]->show();
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically linking signals and slots for buttons

    I need to know which is the button that is clicked in the Display button and there is where i am stuck
    FlixB already answered that - you can use the sender() method to compare against your buttons.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically linking signals and slots for buttons

    Wiseguy.. its true that FixB told me how to go about it.. but i am completely new to QT and i am doing and things are going over my head. so its taking me time to figure out how to use the sender function and other API's

  8. #8
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Dynamically linking signals and slots for buttons

    Quote Originally Posted by chandru@080 View Post
    Wiseguy.. its true that FixB told me how to go about it.. but i am completely new to QT and i am doing and things are going over my head. so its taking me time to figure out how to use the sender function and other API's
    it's not a problem being a beginner. i started with qt some months ago and a lot of things were confusing to me. BUT: what is your problem now? put the line i posted above in your "Display()" method and you have access to the clicked button.

    this thread is something like:
    A: can someone help me adding 2 and 3?
    B: use "plus"-operator
    A: thank you, but i need to know how to add the values
    C: use the solution posted by A
    B: sorry, I'm new. I don't know how to use "plus"

  9. The following user says thank you to FelixB for this useful post:

    chandru@080 (7th December 2010)

  10. #9
    Join Date
    Nov 2010
    Posts
    15
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Dynamically linking signals and slots for buttons

    Well Said... There is no problem now.. I got it done. thanks a lot. I completely agree with the steps that you mentioned.. i was going round and round and later found that it was such a simple thing that can be done in no time... well i guess i need to bang my head somewhere for wasting so much of time.

    Felix thanks a ton for the response.

  11. #10
    Join Date
    Jan 2008
    Posts
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Dynamically linking signals and slots for buttons

    to identify a button, use the QSignalMapper.
    short example :

    Qt Code:
    1. QSignalMapper * mapper = new QSignalMapper(this);
    2. QDomElement child = elem.firstChildElement("choice");
    3. while(!child.isNull())
    4. {
    5. QPushButton * button = new QPushButton(child.text());
    6. connect(button, SIGNAL(clicked()), &mapper, SLOT(map()));
    7. mapper.setMapping(button, child.attribute("value"));
    8. vLayout->addWidget(button);
    9.  
    10. child = child.nextSiblingElement("choice");
    11. }
    12. connect(mapper, SIGNAL(mapped(const QString &)), this, SLOT(onbuttonClicked(const QString &)));
    To copy to clipboard, switch view to plain text mode 
    the slot :
    Qt Code:
    1. void Mainwindow::onbuttonClicked(const QString& value)
    2. {
    3. // value is here to identify the button
    4. }
    To copy to clipboard, switch view to plain text mode 
    And I use it with this kind of XML :
    Qt Code:
    1. <choices name="image">
    2. <choice value="animated">Show an animated image</choice>
    3. <choice value="not">Show a picture</choice>
    4. <choice value="nothing">Pass</choice>
    5. </choices>
    To copy to clipboard, switch view to plain text mode 

    so when I click on "Show a picture", the slot "onbuttonClicked" is called with the value "not".

  12. #11
    Join Date
    Nov 2013
    Posts
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Windows

    Default Re: Dynamically linking signals and slots for buttons

    Hi ,

    As felix said i have used "QPushButton* button = qobject_cast<QPushButton*>sender()" in my push button click slot. But when i compile i get the following error:

    error: cannot resolve overloaded function 'qobject_cast' based on conversion to type 'QPushButton*'

    Kindly help.

  13. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Dynamically linking signals and slots for buttons

    It should be:
    Qt Code:
    1. QPushButton* button = qobject_cast<QPushButton*>(sender());
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  14. #13
    Join Date
    Sep 2011
    Posts
    51
    Thanks
    2
    Qt products
    Qt4

    Default Re: Dynamically linking signals and slots for buttons

    I try to use the code below :
    Qt Code:
    1. while(!child.isNull())
    2. {
    3. QPushButton * button = new QPushButton(child.text());
    4. connect(button, SIGNAL(clicked()), &mapper, SLOT(map()));
    5. mapper.setMapping(button, child.attribute("value"));
    6. vLayout->addWidget(button);
    7.  
    8. child = child.nextSiblingElement("choice");
    9. }
    10. connect(mapper, SIGNAL(mapped(const QString &)), this, SLOT(onbuttonClicked(const QString &)));
    To copy to clipboard, switch view to plain text mode 

    in the SLOT I receive correctly the value passed

    Qt Code:
    1. void Mainwindow::onbuttonClicked(const QString& value)
    2. {
    3. // value is here to identify the button
    4.  
    5. QPushButton* button = qobject_cast<QPushButton*>(sender());
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 

    the button object is always NULL, why this ?
    I want to retrieve button name but the sender is always NULL..
    Last edited by anda_skoa; 7th April 2014 at 12:00. Reason: changed [qtclass] to [code]

  15. #14
    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: Dynamically linking signals and slots for buttons

    Quote Originally Posted by gab74 View Post
    the button object is always NULL, why this ?
    The sender of the signal is the QSIgnalMapper. It cannot be casted to QPushButton, so the qobject_cast returns a null pointer.

    Quote Originally Posted by gab74 View Post
    I want to retrieve button name but the sender is always NULL..
    If you mean button pointer, see QSignalMapper::mapping()

    Cheers,
    _

Similar Threads

  1. Dynamically created buttons.
    By Tomasz in forum Newbie
    Replies: 26
    Last Post: 2nd December 2010, 09:40
  2. Dynamically creating buttons from Xml file
    By chandru@080 in forum Newbie
    Replies: 5
    Last Post: 25th November 2010, 10:34
  3. Replies: 0
    Last Post: 3rd October 2010, 20:51
  4. Dynamically Loading UI With Unspecified Slots
    By spraff in forum Qt Programming
    Replies: 2
    Last Post: 11th April 2010, 10:46
  5. create multiple buttons with signal and slots
    By mohanakrishnan in forum Qt Programming
    Replies: 8
    Last Post: 14th November 2009, 12:03

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.