Results 1 to 5 of 5

Thread: Adding actions to a context menu

  1. #1
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Adding actions to a context menu

    Hello Everybody:

    Simple problem here. I use a QPlainTextEdit to do a simple code editor. Now the text edit allready has a standard right-click-contex-menu and I want to add two actions (comment and uncomment). However with the code I found on the web:
    Qt Code:
    1. action_comment = new QAction("Comment",this);
    2. this->addAction(action_comment);
    3. this->setContextMenuPolicy(Qt::ActionsContextMenu);
    To copy to clipboard, switch view to plain text mode 
    And this works but all the default options disappear and only comment appears. What I want to know is, Is there any way to add actions to the default context menu or do I have to create one from scratch if I want more actions?

    Thanks for any help

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

    Default Re: Adding actions to a context menu

    J-P Nurmi

  3. #3
    Join Date
    Jun 2008
    Posts
    83
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Adding actions to a context menu

    Hi:

    Sorry to bother again, but I did found a solution. I reimplemented the contextMenuEvent with this code:

    Qt Code:
    1. void CodeEditor::contextMenuEvent(QContextMenuEvent *event){
    2. QMenu *menu = this->createStandardContextMenu();
    3. menu->addAction(action_comment);
    4. menu->addAction(action_uncomment);
    5. menu->insertSeparator(action_comment);
    6. menu->exec(event->globalPos());
    7. }
    To copy to clipboard, switch view to plain text mode 

    action_comment/uncomment were two private objects which are created and connected in the constructor to their respective code.

    I'm posting it in case it helps anyone.

  4. #4
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Adding actions to a context menu

    you could also use QWidget::customContextMenuRequested and QWdiget::setContextMenuPolicy with Qt::CustomContextMenu.
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  5. #5
    Join Date
    Jan 2011
    Posts
    5
    Thanks
    23
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Adding actions to a context menu

    a better "working" and tested example is here:
    http://www.qtcentre.org/threads/3516...u-of-qtextedit

    I hope I have helped!

Similar Threads

  1. Context Menu on QTableWidget
    By ankurjain in forum Qt Programming
    Replies: 9
    Last Post: 17th December 2009, 10:52
  2. Custom context menu in QTreeView
    By ttvo in forum Qt Programming
    Replies: 5
    Last Post: 3rd April 2009, 23:29
  3. Shortcut key for context menu
    By darshan.hardas in forum Qt Programming
    Replies: 1
    Last Post: 28th December 2008, 21:32
  4. Replies: 0
    Last Post: 7th April 2008, 15:27
  5. Replies: 4
    Last Post: 25th June 2007, 21:40

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.