PDA

View Full Version : how to show popup on mouse right click on a cell of table widget



pratyaksh
1st February 2008, 04:25
i have a table widget placed on the widget, i want to show popup on mouse right click on a cell of table widget, the code is workin fine but the popup is not confined only to the table widget it is also generating all over the widget i.e. either it is push button or anywhere

so need some help regarding this.............................................. ..........:confused:

aamer4yu
1st February 2008, 09:34
Can we see the code ?
You seem to be using a menu pointer from various places. Without code its hard to say what wrong are u doing.

jpn
1st February 2008, 09:44
It's called "context menu". See this post (http://www.qtcentre.org/forum/f-newbie-4/t-different-pop-up-menus-wanted-1710.html#4). Same approach works for table widget.

pratyaksh
4th February 2008, 04:55
#include<QtGui>
#include<QPushButton>
#include"newform.h"
#include<iostream>
#include<QString>
#include<QTableWidget>
#include<QWidget>
#include<QAction>
#include<QMenu>
#include<Qt>

using namespace std;


Form::Form(QWidget *parent):QWidget(parent)
{

setupUi(this);
//connect(this,SIGNAL(rightClick()),this,SLOT(itemCl icked()));

//connect(this,SIGNAL(rc(QMouseEvent *)),tableWidget,SLOT(mousePressEvent(QMouseEvent *)));
//connect(this,SIGNAL(cellClicked(int,int)),this,SLO T(mousePressEvent(QMouseEvent *)));
cout<<"\nconstructor called\n";
// if(count==1)
//{
// Form::createActions();
// Form::setupContextMenu();
//}
//setCentralWidget(table);
//mousePressEvent(e);
}
/*Form::Form(int n)
{
setupUi(this);
count=n;
// connect(tableWidget,SIGNAL(cellClicked(int,int)),t his,SLOT(mouseClick()));
if(count==1)
{
Form::createActions();
Form::setupContextMenu();
}
}*/
void Form::mouseClick()
{
// count=1;
// Form fm;
Form::createActions();
Form::setupContextMenu();
}

void Form::display()
{

int no_of_row;
int no_of_column;
cout<<"\n"<<"test56767"<<endl;
no_of_column = tableWidget->columnCount ();
no_of_row = tableWidget->rowCount () ;
cout<<"\n ROW "<<no_of_row<<endl;
cout<<"\n Column "<<no_of_column<<endl;

//tableWidget->clear ();
//tableWidget->removeColumn(no_of_column);
for(int k=no_of_row;k>=0;k--)
{
cout<<"\t " <<k;
tableWidget->removeRow (k);

}
cout<<"\n";
for(int j=0;j<100;j++)
{
tableWidget->insertRow(j);
for(int i=0;i<20;i++)
{
QTableWidgetItem *newItem = new QTableWidgetItem();
if(i==0)
{
newItem->setText("0");
tableWidget->setItem(j,i,newItem);
}
if(i==1)
{
newItem->setText("1");
tableWidget->setItem(j,i,newItem);
}
if(i==2)
{
newItem->setText("2");
tableWidget->setItem(j,i,newItem);
}
if(i==3)
{
newItem->setText("3");
tableWidget->setItem(j,i,newItem);
}
if(i==4)
{
newItem->setText("4");
tableWidget->setItem(j,i,newItem);
}
if(i==5)
{
newItem->setText("5");
tableWidget->setItem(j,i,newItem);
}
if(i==6)
{
newItem->setText("6");
tableWidget->setItem(j,i,newItem);
}
if(i==7)
{
newItem->setText("7");
tableWidget->setItem(j,i,newItem);
}
if(i==8)
{
newItem->setText("8");
tableWidget->setItem(j,i,newItem);
}
if(i==9)
{
newItem->setText("9");
tableWidget->setItem(j,i,newItem);
}
if(i==10)
{
newItem->setText("10");
tableWidget->setItem(j,i,newItem);
}
if(i==11)
{
newItem->setText("11");
tableWidget->setItem(j,i,newItem);
}
if(i==12)
{
newItem->setText("12");
tableWidget->setItem(j,i,newItem);
}
if(i==13)
{
newItem->setText("13");
tableWidget->setItem(j,i,newItem);
}
if(i==14)
{
newItem->setText("14");
tableWidget->setItem(j,i,newItem);
}
if(i==15)
{
newItem->setText("15");
tableWidget->setItem(j,i,newItem);
}
if(i==16)
{
newItem->setText("16");
tableWidget->setItem(j,i,newItem);
}
if(i==17)
{
newItem->setText("17");
tableWidget->setItem(j,i,newItem);
}
if(i==18)
{
newItem->setText("18");
tableWidget->setItem(j,i,newItem);
}
if(i==19)
{
newItem->setText("19");
tableWidget->setItem(j,i,newItem);
}
}

}
//emit rightClick(tableWidget->grabMouse());
}
void Form::mclick()
{
QMenu *contextMenu = new QMenu ( this );
QMenu *subMenu = new QMenu ( this );
Q_CHECK_PTR (contextMenu);
Q_CHECK_PTR (subMenu);

subMenu=contextMenu->addMenu ( "Math Opr.");

subMenu->addAction(cell_addAction);
subMenu->addAction(cell_subAction);
subMenu->addAction(cell_mulAction);
subMenu->addAction(cell_divAction);
subMenu->addAction(cell_sumAction);
contextMenu->addAction(firstSeparator);
contextMenu->addAction(colorAction);
contextMenu->addAction(fontAction);
contextMenu->addAction(secondSeparator);
contextMenu->addAction(clearAction);

pb3->setMenu(contextMenu);

//contextMenu->popup(QCursor::pos());
//contextMenu->exec ();

//setContextMenuPolicy(Qt::ActionsContextMenu);

}

