Results 1 to 4 of 4

Thread: how to trigger context menu actions

  1. #1
    Join Date
    Sep 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default how to trigger context menu actions

    I manually added the following menu to my QTableWidget.

    Here is what I have:

    context-menu.png

    Part of the code was setup using Qt Designer and I'm modifying it slightly in my code.

    Here is the code that I've added to have a context menu:

    self.group1RegistersTableWidget.addAction(self.act ionReadRegister)
    self.group1RegistersTableWidget.addAction(self.act ionWriteRegister)
    self.actionSeparator.setSeparator(True)
    self.actionSeparator.setText("")
    self.group1RegistersTableWidget.addAction(self.act ionSeparator)
    self.group1RegistersTableWidget.addAction(self.act ionBinRegisterFormat)
    self.group1RegistersTableWidget.addAction(self.act ionDecRegisterFormat)
    self.group1RegistersTableWidget.addAction(self.act ionHexRegisterFormat)

    self.group2RegistersTableWidget.addAction(self.act ionReadRegister)
    self.group2RegistersTableWidget.addAction(self.act ionWriteRegister)


    and here is the code that I used

    I'm somewhat new to Qt. How do I process these actions. How do I know that Read/Write/Binary/Decimal/Hexadecimal has been clicked. As far as I understand I need to connect some signal to some slot, but I can't figure out which ones. Would someone be able to help me out. I'm using `PyQt4`, but C++ code would also be fine.

    Any help is appreciated.

  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: how to trigger context menu actions

    The actions objects have a "triggered()" signal.

    I am not using PyQt myself but I think connect works something like
    Qt Code:
    1. self.actionReadRegister.triggered.connect(self.someMethod);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    flashburn (20th September 2016)

  4. #3
    Join Date
    Sep 2016
    Posts
    4
    Thanks
    1
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: how to trigger context menu actions

    Thanks. I think you got it. How did you know where to look? I wasn't able to figure it out

  5. #4
    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: how to trigger context menu actions

    You look at the Qt documentation, in this case for QWidget (which is where your addAction() method is defined). You see that the argument for addAction() is an instance of a QAction class. If you look at its documentation, you'll see a list of the signals; if the class inherits from another Qt class, look at the signals and slots for the base classes as well. Not only does QAction have a triggered() signal, but it also has 3 others, plus two inherited from QObject, its base class.

    It also helps to look at the Qt examples and tutorials where almost anything you would want to do in Qt is demonstrated, at least at a basic level, in C++ and usually QML. The documentation for Qt 4.8 is also available if you google for it.
    <=== 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. pop up menu cannot trigger on qtwebkit
    By horoma in forum Qt Programming
    Replies: 0
    Last Post: 15th December 2012, 05:28
  2. Replies: 6
    Last Post: 20th July 2012, 23:50
  3. Adding actions to a context menu
    By aarelovich in forum Qt Programming
    Replies: 4
    Last Post: 7th December 2011, 13:47
  4. Replies: 1
    Last Post: 4th November 2011, 12:25
  5. Trigger Menu on Mouse Tap.
    By QTWarrior in forum Qt Programming
    Replies: 0
    Last Post: 18th November 2010, 22:27

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.