PDA

View Full Version : Open a pop up menu when right click on any cell of the table



katta_ashish
8th September 2010, 10:40
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.

tbscope
8th September 2010, 10:58
A popup menu in Qt is called a context menu.

Implement the context menu events and signals.

katta_ashish
8th September 2010, 12:36
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

kemp
8th September 2010, 12:57
QTableWidgetItem * itemAt ( const QPoint & point ) const




void MyTableWidget::contextMenuEvent( QContextMenuEvent * e )
{
QAction *pAddAction = new QAction("Add",this);
connect(pAddAction,SIGNAL(triggered()),this,(slotA dd()));
QAction *pRemoveAction = new QAction("Remove",this);
connect(pRemoveAction ,SIGNAL(triggered()),this,(slotRemove()));
QAction *pUpdateAction = new QAction("Update",this);
connect(pUpdateAction ,SIGNAL(triggered()),this,(slotUpdate()));
//
QMenu *pContextMenu = new QMenu( this);
pContextMenu->addAction(pAddAction);
pContextMenu->addAction(pRemoveAction );
pContextMenu->addAction(pUpdateAction );
//
pContextMenu->exec( e->globalPos() );

delete pContextMenu;
pContextMenu = NULL;
}

katta_ashish
9th September 2010, 07:06
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(cons t 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....