Results 1 to 8 of 8

Thread: QPushButton clicked

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

    Default QPushButton clicked

    Hello forum,
    I'd like to ask you, if is possible to get clicked button by his accessibleName value.
    I know only this ways:
    1) RMB-->Slot--> generate clicked event
    2) make a connect, sth like connect(ui->pushButton, SIGNAL( .... )


    Thanks.

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

    It will be easier if you tell what it is you want to achieve, the end result, not the way your want to do it, we probably can help you get there in a better way.
    ==========================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
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QPushButton clicked

    The first reason is that I just want to know it, if it is possible.
    The second, let's suppose that I have a application where is a lot of buttons and I'm not sure if is good / clean / .. make a lot of connections. I think that could be better make a event that is automatically called after every clicked button and inside this function (event) will be sth to get AccessibleName.

    Pseudocode:
    Qt Code:
    1. void MainWindow::event(sth) {
    2. if(sth->accessibleName == "btn1")
    3. // do stuff #1
    4. else if(sth->accessibleName == "btn2")
    5. // do stuff #2
    6. etc...
    7. }
    To copy to clipboard, switch view to plain text mode 

    So the main purpose of it is how to serve a lot of buttons.
    Last edited by ado130; 15th February 2017 at 10:57.

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

    Have a look at QSignalMapper
    Other than that, you can use use sender()->objectName() in the slot, but if you need that kind of a hack, something in your design probably is not quite right.
    ==========================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.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 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.

  6. #6
    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 10:42.

  7. #7
    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.

  8. #8
    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, 11:09
  2. Replies: 2
    Last Post: 26th October 2011, 16: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, 16:42
  4. Qpushbutton clicked()
    By sasori1512 in forum Qt Programming
    Replies: 3
    Last Post: 20th February 2011, 03:03
  5. QPushButton color when clicked
    By ntp in forum Qt Programming
    Replies: 3
    Last Post: 30th April 2008, 19: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.