[Qt-interest] QWidgetAction stays visible after triggered() signal and trigger() slot.
John Weeks
john at wavemetrics.com
Thu Jul 14 17:48:08 CEST 2011
I was wondering if you ever found a solution to this problem.
I have a similar problem- a cascade of contextual menus, at the innermost level a QWidgetAction that displays a custom color picker inside the menus. After trying numerous variations, I finally concluded that the only solution was to have the widget inside the QWidgetAction emit a signal, connect that to a slot in the code that puts up the menu, and use that connection to call QMenu::close on the top-level menu. Here's a screen shot of the menus:
The essence of the code is like this:
In the constructor for the tool-bar-like window:
ColorPickerAction = new WMColorPickerAction(this);
connect(ColorPickerAction, SIGNAL(colorChanged(const QColor)), this, SLOT(onColorChanged(const QColor)));
CursorColorMenu.addAction(ColorPickerAction);
The color picker itself uses a QTableWidget with custom delegate. It emits a signal when a color cell is clicked:
void WMColorPickerWidget::onCurrentItemChanged(QTableWidgetItem * current, QTableWidgetItem * previous) {
Q_UNUSED(previous);
if (current) {
emit selectedColorChanged(current->backgroundColor()); // TO DO: Igor will need RGB16Color, not QColor
}
}
The toolbar-like window (InfoPanel) ultimately gets a signal from that:
void
InfoPanel::onColorChanged(const QColor theColor)
{
changeCursorColor(theColor, menuCursorNum);
TraceNameMenu->close();
}
TraceNameMenu is the top-level menu, the one with "Style" at the top.
Regards, John Weeks
> What I need is a QMenu that displays a button, and when I press the button,
> it triggers an action and goes away. I have managed all except the last
> part: the menu doesn't disappear (more specifically: the program blocks
> execution on QMenu::exec(), no matter if trigger() is executed or not.)
>
> What is the correct (!) way to let QWidgetAction behave like a simple
> QAction in QMenu (i.e. to remove the widget after triggering the action.)?
> Take the the example below as a starting point:
>
> class A : public QWidgetAction {
> public:
> A(QObject *parent = 0) : QWidgetAction(parent){
> QPushButton *b = new QPushButton("BUTTON");
> setDefaultWidget(b);
>
> connect(b, SIGNAL(clicked()),
> this, SLOT(trigger())); //Docs suggest executing this
> connect(b, SIGNAL(clicked()),
> this, SIGNAL(triggered()));
> }
> };
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110714/3b05c6ae/attachment.html
-------------- next part --------------
A non-text attachment was scrubbed...
Name: PastedGraphic-2.png
Type: image/png
Size: 39513 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110714/3b05c6ae/attachment.png
More information about the Qt-interest-old
mailing list