PDA

View Full Version : how to add context menu to the table widget header?



aurora
21st February 2012, 06:31
Hi i added context menu to a table widget...
But it is not displaying for table header...what changes i need to do?


my context menu function is:

void MainWindow::ProvideContextMenu(const QPoint &point)
{
QTableWidgetItem *item=filesTable->itemAt(point);



QPoint globalpos=filesTable->mapToGlobal(point);
QString cell=item->text();
cout<<"EVENT GENERATED..."<<cell.toStdString()<<endl;

QAction *pAddAction= new QAction("add",filesTable);
QAction *pSplitAction= new QAction("SPLIT",filesTable);
QMenu *pContextMenu = new QMenu( this);
pContextMenu->addAction(pAddAction);
pContextMenu->addAction(pSplitAction);
pContextMenu->exec(globalpos);

}

ChrisW67
21st February 2012, 07:02
what changes i need to do?
Since you code does not show us how you are summoning this menu it is hard to say.

You need to stop cross-posting the same problem... it only serves to spread effort. Information that is present in the other thread is not automagically available here.

myta212
21st February 2012, 10:21
Hi,
I am success to create contextmenu in QTableView. I think this is same with QTableWidget. You can use this

QHeaderView * QTableView::horizontalHeader () const

This is a sample code :


QTableView *tblv;
QHeaderView *horizontalHeader;

//connect horizontal header QTableView with contextmenu
horizontalHeader = tblv->horizontalHeader();
horizontalHeader->setContextMenuPolicy(Qt::CustomContextMenu); //set contextmenu
connect(horizontalHeader, SIGNAL(customContextMenuRequested( const QPoint& )),
this, SLOT(tablev_customContextMenu( const QPoint& )));


You can modified this with your problem. This site have a sample how to create contextmenu in QTableView. I think you can use that.
http://toto-share.com/2011/07/contextmenu-in-qtableview/

Best regards,
myta212