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); table->setModel(model); table->setContextMenuPolicy(Qt::CustomContextMenu); //connect(table, SIGNAL(customContextMenuRequested(QPoint)),SLOT(customMenuRequested(QPoint))); connect(this, SIGNAL(customContextMenuRequested(const QPoint &pos)), this, SLOT(onRightClick(const QPoint &pos))); table->horizontalHeader()->setContextMenuPolicy(Qt::CustomContextMenu); connect(table->horizontalHeader(), SIGNAL(customContextMenuRequested(QPoint)), SLOT(customHeaderMenuRequested(QPoint))); l->addWidget(table); } 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)); menu->popup(table->viewport()->mapToGlobal(pos)); } 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)); menu->popup(table->horizontalHeader()->viewport()->mapToGlobal(pos)); } #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; menu.addAction(someActionCreatedByYou1); menu.addAction(someActionCreatedByYou2); menu.exec(QCursor::pos()); } #endif Widget::~Widget() { delete ui; } void Widget::on_pushButton_clicked() { }
[edit: Add missing coding tags SGaist]
-
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:
- Model View
- Model View Programming
- The related examples
Did you take a look at them ?