Open a pop up menu when right click on any cell of the table
Hi everyone...
I want to know that how can i open a new pop up menu when i right click on the table items.In the pop up menu some actions like add, delete,update etc should be given which will create a new row or delete the selected row.I am a newbie in QT so i does not have any idea of QT so if anybody can give me full details(with code if possible) then i will bw really grateful towards him/her.
Thank you.
Re: Open a pop up menu when right click on any cell of the table
A popup menu in Qt is called a context menu.
Implement the context menu events and signals.
Re: Open a pop up menu when right click on any cell of the table
Quote:
Originally Posted by
tbscope
A popup menu in Qt is called a context menu.
Implement the context menu events and signals.
Thank you for your reply but that i already know...Now what i want to know is how to implement that to my table so that when any cell is right clicked it should show me the options of add, delete etc
Re: Open a pop up menu when right click on any cell of the table
QTableWidgetItem * itemAt ( const QPoint & point ) const
Code:
{
connect(pAddAction,SIGNAL(triggered()),this,(slotAdd()));
connect(pRemoveAction ,SIGNAL(triggered()),this,(slotRemove()));
connect(pUpdateAction ,SIGNAL(triggered()),this,(slotUpdate()));
//
pContextMenu->addAction(pAddAction);
pContextMenu->addAction(pRemoveAction );
pContextMenu->addAction(pUpdateAction );
//
pContextMenu->exec( e->globalPos() );
delete pContextMenu;
pContextMenu = NULL;
}
Re: Open a pop up menu when right click on any cell of the table
Thank you very much for your valuable reply.....My problem is solved now with some of your help and some of mine coding.I am pasting the code also so that it can help others also...
First create a signal and slot in main program like this
connect(ui->tableWidget,SIGNAL(customContextMenuRequested(con st QPoint &)),this,SLOT(ProvideContextMenu(const QPoint &)));
and then just create the slots functions for them like this....
void MainWindow::ProvideContextMenu(const QPoint &pos)
{
item = ui->tableWidget->itemAt(pos);
// QPoint globalPos = ui->tableWidget->mapToGlobal(pos);
QAction *pAddAction = new QAction("Add",ui->tableWidget);
connect(pAddAction,SIGNAL(triggered()),this,SLOT(n ewRow()));
QAction *pRemoveAction = new QAction("Remove",ui->tableWidget);
connect(pRemoveAction ,SIGNAL(triggered()),this,SLOT(deleteRow()));
QAction *pUpdateAction = new QAction("Update",ui->tableWidget);
//connect(pUpdateAction ,SIGNAL(triggered()),this,SLOT(Update()));
QMenu *pContextMenu = new QMenu( this);
pContextMenu->addAction(pAddAction);
pContextMenu->addAction(pRemoveAction );
pContextMenu->addAction(pUpdateAction );
pContextMenu->exec( mapToGlobal(pos) );
delete pContextMenu;
pContextMenu = NULL;
}
void MainWindow::newRow()
{
int row = ui->tableWidget->rowCount();
ui->tableWidget->insertRow(row);
}
void MainWindow::deleteRow()
{
int row = ui->tableWidget->row(item);
ui->tableWidget->removeRow(row);
}
Do not forget to declare them in .h file or wherever you want....