Results 1 to 3 of 3

Thread: Context Menu Code Placement

  1. #1
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Context Menu Code Placement

    Hello,

    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.
    Qt Code:
    1. class MainForm(QtGui.QDialog):
    2. super(MainForm, self).__init__(parent)
    3.  
    4. self.dataset = dmeta()
    5. self.tablemodel = MetaTableModel(self.dataset)
    6. self.tv = QtGui.QTableView()
    7. self.tv.setModel(self.tablemodel)
    8.  
    9. self.cAddColumn = QtGui.QAction('Insert Column',self.tv)
    10. self.cAddColumn.triggered.connect(self.contextAddColumn)
    11. self.tv.addAction(self.cAddColumn)
    12. self.cRemoveColumn = QtGui.QAction('Remove Column',self.tv)
    13. self.cRemoveColumn.triggered.connect(self.contextRemoveColumn())
    14. self.tv.addAction(self.cRemoveColumn)
    15.  
    16. def contextAddColumn(self,index):
    17. pass # Add column in the appropriate spot
    18.  
    19. def contextRemoveColumn(self,index):
    20. pass # Remove column in the appropriate spot
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Context Menu Code Placement

    A usual approach is to reimplement contextMenuEvent for the view (not the form the view is on).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    mechsin (14th October 2011)

  4. #3
    Join Date
    Apr 2011
    Posts
    19
    Thanks
    3
    Thanked 4 Times in 2 Posts
    Qt products
    Platforms
    Unix/X11 Windows

    Default Re: Context Menu Code Placement

    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

Similar Threads

  1. QTabBar - Context menu on tab
    By gruszczy in forum Qt Programming
    Replies: 5
    Last Post: 3rd July 2020, 14:33
  2. Context menu on tab
    By wolfi3b in forum Newbie
    Replies: 2
    Last Post: 18th October 2010, 18:35
  3. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 23:02
  4. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 09:55
  5. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 08:59

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.