View Full Version : Context Menu Code Placement

13th October 2011, 21:34

So I am switching a gui I have from using the QTable Widget to using a table view. I had a context menu on the old gui where I just added actions and had those actions connected to slots. When I moved over to a view/model approch I was left not really sure how I should implement. I started by adding the actions to the Model but then discovered that the model had no way to tell what the current index is so then I ended up moving them to the Mainform but that just doesn't seem right because to get to the current index I need the context slot function to be defined in the MainForm. I am just looking for any obvious do's or don'ts I am missing I am relativly new to GUI design below is and example of what I currently have.

class MainForm(QtGui.QDialog):
super(MainForm, self).__init__(parent)

self.dataset = dmeta()
self.tablemodel = MetaTableModel(self.dataset)
self.tv = QtGui.QTableView()

self.cAddColumn = QtGui.QAction('Insert Column',self.tv)
self.cAddColumn.triggered.connect(self.contextAddC olumn)
self.cRemoveColumn = QtGui.QAction('Remove Column',self.tv)
self.cRemoveColumn.triggered.connect(self.contextR emoveColumn())

def contextAddColumn(self,index):
pass # Add column in the appropriate spot

def contextRemoveColumn(self,index):
pass # Remove column in the appropriate spot

13th October 2011, 21:53
A usual approach is to reimplement contextMenuEvent for the view (not the form the view is on).

14th October 2011, 12:25
OK I see it appears to me that I actually don't need the Mainform the tableview itself already has the attribute show so I don't need to wrap it inside of another class. Thanks