Results 1 to 7 of 7

Thread: Right click context menu by sub-classing how to catch the trigger

  1. #1
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Right click context menu by sub-classing how to catch the trigger

    Hi all I want to have a context menu on a qlistwidget.
    I am using the designer and followed the first example from:

    http://www.developer.nokia.com/Commu...licks_using_Qt

    Everything is working fine, the menu pops up. But how can I catch in my MainWindow if the first entry from context menu is clicked or action is triggerd. Or to run a procedure from MainWindow when context menu is clicked.

    Any help is appreciated.

  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: Right click context menu by sub-classing how to catch the trigger

    The menu consists of a bunch of actions. When you choose an entry in the menu, the action associated with it will be triggered(). Alternatively QMenu::exec() returns a pointer to the action that was chosen, you can use that as well.
    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. #3
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Right click context menu by sub-classing how to catch the trigger

    Quote Originally Posted by wysota View Post
    The menu consists of a bunch of actions. When you choose an entry in the menu, the action associated with it will be triggered().
    I tried like this in my MainWidget.cpp but it is not working. I also made actionNew Public and class and header is linked to MainWidget.cpp/.h

    QObject::connect(ui->listwidget->actionNew, SIGNAL(triggered()), this, SLOT(showMaximized()));

    A normal menu works like a charm but this method mentioned with sub-classing eats my brain I tried so many things I am going crazy.

  4. #4
    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: Right click context menu by sub-classing how to catch the trigger

    Subclassing has nothing to do with this.
    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.


  5. #5
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Right click context menu by sub-classing how to catch the trigger

    Ok I see. I got it working now by adding actions and a signal. In Mainwindow I connected to the signal. I hope this is the right procedure.

  6. #6
    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: Right click context menu by sub-classing how to catch the trigger

    If I want a context menu on a view, then I usually do something like this (assuming I'm not subclassing the view itself):

    Qt Code:
    1. listView->installEventFilter(this);
    2.  
    3. bool Window::eventFilter(QObject *obj, QEvent *e) {
    4. if(obj == listView && e->type == QEvent::ContextMenu) {
    5. showViewContextMenu(static_cast<QContextMenuEvent*>(e)->pos());
    6. }
    7. return ...;
    8. }
    To copy to clipboard, switch view to plain text mode 

    ... or ...

    Qt Code:
    1. listView->setContextMenuPolicy(Qt::CustomContextMenu);
    2. connect(listView, SIGNAL(customContextMenuRequested(QPoint)), this, SLOT(showViewContextMenu(QPoint)));
    To copy to clipboard, switch view to plain text mode 

    ... and then ...

    Qt Code:
    1. void Window::showViewContextMenu(const QPoint &pt) {
    2. QModelIndex idx = listView->indexAt(pt);
    3. modelIndexForContextMenu = idx; // optional, to let slots know what index is the base for the context menu
    4. QMenu menu;
    5. fillMenuWithActionsDependingOnIndex(&menu, idx);
    6. menu.exec(listView->viewport()->mapToGlobal(pt));
    7. }
    To copy to clipboard, switch view to plain text mode 

    assuming I have a bunch of QAction objects with triggered() signals already connected to respective slots I can do:
    Qt Code:
    1. void Window::fillMenuWithActionsDependingOnIndex(QMenu *menu, const QModelIndex &idx) {
    2. ui->action1->setEnabled(idx.parent().isValid());
    3. ui->action2->setEnabled(idx.column() == 0);
    4. // etc.
    5. menu->addAction(ui->action1);
    6. menu->addAction(ui->action2);
    7. // etc.
    8. }
    To copy to clipboard, switch view to plain text mode 

    Everything else works on its own.

    Of course that's only one of possible approaches.

    By the way, code in the article you linked to is flawed and can cause an application to crash. By some luck it probably doesn't crash but it doesn't make the code valid.
    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.


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

    Tuxedo (22nd March 2017)

  8. #7
    Join Date
    Jul 2012
    Posts
    7
    Thanks
    3
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Right click context menu by sub-classing how to catch the trigger

    This with the QModelIndex looks nice I will try it. With your methode you don't need to emit a signal thats good. Thank you for your detailed answer.

Similar Threads

  1. Replies: 2
    Last Post: 3rd May 2011, 21:01
  2. how to make the popup context menu disappear when click the options softkey ?
    By kongkong163 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th January 2011, 03:39
  3. Trigger Menu on Mouse Tap.
    By QTWarrior in forum Qt Programming
    Replies: 0
    Last Post: 18th November 2010, 21:27
  4. cancle right click context menu in qtwebkit
    By umen in forum Qt Programming
    Replies: 1
    Last Post: 6th August 2009, 21:06
  5. QWT right click window.. (Context Menu)
    By maveric in forum Qt Programming
    Replies: 4
    Last Post: 25th May 2008, 08:07

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.