Results 1 to 5 of 5

Thread: QLineEdit contextMenu

  1. #1
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QLineEdit contextMenu

    Hi,

    I have a class inherited from QLineEdit and I want to show a custom context Menu.

    Qt Code:
    1. MyLineEdit::MyLineEdit(QWidget *parent) : QLineEdit(parent)
    2. {
    3. ui.setupUi(this);
    4. this->setContextMenuPolicy(Qt::CustomContextMenu);
    5. }
    6.  
    7. void MyLineEdit::contextMenuEvent(QContextMenuEvent *event)
    8. {
    9. //Never enters here
    10. QMenu* pqMenu = new QMenu();
    11. pqMenu->addAction(tr("Firts"));
    12. pqMenu->addAction(tr("Second"));
    13. pqMenu->exec(event->globalPos());
    14. delete (pqMenu);
    15. }
    To copy to clipboard, switch view to plain text mode 

    In the header file I have:
    Qt Code:
    1. protected:
    2. void contextMenuEvent(QContextMenuEvent *event);
    To copy to clipboard, switch view to plain text mode 

    Anyone knows what is the problem?

    Thanks,
    Òscar Llarch i Galán

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit contextMenu

    Hi

    Sorry, I don't undersand why, but I have recompiled all the project and now it works.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit contextMenu

    use instead QLineEdit::createStandardContextMenu().
    Also, make sure contextMenuPolicy is not set to Qt::NoContextMenu.

    regards

  4. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit contextMenu

    Quote Originally Posted by ^NyAw^ View Post
    Hi

    Sorry, I don't undersand why, but I have recompiled all the project and now it works.
    Yes, but using the solution I posted is more efficient because you create the menu only once, not every time you show it.

    You solution is good if tou want to customize the menu according to some runt-time options ( maybe add or remove items from it ), etc.

  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QLineEdit contextMenu

    Hi,

    Ok, will try it. Thanks for your idea
    Òscar Llarch i Galán

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
  •  
Qt is a trademark of The Qt Company.