Results 1 to 14 of 14

Thread: Dynamically linking signals and slots for buttons

Hybrid View

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

  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: 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
  •  
Qt is a trademark of The Qt Company.