PDA

View Full Version : context menu not displaying



aurora
16th February 2012, 06:34
I created a table inside a function and connect as below...

filesTable= new QTableWidget(this);

connect(filesTable,SIGNAL(customContextMenuRequest ed(QPoint)),this,SLOT(ProvideContextMenu(QPoint))) ;

but, it is not generating signal, i am unable too see any context menu over there...

AlexSudnik
16th February 2012, 08:29
customContextMenuRequested() signal is only emitted with ContextMenuPolicy set to Qt::CustomContextMenu , so you

need to add this:

filesTable->setContextMenuPolicy(Qt::CustomContextMenu);

*and why did you leave a whitespace in "SIGNAL(customContextMenuRequest_ed(QPoint)

aurora
16th February 2012, 09:01
Thank u so much Alex...
But problem is that context menu is not displaying where mouse pointer is...It is displaying somewhere else..
What changes i need to do?
my code as shown below...




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

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(mapToGlobal(pos));

}

AlexSudnik
16th February 2012, 10:00
Coordinate transformations might be tricky...Here is a code snippet i used long time ago...and the thing is that i'm not sure if i figured it out completely back then...cuz i had the same problem.



//..

view = new QTableView;

// ...

void Widget::customContextMenuRequested(const QPoint &point)
{

QModelIndex index = view->indexAt(point);

if( index.isValid() )
{

QPoint viewPortPosition = view->pos();

int headerOffset = view->horizontalHeader()->sectionSize(0);

int adjustedHeight = viewPortPos.y() + headerOffset;

viewPortPos.setY(adjustedHeight);

QMenu menu;

// ...

menu.exec( mapToGlobal(QPoint( viewPortPos + point ) ) );

}

}



Even thought i've used QTableView , i'm sure it's gonna give you an idea...If you find that there is something wrong with my implementation (i'm sure there is:D) , i'd be glad to hear the right way to do it!^^

aurora
21st February 2012, 05:56
hmmm...not working...:(

ChrisW67
21st February 2012, 06:45
*and why did you leave a whitespace in "SIGNAL(customContextMenuRequest_ed(QPoint)
The space is inserted by the forum to break up long "words" when the poster cannot be bothered to use
tags around code.

AlexSudnik
21st February 2012, 07:00
You still have it appearing in the wrong space ? If so...you just want to use QTableWidget's context menu , right ?

P.S : ChrisW67 - thanks^^ , i see now^^

aurora
21st February 2012, 08:10
ya right now i'm using QTable widget but thinking to change it table view.....

thomas@itest
21st February 2012, 09:54
Hi !
I use :

menu.exec(QCursor::pos())

AlexSudnik
21st February 2012, 10:22
Hmm , try this:




void Widget::ProvideContextMenu( const QPoint& position )
{

QModelIndex index = indexAt(position);

if( index.isValid( ) ) // not neccessary , if you're about to perform item-independent actions
{

QMenu menu;


QAction menuAction(this);

menuAction.setText("Demonstrational Text");


menu.addAction(&menuAction);

menu.exec( mapToGlobal( position ) );


}


}



Since initial point coordinates are given in the widget's local space (i mean , not in QWidget * QAbstractScrollArea::viewport () coordinates ) , we have to display it in the same space , using mapToGlobal function.