What is event->accept means in contextMenuEvent



  • class myTextEdit: public QTextEdit

    void myTextEdit:contextMenuEvent(QContextMenuEvent event) {
    QMenu * menu = createStandardContextMenu();
    QAction clearAction = new IN_CURRENT_POOL QAction("Clear",this);
    clearAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_R));
    connect(clearAction,SIGNAL(triggered()),this,SLOT(clear()));
    menu->addSeparator();
    menu->addAction(clearAction);
    menu->exec(event->globalPos());
    delete menu;
    event->accept();
    }

    What will happend if I do not put event->accept(); in this case and From the documenntation , I could not make it what does event->accept(); do in above code .


  • Moderators

    From the documentation (http://doc.qt.io/qt-5/qevent.html#accept):

    void QEvent::accept()
    
    Sets the accept flag of the event object, the equivalent of calling setAccepted(true).
    
    Setting the accept parameter indicates that the event receiver wants the event. Unwanted events might be propagated to the parent widget.
    

    If you call accept then the event is not propagated to the parent of the widget. If you reject the event then it is propagated to the parent widget which then can do what ever it does with such events.


Log in to reply
 

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