Results 1 to 7 of 7

Thread: Misplaced Context Menu

  1. #1
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Misplaced Context Menu

    Hi guys, I've been playing around with a context menu on a QListWidget and I can't make it work.
    I have a slot connected to the customContextMenuRequested(const QPoint pos&) which creates a QMenu and opens it in "pos" (theoretically).
    The thing is that I can't show the menu on the right place. It shows itself way too far from mouse, even when I debug the itemAt(pos)->text() and can see the right item is selected.
    This is my code:
    Qt Code:
    1. void AdminWindow::contextMenuRequested( const QPoint &pos )
    2. {
    3. if (tablesList->itemAt(pos))
    4. {
    5. QMenu popupMenu(tablesList);
    6. connect( popupMenu.addAction("&Edit Table"), SIGNAL(triggered()), this, SLOT(editActionTriggered()) );
    7. connect( popupMenu.addAction("&Drop Table"), SIGNAL(triggered()), this, SLOT(dropActionTriggered()) );
    8. // qWarning(tablesList->itemAt(pos)->text().toAscii());
    9.  
    10. popupMenu.exec(pos + QPoint(tablesList->horizontalScrollBar()->value(), tablesList->verticalScrollBar()->value()));
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 

    I tried adding values from scrollbars as I saw in this forum, but no luck.
    I also tried with mapToParent() and mapToGlobal(), same results.
    Any ideas?

    Thanks a lot in advance.
    Kandalf
    There's no place like ~

  2. #2
    Join Date
    Feb 2006
    Location
    Boulder, Colorado, USA
    Posts
    63
    Thanked 8 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Misplaced Context Menu

    Try
    Qt Code:
    1. popupMenu.exec( mapToGlobal( pos ) );
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Misplaced Context Menu

    Quote Originally Posted by jrideout View Post
    Try
    Qt Code:
    1. popupMenu.exec( mapToGlobal( pos ) );
    To copy to clipboard, switch view to plain text mode 
    Sorry, already tried, but no luck.

    Thanx anyway.
    Kandalf
    There's no place like ~

  4. #4
    Join Date
    Jan 2006
    Location
    travelling
    Posts
    1,116
    Thanks
    8
    Thanked 127 Times in 121 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Misplaced Context Menu

    Quote Originally Posted by kandalf View Post
    Sorry, already tried, but no luck.
    Then try the contrary :

    if pos is in global coordinates :
    Qt Code:
    1. popupMenu.exec( tablesList->mapFromGlobal(pos) );
    To copy to clipboard, switch view to plain text mode 
    if pos is in local coordinates :
    Qt Code:
    1. popupMenu.exec( tablesList->mapFromGlobal(mapToGlobal(pos)) );
    To copy to clipboard, switch view to plain text mode 
    Hope this helps.
    Last edited by fullmetalcoder; 16th February 2007 at 12:45. Reason: typos in code (dot instead of arrow)...
    Current Qt projects : QCodeEdit, RotiDeCode

  5. #5
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Misplaced Context Menu

    Ok guys, I couldn't make it work. I reimplemented contextMenuEvent method and now it works as it should.

    My code ended up like this:
    Qt Code:
    1. TableListWidget.h
    2. #include <QListWidget>
    3.  
    4. class TableListWidget : public QListWidget
    5. {
    6. Q_OBJECT
    7. public:
    8. TableListWidget(QWidget *);
    9.  
    10. void contextMenuEvent(QContextMenuEvent *);
    11. };
    12.  
    13. TableListWidget.cpp
    14. ...
    15. void TableListWidget::contextMenuEvent( QContextMenuEvent *event)
    16. {
    17. QMenu popupMenu(this);
    18. connect( popupMenu.addAction("&Edit Table"), SIGNAL(triggered()), this, SLOT(editActionTriggered()) );
    19. connect( popupMenu.addAction("&Drop Table"), SIGNAL(triggered()), this, SLOT(dropActionTriggered()) );
    20. // qWarning(tablesList->itemAt(pos)->text().toAscii());
    21.  
    22. // popupMenu.exec(pos + QPoint(tablesList->horizontalScrollBar()->value(), tablesList->verticalScrollBar()->value()));
    23. popupMenu.exec(this->mapToGlobal(event->pos()));
    24. }
    25. ...
    To copy to clipboard, switch view to plain text mode 

    Thanx everybody for the answers anyway.

    Cheers!
    Kandalf
    There's no place like ~

  6. #6
    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: Misplaced Context Menu

    Quote Originally Posted by kandalf View Post
    My code ended up like this:
    Qt Code:
    1. popupMenu.exec(this->mapToGlobal(event->pos()));
    To copy to clipboard, switch view to plain text mode 
    By the way, QContextMenuEvent already contains a global position of the request so it could be even:
    Qt Code:
    1. popupMenu.exec(event->globalPos());
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  7. #7
    Join Date
    Jan 2006
    Posts
    46
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Misplaced Context Menu

    Qt Code:
    1. popupMenu.exec(event->globalPos());
    To copy to clipboard, switch view to plain text mode 
    Thanx! I saw this method in docs and I thought I tried it and did not work. But, actually, I didn't, because it DOES work.

    Thanx again.
    Cheers.
    Kandalf
    There's no place like ~

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 09:52
  2. menu on right click on canvas
    By quickNitin in forum Newbie
    Replies: 4
    Last Post: 8th October 2006, 12:09
  3. context menu advice
    By larry104 in forum Qt Programming
    Replies: 1
    Last Post: 4th October 2006, 07:55
  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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.