Results 1 to 4 of 4

Thread: Programmatically click second item on QPushButton that has a QMenu

  1. #1
    Join Date
    Jan 2017
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Programmatically click second item on QPushButton that has a QMenu

    I put a three-option menu (using QMenu add.Action) on a QPushButton (ExpensesChtBtn). In other words, when the button is manually clicked, three options appear. Is it possible to programmatically "click" the second option?

    To click the button, I'm pretty sure that the following is correct:

    Qt Code:
    1. self.chartsWindow.ui.ExpensesChtBtn.clicked.emit()
    To copy to clipboard, switch view to plain text mode 

    How do I modify this code to effectively click the second menu item on that button -- let's say the second menu item is labeled "piechart"?
    thanks

  2. #2
    Join Date
    Jul 2008
    Posts
    406
    Thanks
    9
    Thanked 52 Times in 50 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Programmatically click second item on QPushButton that has a QMenu

    Hi, you can use QAction's trigger() method. This will not execute an animated click, but it will emit the triggered signal of the QAction.

    Ginsengelf

  3. #3
    Join Date
    Jan 2017
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Programmatically click second item on QPushButton that has a QMenu

    How would I modify my code to trigger() the "piechart" menu item?

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,063
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Programmatically click second item on QPushButton that has a QMenu

    To click the button, I'm pretty sure that the following is correct:
    Actually, you probably want to simply call the QPushButton::click() slot. The internal code will ensure that the right signals get emitted and the button changes state as needed.

    It isn't clear what you want to happen when the button is clicked. If the user clicks the button, do you want the menu to appear and let the user choose one of the three options? If you want to programmatically choose one of the options instead, then you don't need to bother with simulating a click at all - simply call the QAction::trigger() slot as Ginsengelf suggested.

    As to how, your program must be executing some code where you have programmatically decided that's what you want to do. Just call it there. You can always retrieve the list of QAction instances you have added to your button using QWidget::actions(). Use QList::at() or QList::operator[]() to retrieve the pointer to to the action at the index you want.
    <=== 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.

Similar Threads

  1. Replies: 2
    Last Post: 5th September 2014, 09:08
  2. QMenu with a additional QPushButton
    By seux in forum Newbie
    Replies: 3
    Last Post: 4th June 2011, 06:25
  3. programmatically right click on QSystemTrayIcon
    By noa l in forum Qt Programming
    Replies: 5
    Last Post: 10th April 2011, 13:42
  4. Defining position for QMenu used by QPushButton
    By Ghaleon in forum Qt Programming
    Replies: 1
    Last Post: 13th February 2009, 09:23
  5. QPushButton QMenu QAction
    By hgedek in forum Newbie
    Replies: 2
    Last Post: 1st November 2007, 12:29

Tags for this Thread

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.