PDA

View Full Version : QTreeWidget and customContextMenuRequested



seneca
17th January 2006, 18:49
I'm trying to create a custom popup menu in a tree widget depending on what item was right-clicked. However currently I'm stuck because the signal customContextMenuRequested seems not to get fired. Is there something else besides setContextMenuPolicy to make the signal work?


AMainView::AMainView(QWidget *parent) : QMainWindow(parent)
{
....
iTree = new QTreeWidget();
iTree->setColumnCount(1);
iTree->setContextMenuPolicy(Qt::CustomContextMenu);
iTree->header()->hide();
connect(
iTree, SIGNAL(customContextMenuRequested(const QPoint& aPosition)),
this, SLOT(treeContextMenu(const QPoint& aPosition))
);
....
}

// declared under public slots in amainview.h:
void AMainView::treeContextMenu(const QPoint& aPosition)
{
QMessageBox::warning(
this, windowTitle(),
QString().sprintf("x=%d y=%d", aPosition.x(), aPosition.y())
);
}

jacek
17th January 2006, 20:02
connect(
iTree, SIGNAL(customContextMenuRequested(const QPoint& aPosition)),
this, SLOT(treeContextMenu(const QPoint& aPosition))
);
You can't put parameter names in SLOT and SIGNAL macros.

This should be:

connect( iTree, SIGNAL( customContextMenuRequested( const QPoint& ) ),
this, SLOT( treeContextMenu( const QPoint& ) ) );

seneca
17th January 2006, 20:10
Thanks jacek, I figured out that this here works perfectly:


connect(
iTree, SIGNAL(customContextMenuRequested(QPoint)),
this, SLOT(treeContextMenu(QPoint))
);