Results 1 to 4 of 4

Thread: [QMenu] Context menu for widget created in Qt Creator

  1. #1
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default [QMenu] Context menu for widget created in Qt Creator

    I'd like to add context menus for widgets on my form.
    As I know to do that I should reimplement Widget::contextMenuEvent(QContextMenuEvent*) function.
    But how can I reimplement it if widget is created in Qt Creator (designer)?

    @EDIT
    Ok, I found out I can do it by this way:
    Qt Code:
    1. void QLineEdit::contextMenuEvent ( QContextMenuEvent * event ){
    2. QMenu *menu=new QMenu;
    3. (...)
    4. menu->exec(event->globalPos());
    5. }
    To copy to clipboard, switch view to plain text mode 
    But is there any way to set context menu for only one lineedit?
    Last edited by Macok; 4th March 2009 at 16:05.

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QMenu] Context menu for widget created in Qt Creator

    That is one way... create your own custom widget and override the contextmenu event. From designer, you can promote the widget to your custom widget.

    The other way is -
    Set QWidget::setContextMenuPolicy to Qt::CustomContextMenu, and connect QWidget::customContextMenuRequested() signal to your handling class. and show the menu as you want in the global pos.

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

    Macok (4th March 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    51
    Thanks
    28
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: [QMenu] Context menu for widget created in Qt Creator

    Thanks a lot.
    I chose second method becouse first seems to be more complicated.
    It works, but menu appears somewhere in the corner of my desktop.
    I think I have to convert QPoint to global position, but how to do it?

    @EDIT
    Ok I found it.
    Function to convert QPoint to global position is QPoint QWidget::mapToGlobal ( const QPoint & pos ) const
    Last edited by Macok; 4th March 2009 at 18:11.

  5. #4
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: [QMenu] Context menu for widget created in Qt Creator

    Even QCursor::pos will give you global pos

    Use whatever suits you

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.