Results 1 to 8 of 8

Thread: QPushButton clicked

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,313
    Thanks
    314
    Thanked 870 Times in 857 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPushButton clicked

    So the main purpose of it is how to serve a lot of buttons.
    It sounds like you are asking for a slot to be called any time a button is clicked, whether you connect to its signal or not. How would that even be possible? How could the poor button know which of the slots in your app to call if you don't tell it?

    If you don't connect -some- slot to -every- button's clicked() signal, your slots will only be called for the buttons where you did make a connection.

    If you make a single slot and connect it to all of the buttons' clicked() signals, you can use the QObject::sender() method inside the slot to give you the pointer to the button instance that invoked the slot, and you can use that pointer to find out the button's properties (like name).

    Edit: high_flyer's QSignalMapper might also work, but it is a bit hard to understand for a beginner.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  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: QPushButton clicked

    For the setup you can just retrieve all buttons or all buttons within a widget using QObject::findChildren().

    Then loop over the list to connect to the slot or signal mapper.

    Cheers,
    _
    Last edited by anda_skoa; 17th February 2017 at 09:42.

  3. #3
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPushButton clicked

    Thanks!
    d_stranz - you are right. I thought that exist a event that is called any time a button is clicked and then in that event/function I get clicked button property (e.g. accessibleName) and accordingly to it I do sth.
    So, I'll try QObject::findChildre() to connect to the slot and QObject::sender() to get the clicked button.

  4. #4
    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: QPushButton clicked

    There is no "click" event because these are in fact multiple events in a certain order, i.e. a mouse press event followed by a mouse release event within a button's geometry.

    If there were an event you could install a global event filter but that would also be way more hackish than dealing with the buttons explicitly.

    Cheers,
    _

Similar Threads

  1. Replies: 0
    Last Post: 12th May 2013, 10:09
  2. Replies: 2
    Last Post: 26th October 2011, 15:41
  3. how can i get the QPushButton clicked on a QTable
    By showben in forum Qt Programming
    Replies: 2
    Last Post: 2nd March 2011, 15:42
  4. Qpushbutton clicked()
    By sasori1512 in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2011, 02:03
  5. QPushButton color when clicked
    By ntp in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2008, 18:34

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.