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)
    {
    setText(string);
    setReadOnly(true);
    setStyleSheet("background: rgb(236,236,236); border: 0px;");
    setContextMenuPolicy(Qt::CustomContextMenu);
    }

    void customisedLineEdit::contextMenuEvent(QContextMenuEvent event) {
    printf("\n here in contextMenu:");
    QMenu
    menu = createStandardContextMenu();
    QAction* copyAction = new IN_CURRENT_POOL QAction("Copy",this);
    menu->addAction(copyAction);
    copyAction->setShortcut(tr("Ctrl+C"));
    connect(copyAction,SIGNAL(triggered()),this,SLOT(copy()));
    menu->exec(event->globalPos());

    delete menu;
    event->accept();

    }

    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 http://doc.qt.io/qt-5/qtwidgets-mainwindows-menus-example.html
    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"));
        menu->exec(mapToGlobal(pos));
        delete menu;
    });
    

Log in to reply
 

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