Copy from QLineEdit using Pop up menu

  • Thanks One more thing , Can I customise the Pop menu in which I have only copy

    I do not want menu items
    Undo,Redo, Cut ,Paste ,SelectAll

    I only want Copy in my Right mouse button popup menu . COuld you please provide the sameple for it

  • Moderators

    this topic is forked from a previous one

  • QLabel* CountLabel = new QLabel(tr(" count:"));
    QLineEdit* countValueLabel = new QLineEdit(QString("% 1").arg(count));
    countValueLabel->setStyleSheet("background: rgb(236,236,236); border: 0 px;");
    countValueLabel->setBuddy(countValueLabel );

  • class customisedLineEdit: public QLineEdit {


    customisedLineEdit::customisedLineEdit(QString string)
    setStyleSheet("background: rgb(236,236,236); border: 0px;");

    void customisedLineEdit::contextMenuEvent(QContextMenuEvent event) {
    printf("\n here in contextMenu:");
    menu = createStandardContextMenu();
    QAction* copyAction = new IN_CURRENT_POOL QAction("Copy",this);

    delete menu;


    I do not get any menu on RMB . Could you suggest how to get only copy in ContextMenu for QlinedEdit

  • Moderators

    @Qt-Enthusiast said in Copy from QLineEdit using Pop up menu:

    void customisedLineEdit::contextMenuEvent(QContextMenuEvent event)

    It must be

    void customisedLineEdit::contextMenuEvent(QContextMenuEvent *event) 

    as you can see here
    Is contextMenuEvent called?

  • @Qt-Enthusiast

    When Qt::CustomContextMenu is set on the widget, contextMenuEvent() handler is not called. Instead QWidget::customContextMenuRequested() signal emitted.

    Removed setContextMenuPolicy(Qt::CustomContextMenu) or like this:

    connect(this, &MyLineEdit::customContextMenuRequested, [=](const QPoint &pos){
        QMenu *menu = createStandardContextMenu();
        menu->addAction(tr("My Menu Item"));
        delete menu;

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.