How to create Pop up menu in Qt



  • I have added a tableview in the form. I want to display a pop-up menu with only the cut, copy, selected item options when the user clicks ( right click) on the tableview. How can I achieve this ?Any idea or code available?



  • Take a look on "Menus Example":http://qt-project.org/doc/qt-5/qtwidgets-mainwindows-menus-example.html

    Does it work for you ?



  • In my project am already implemented the menu option in menu bar.About my doubt now i want to implement a pop up menu when the user right click on the tableview.





  • Thanks.That was really hepful.
    @void Connection::customMenuRequested(QPoint pos)
    {
    //Context Menu Creation

    QModelIndex index=ui->tableView_2->indexAt(pos);
    QModelIndex index1=ui->tableView->indexAt(pos);
    QModelIndex index2=ui->tableView_3->indexAt(pos);

    QMenu *menu=new QMenu(this);
    
    menu->addAction(QString("Copy"), this,SLOT(slotCopy()));
    menu->addAction(QString("Cut"), this,SLOT(slotCut()));
    menu->addAction(QString("Paste"),this,SLOT(slotPaste()));
    menu->addAction(QString("Select"),this,SLOT(slotSelect()));
    menu->addAction(QString("Close"), this,SLOT(slotClose()));
    

    menu->popup(ui->tableView_2->viewport()->mapToGlobal(pos));
    menu->popup(ui->tableView->viewport()->mapToGlobal(pos));
    menu->popup(ui->tableView_3->viewport()->mapToGlobal(pos));

    }
    void Connection::slotClose()// Context Menu Option for Close
    {
    this->close();
    }
    @
    When i was right click the tablview the context menu will be displayed and also working the close menu option. But i dn't knw how to write the code for cut,copy,paste, select. I searched in Google. But couldn’t find any examples out there.


Log in to reply
 

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