Results 1 to 4 of 4

Thread: context menu advice

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default context menu advice

    Hi,

    I would need some advice how to implement context menus the right way.

    Here the situation:

    I have a main widget which contains two tableviews each assigned to a model. Right click over a row triggers a different action for each of the two tables.

    scenario 1:
    subclass tableview and implement
    void MyTableView1::contextMenuEvent(QContextMenuEvent* e) and
    void MyTableView2::contextMenuEvent(QContextMenuEvent* e)
    Check if
    QModelIndex index = indexAt(e->pos());
    is valid
    Problem 1: hard to get from here to the model data to change something.

    scenario 2:
    Implement contextMenuEvent(QContextMenuEvent* e) for main window and check if
    QModelIndex index = table1->indexAt(e->pos());
    QModelIndex index = table2->indexAt(e->pos());
    are valid.
    Problem 2: e->pos() gives me the position releative to the main window. Is there a way to translate these positions relative to the tableViews?

    So, in summary what is the correct way to implement context menus: globally or relative to the widget

    Thanks.

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: context menu advice

    Quote Originally Posted by larry104 View Post
    Problem 1: hard to get from here to the model data to change something.
    Why? You can access the model through QAbstractItemView::model().
    Qt Code:
    1. model()->setData(index, data, role);
    To copy to clipboard, switch view to plain text mode 

    Quote Originally Posted by larry104 View Post
    Problem 2: e->pos() gives me the position releative to the main window. Is there a way to translate these positions relative to the tableViews?
    Yes, see docs for QWidget::mapFrom*() and QWidget::mapTo*().
    J-P Nurmi

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

    larry104 (4th October 2006)

  4. #3

    Default Re: context menu advice

    The best practice is to subclass each QTableView and implement contextMenuEvent locally (scenario 1). To access model data, use model()->data(index) from the valid QModelIndex. This keeps context handling modular and avoids position translation issues.

    *** Moderator note: Spam link removed. There is zero tolerance for spam in this forum. ***
    Last edited by d_stranz; 7th August 2025 at 18:21. Reason: removed URL

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,330
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: context menu advice

    @symbolsoden: This is a 19 year old thread. If your only purpose for replying to it was to post a spam link to a games website, then don't do that again or you'll be banned.
    <=== 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. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  2. Qt's optimized OpenGL context switching
    By sverhoff in forum Qt Programming
    Replies: 0
    Last Post: 28th March 2006, 16:40
  3. Context OpenGL
    By mickey in forum Qt Programming
    Replies: 4
    Last Post: 3rd March 2006, 21:13
  4. Q3TextEdit custom context menu
    By bcteh_98 in forum Qt Programming
    Replies: 1
    Last Post: 15th February 2006, 21:00
  5. Tracking separators in a menu (insertSeparator)
    By PrimeCP in forum Qt Programming
    Replies: 4
    Last Post: 25th January 2006, 18:10

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
  •  
Qt is a trademark of The Qt Company.