PDA

View Full Version : how to trigger context menu actions



flashburn
20th September 2016, 01:53
I manually added the following menu to my QTableWidget.

Here is what I have:

12115

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.

anda_skoa
20th September 2016, 07:06
The actions objects have a "triggered()" signal.

I am not using PyQt myself but I think connect works something like


self.actionReadRegister.triggered.connect(self.som eMethod);


Cheers,
_

flashburn
20th September 2016, 17:44
Thanks. I think you got it. How did you know where to look? I wasn't able to figure it out

d_stranz
20th September 2016, 18:00
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 (http://doc.qt.io/qt-5/qtexamplesandtutorials.html) 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.