Results 1 to 9 of 9

Thread: rigth click of mouse to create sub menu

  1. #1
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default rigth click of mouse to create sub menu

    I would like to create a menu when the mouse is above a Qwidget and the right button of the mouse is pressed

    I looked at mouseevent example but I am not sure how to display the menu.

    did anybody came across this or has an idea how to do it??

    thanks

  2. #2
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rigth click of mouse to create sub menu

    I think you can use QMenu and its function void popup ( const QPoint & p, QAction * atAction = 0 ) for this.

    I would guess that there is a widget in your app, and when you right click on it a menu should popup. So yu take your widget's signal
    Qt Code:
    1. void customContextMenuRequested ( const QPoint & pos )
    To copy to clipboard, switch view to plain text mode 

    and connect it to a slot. Inside this slot create a QMenu object, populate it with necessary data and call popup.
    I'm a rebel in the S.D.G.

  3. #3
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: rigth click of mouse to create sub menu

    Do you have some examples
    for what I understood
    fisrt i need
    Qt Code:
    1. connect(mywidget, SiGNAL(rightClick()), this , popupMenu())
    2. ...
    To copy to clipboard, switch view to plain text mode 

    then declare QMenu into the popupMenu()
    Qt Code:
    1. QMenu menu;
    2. menu.addAction(a);
    3. menu.popup(pos,at);
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Does the rightclick() SIGNAL exist for a QTableview??
    Last edited by SunnySan; 11th November 2008 at 11:50.

  4. #4
    Join Date
    Dec 2006
    Posts
    849
    Thanks
    6
    Thanked 163 Times in 151 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rigth click of mouse to create sub menu

    no. you get a contextMenuEvent, when the right mouse button is clicked.
    (if you configure the QWidget properly: see QWidget::setContextMenuPolicy() )

  5. #5
    Join Date
    May 2008
    Location
    Kyiv, Ukraine
    Posts
    418
    Thanks
    1
    Thanked 29 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rigth click of mouse to create sub menu

    Lets say you have a table and a right click on it should call a context menu.

    Qt Code:
    1. // create table, let it be as follows
    2.  
    3. connect(table, SIGNAL(customContextMenuRequested ( const QPoint &)), this, SLOT(popupYourMenu(const QPoint &)));
    To copy to clipboard, switch view to plain text mode 

    Then declare (as a slot) and implement your popupYourMenu slot
    Qt Code:
    1. void YourClass:popupYourMenu(const QPoint & pos)
    2. {
    3. // create popupMenu as QMenu object
    4. // populate it
    5.  
    6. popupMenu->popup(pos);
    7. }
    To copy to clipboard, switch view to plain text mode 

    That should help.
    I'm a rebel in the S.D.G.

  6. The following user says thank you to lyuts for this useful post:

    waynew (10th January 2010)

  7. #6
    Join Date
    Jul 2008
    Location
    Mumbai, India
    Posts
    10
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: rigth click of mouse to create sub menu

    just create a QMenu in the contextMenuEvent(QGraphicsSceneContextMenuEvent *event) function and add QAction in the menu. And u can connect these QAction to different slots which is called when u select a particular option from the menu.
    Example:
    contextMenuEvent(QGraphicsSceneContextMenuEvent *event)
    {
    QAction action1(QIcon("./abcd.png"), "action 1", this);
    QAction action2(QIcon("./abcd2.png"), "action 2", this);
    connect(&action1, SIGNAL(triggered()), this, SLOT(xyz()));
    connect(&action2, SIGNAL(triggered()), this, SLOT(xyz2()));
    QMenu myMenu;
    myMenu.addAction(&action1);
    myMenu.addAction(&action1);
    }

    I hope this may help you.

  8. #7
    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: rigth click of mouse to create sub menu

    Ya, QWidget::contextMenuEvent is a better option.
    You will also need to show the menu using QMenu::exec.

    Also be careful about the actions being created on stack.

  9. #8
    Join Date
    Jul 2008
    Posts
    69
    Thanks
    9
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: rigth click of mouse to create sub menu

    Thanks guys for your guidance but it doesn't seem to work.

    where do you defined the right click ??

    I add this code to my mainwindow.cpp
    Qt Code:
    1. connect(ui.mytableView, SIGNAL(customContextMenuRequested ( const QPoint &)), this, SLOT(popuprightclickMenu(const QPoint &)));
    To copy to clipboard, switch view to plain text mode 

    and

    Qt Code:
    1. void mainwindow::popuprightclickMenu(const QPoint & pos)
    2. {
    3. qDebug()<<":popuprightclickMenu"; //just to see if activated
    4. QAction action1( "action 1", this);
    5. connect(&action1, SIGNAL(triggered()), this, SLOT(about()));
    6.  
    7. QMenu *popupMenu;
    8. popupMenu->addAction(&action1);
    9. popupMenu->addAction(&action1);
    10.  
    11. popupMenu->popup(pos);
    12. }
    To copy to clipboard, switch view to plain text mode 

    BUT I can not see even the debug comments in my console...

  10. #9
    Join Date
    Oct 2008
    Posts
    70
    Thanks
    1
    Thanked 9 Times in 9 Posts

    Default Re: rigth click of mouse to create sub menu

    This signal customContextMenuRequested is emitted when the widget's contextMenuPolicy is Qt::CustomContextMenu. Please check contextMenuPolicy for you widget.

Similar Threads

  1. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  2. QGraphicItem mouse click detection
    By PrimeCP in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2007, 11:35

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.