Results 1 to 10 of 10

Thread: extend the standard context menu of qtextedit

  1. #1
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Question extend the standard context menu of qtextedit

    Hi all,
    when i extend the standard context menu of qtextedit, i found all the actions is disable except the "paste" and "clear". why?
    Qt Code:
    1. QMenu *editContextMenu = ui->textEdit->createStandardContextMenu();
    2. editContextMenu->addAction(clear);
    3. ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
    4. ...
    5. editContextMenu->exec(QCursor::pos());
    To copy to clipboard, switch view to plain text mode 

  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: extend the standard context menu of qtextedit

    Some actions are enabled only in some contexts and not in others. Try selecting some text and then bring up the context menu.
    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
    Oct 2010
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: extend the standard context menu of qtextedit

    Hi wysota,
    I have input something in the textedit, and then select them by mouse(because the "select all" action is disable), but the cut and copy actions are still disable.
    If i don't extend the standard context menu, everything works well.

  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: extend the standard context menu of qtextedit

    Wait a moment, where did you put the code snippet you pasted in the first post?
    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
    Oct 2010
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: extend the standard context menu of qtextedit

    Quote Originally Posted by wysota View Post
    Wait a moment, where did you put the code snippet you pasted in the first post?
    Hi wysota,
    The example is like this:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. ...
    4. createContextMenu()
    5. connect(ui->textEdit,SIGNAL(customContextMenuRequest(const QPoint&)),this,SLOT(showContextMenu()));
    6. ...
    7. }
    8.  
    9. void MainWindow::createContextMenu()
    10. {
    11. editContextMenu = ui->textEdit->createStandardContextMenu();
    12. editContextMenu->addAction(clear);
    13. ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
    14. }
    15.  
    16. void MainWindow::showContextMenu()
    17. {
    18. if(editContextMenu)
    19. editContextMenu ->exec(QCursor::pos());
    20. }
    To copy to clipboard, switch view to plain text mode 

    Thanks for your reply
    Last edited by cai; 18th October 2010 at 18:13.

  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: extend the standard context menu of qtextedit

    It should be:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
    4. connect(ui->textEdit,SIGNAL(customContextMenuRequest(const QPoint&)),
    5. this,SLOT(showContextMenu(const QPoint &)));
    6. }
    7.  
    8. void MainWindow::showContextMenu(const QPoint &pt)
    9. {
    10. QMenu *menu = ui->textEdit->createStandardContextMenu();
    11. menu->addAction(clear);
    12. menu->exec(ui->textEdit->mapToGlobal(pt));
    13. delete menu;
    14. }
    To copy to clipboard, switch view to plain text mode 
    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 2 users say thank you to wysota for this useful post:

    cai (19th October 2010), frankiefrank (27th October 2014)

  8. #7
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: extend the standard context menu of qtextedit

    Quote Originally Posted by wysota View Post
    It should be:
    Qt Code:
    1. MainWindow::MainWindow()
    2. {
    3. ui->textEdit->setContextMenuPolicy(Qt::CustomContextMenu);
    4. connect(ui->textEdit,SIGNAL(customContextMenuRequest(const QPoint&)),
    5. this,SLOT(showContextMenu(const QPoint &)));
    6. }
    7.  
    8. void MainWindow::showContextMenu(const QPoint &pt)
    9. {
    10. QMenu *menu = ui->textEdit->createStandardContextMenu();
    11. menu->addAction(clear);
    12. menu->exec(ui->textEdit->mapToGlobal(pt));
    13. delete menu;
    14. }
    To copy to clipboard, switch view to plain text mode 
    Hi wysota,
    Yes, it works well. But i still don't know why? Can you tell me?
    thanks.

  9. #8
    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: extend the standard context menu of qtextedit

    You were "pre-creating" the context menu and trying to reuse it with each context menu request but the menu and its actions are created based on the current state of the editor (the actions are not updated), so they didn't reflect any changes (such as selecting text) that happened after you called createStandardContextMenu().
    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.


  10. The following 2 users say thank you to wysota for this useful post:

    cai (19th October 2010), oberlus (10th November 2011)

  11. #9
    Join Date
    Oct 2010
    Posts
    6
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: extend the standard context menu of qtextedit

    Quote Originally Posted by wysota View Post
    You were "pre-creating" the context menu and trying to reuse it with each context menu request but the menu and its actions are created based on the current state of the editor (the actions are not updated), so they didn't reflect any changes (such as selecting text) that happened after you called createStandardContextMenu().
    I get it. Thanks for your help!

  12. #10
    Join Date
    Mar 2010
    Posts
    10
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: extend the standard context menu of qtextedit

    Seems customContextMenuRequest has now been changed to customContextMenuRequested

Similar Threads

  1. Context menu on tab
    By wolfi3b in forum Newbie
    Replies: 2
    Last Post: 18th October 2010, 17:35
  2. Two context menu for a treewidget
    By phillip_Qt in forum Qt Programming
    Replies: 2
    Last Post: 24th November 2009, 11:06
  3. Context menu
    By dejvis in forum Newbie
    Replies: 2
    Last Post: 20th September 2009, 22:02
  4. Qwt and context menu
    By giusepped in forum Qwt
    Replies: 1
    Last Post: 9th December 2008, 08:55
  5. Context Menu
    By RY in forum Newbie
    Replies: 1
    Last Post: 10th September 2008, 07:59

Tags for this Thread

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.