PDA

View Full Version : QTableWidget right click menu



stefan
23rd June 2008, 19:45
Hi.
Im starting to use Qt, and after installation/building/starting_project probelm i have a problem for which i can't find solution searching the forums..

I need to add a (right mouse button click) menu to QTableWidget, but i can't get it working.

frmSettings::frmSettings(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

context= new QMenu(this);
context->addAction("Add");
context->addAction("Delete");

connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(QPoint* position)), SLOT(frmSettings::ProvideContexMenu(position)));
}
void frmSettings::ProvideContexMenu(const QPoint* position)
{
context->exec(*position);
}

tableOptimizationVariables is QTableWidget

Thanx

jpn
23rd June 2008, 20:08
You cannot put variable names but just types inside SIGNAL() and SLOT() macros and there is no such signal as QWidget::customContextMenuRequested(QPoint*). It's a reference, not a pointer.

So it should be:

connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));

Also, did you switch the context menu policy to Qt::CustomContextMenu?

stefan
23rd June 2008, 20:21
thank you, but its not working.
Yes ContextMenuPolicy was set to CustomContextMenu in .ui file. I treid to do this in constructor but without success:
frmSettings::frmSettings(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);

context= new QMenu(this);
context->addAction("Add");
context->addAction("Delete");
ui.tableOptimizationVariables->setContextMenuPolicy(Qt::CustomContextMenu);
// connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(QPoint* position)),frmSettings, SLOT(ProvideContexMenu(position)));
connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));
}
void frmSettings::ProvideContexMenu(const QPoint* position)
{
context->exec(*position);
}

Can somebody give me complete code (it shoud be couple of lines) for calling context menu? Is it diferent for different controls? Maybe some QTableWidget-specific stuff must be done?

//LATER ADDED TEXT

I tried to link contect menu to QLineEdit control (to see if default copy/cut/paste menu is invoked):

ui.tableOptimizationVariables->setContextMenuPolicy(Qt::CustomContextMenu);
ui.txtName->setContextMenuPolicy(Qt::CustomContextMenu);
connect(ui.tableOptimizationVariables, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));
connect(ui.txtName, SIGNAL(customContextMenuRequested(const QPoint&)), SLOT(ProvideContexMenu(const QPoint&)));

So i lost "Copy/Cut/Paste/|/Select All" menu, which mean that context menu is set to custom? But when i debugged I noticed that slot void frmSettings::ProvideContexMenu(const QPoint* position) is never entered. What am I doing wrong?

jpn
23rd June 2008, 20:33
You forgot to adjust frmSettings::ProvideContexMenu(). It still takes a pointer.

The easiest way to construct a context menu is to use Qt::ActionsContextMenu. All it takes is to add actions via QWidget::addAction() and the menu is constructed automatically for you:


setContextMenuPolicy(Qt::ActionsContextMenu);
QAction* fooAction = new new QAction("foo");
QAction* barAction = new new QAction("bar");
connect(fooAction, SIGNAL(triggered()), this, SLOT(doSomethingFoo()));
connect(barAction, SIGNAL(triggered()), this, SLOT(doSomethingBar()));
addAction(fooAction);
addAction(barAction);

stefan
23rd June 2008, 20:38
You forgot to adjust frmSettings::ProvideContexMenu(). It still takes a pointer.

YES! ;) thank you. I'm used to .NET warnings for every litle thing.. so im kinda strugling with QT.. :)
Replies on my post where fast and exact :) nice forum guys!

jpn
23rd June 2008, 20:42
YES! ;) thank you. I'm used to .NET warnings for every litle thing.. so im kinda strugling with QT.. :)
Signal slot connections are established at run time, that's why there is no error/warning at compilation time. Notice that QObject::connect() outputs a detailed warning in the debug output in case establishing the connection fails.


Replies on my post where fast and exact :) nice forum guys!
Thanks, you're welcome. :)