0

I am working on a QTableView displaying a custom model subclasses from QAbstractTableModel in PyQt4. I need to be able to allow the user to set which column they want to serve as a specific type of data. To achieve this I want to implement a context menu when clicking on the header of a column, allowing options to set it as these types. I'm not sure how to create a context menu like this which can differentiate between different columns. Can anyone point me in the right direction?

Thanks

Andrew Ring
  • 3,185
  • 1
  • 23
  • 28
  • possible duplicate of [Getting Header Column on Right Click for QTableWidget](http://stackoverflow.com/questions/11888635/getting-header-column-on-right-click-for-qtablewidget) – ekhumoro Aug 10 '12 at 23:52
  • another duplicate in [How can I get right-click context menus for clicks in QTableView header?](http://stackoverflow.com/questions/7782071/how-can-i-get-right-click-context-menus-for-clicks-in-qtableview-header) –  Nov 25 '12 at 16:53

1 Answers1

3

You can access the information from the header view. You can do something like:

def __init__( self, parent ):
    # initialize class
    ...

    # setup menu options
    header = self.ui.tree.header()
    header.setContextMenuPolicy(Qt.CustomContextMenu)
    header.customContextMenuRequested.connect( self.showHeaderMenu )

def showHeaderMenu( self, point ):
    column = self.ui.tree.header().logicalIndexAt(point.x())

    # show menu about the column
    menu = QMenu(self)
    menu.addAction('Hide Column')

    menu.popup(header.mapToGlobal(pos))
Eric Hulser
  • 3,912
  • 21
  • 20