Important: Please read the Qt Code of Conduct -

Add contextual menu to QTableView

  • Hi All,
    In QT4 i am trying to implement context menu for my table got options on table like add delete after clicking the right clicking of mouse button ,......further i need to edit the table header elements as well as contents of the table on the same window with using push button or without using push button i am sharing my code below ....plz any one help me above issue and further procedure....

    model=new QStandardItemModel(10,5,this);
    QVBoxLayout *l=new QVBoxLayout(this);
    table=new QTableView(this);
    //connect(table, SIGNAL(customContextMenuRequested(QPoint)),SLOT(customMenuRequested(QPoint)));
    connect(this, SIGNAL(customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos)));
    void Widget::customMenuRequested(QPoint pos){
    QModelIndex index=table->indexAt(pos);
    QMenu *menu=new QMenu(this);
    menu->addAction(new QAction("Add", this));
    menu->addAction(new QAction("delete", this));
    //menu->addAction(new QAction("refresh", this));
    void Widget::customHeaderMenuRequested(QPoint pos){
    int column=table->horizontalHeader()->logicalIndexAt(pos);
    QMenu *menu=new QMenu(this);
    menu->addAction(new QAction("ID", this));
    menu->addAction(new QAction("Name", this));
    menu->addAction(new QAction("Contact_No", this));
    #if 0
    void SomeDialog::itemClicked(QTableWidgetItem* item) // or any other slot
      QMenu* contextMenu = new QMenu ( this );
      Q_CHECK_PTR ( contextMenu );
      contextMenu->addAction ( "New" , this , SLOT (newUnitBtnSlot()) );
      contextMenu->addAction ( "Clone" , this , SLOT (cloneUnitBtnSlot()) );
      contextMenu->popup( QCursor::pos() );
      contextMenu->exec ();
      delete contextMenu;
      contextMenu = 0;
    void someWidget::someWidgetContextMenu(QPoint pos)
        QMenu menu;
        delete ui;
    void Widget::on_pushButton_clicked()

    [edit: Add missing coding tags SGaist]

  • @SGaist
    Hi SGaist, plz can u see my code and guide me in right direction .....
    WIth regards

  • @veera
    Please post new question for your problem. Do not try to use others topic .

  • @veera
    I am creating the table for context menu like add delete after right clicking the mouse button got the options as add delete i need to save the data how should i do it......

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    First thing, like @Ratzz wrote, don't highjack other people thread with unrelated questions. That's rude. I've created a new thread for your question.

    Second, don't ping your own questions so soon, allow at least 24 hours before doing that. This is a community forum and people answering here are not all living in the same time zone as you.

    Then, what issue do you have exactly ? Currently, it looks like you are asking for someone to review your code and write the solution.

    Also, at least part of the topics you are asking about are covered by the documentation:

    Did you take a look at them ?

Log in to reply