void Form::del()
{
{ delete cell_addAction;
delete cell_subAction;
delete cell_mulAction;
delete cell_divAction;
delete cell_sumAction;
delete firstSeparator;
delete colorAction;
delete fontAction;
delete secondSeparator;
delete clearAction;
}

}

void Form::mousePressEvent(QMouseEvent *event)
{
if(count==1)
{
del();
count=0;
}
// cout<<"\n"<<"testmousepressevent455"<<endl;
if(event->button() == Qt::LeftButton)
{
//QString test = QString(event->x()) + ", " + QString(event->y());
QMessageBox::warning( this, tr("Position"), tr( "Left Button Pressed" ) );
}

else if(event->button() == Qt::RightButton)
{
//emit rightClick();
//setupContextMenu();
// Form frm;
QMessageBox::warning( this, tr("Position"), tr( "Right Button Pressed" ) );
}

}




void Form::createActions()
{
count =1;
cell_sumAction = new QAction(tr("Sum"), this);
connect(cell_sumAction, SIGNAL(triggered()), this, SLOT(actionSum()));

cell_addAction = new QAction(tr("&Add"), this);
cell_addAction->setShortcut(Qt::CTRL | Qt::Key_Plus);
connect(cell_addAction, SIGNAL(triggered()), this, SLOT(actionAdd()));

cell_subAction = new QAction(tr("&Subtract"), this);
cell_subAction->setShortcut(Qt::CTRL | Qt::Key_Minus);
connect(cell_subAction, SIGNAL(triggered()), this, SLOT(actionSubtract()));

cell_mulAction = new QAction(tr("&Multiply"), this);
cell_mulAction->setShortcut(Qt::CTRL | Qt::Key_multiply);
connect(cell_mulAction, SIGNAL(triggered()), this, SLOT(actionMultiply()));

cell_divAction = new QAction(tr("&Divide"), this);
cell_divAction->setShortcut(Qt::CTRL | Qt::Key_division);
connect(cell_divAction, SIGNAL(triggered()), this, SLOT(actionDivide()));

fontAction = new QAction(tr("Font..."), this);
fontAction->setShortcut(Qt::CTRL|Qt::Key_F);
connect(fontAction, SIGNAL(triggered()), this, SLOT(selectFont()));

colorAction = new QAction(QPixmap(16, 16), tr("Background &Color..."), this);
connect(colorAction, SIGNAL(triggered()), this, SLOT(selectColor()));

clearAction = new QAction(tr("Clear"), this);
clearAction->setShortcut(Qt::Key_Delete);
connect(clearAction, SIGNAL(triggered()), this, SLOT(clear()));


exitAction = new QAction(tr("E&xit"), this);
connect(exitAction, SIGNAL(triggered()), qApp, SLOT(quit()));

firstSeparator = new QAction(this);
firstSeparator->setSeparator(true);

secondSeparator = new QAction(this);
secondSeparator->setSeparator(true);


}

void Form::actionSum()
{

int row_first = 0;
int row_last = 0;
int row_cur = 0;

int col_first = 0;
int col_last = 0;
int col_cur = 0;


}



void Form::actionMath_helper(const QString &title, const QString &op)
{

QString cell1 = "C1";
QString cell2 = "C2";
QString out = "C3";
//QTableWidgetItem *current = tableWidget->currentItem();

}


void Form::actionAdd()
{
actionMath_helper(tr("Addition"), "+");
}

void Form::actionSubtract()
{
actionMath_helper(tr("Subtraction"), "-");
}

void Form::actionMultiply()
{
actionMath_helper(tr("Multiplication"), "*");
}

void Form::actionDivide()
{
actionMath_helper(tr("Division"), "/");
}


void Form::clear()
{
foreach (QTableWidgetItem *i, tableWidget->selectedItems())
i->setText("");
}


void Form::setupContextMenu()
{


addAction(cell_addAction);
addAction(cell_subAction);
addAction(cell_mulAction);
addAction(cell_divAction);
addAction(cell_sumAction);
addAction(firstSeparator);
addAction(colorAction);
addAction(fontAction);
addAction(secondSeparator);
addAction(clearAction);
setContextMenuPolicy(Qt::ActionsContextMenu);
}

jpn
4th February 2008, 06:16
You didn't check out the example, did you?

Please use [code]-tags for small code blocks
Please attach large code blocks as files
Please post only parts of code that are relevant to the problem