बिल्डिंग @ जानकार के जवाब पर, मैं मेरा कुछ सी ++ 11 विशेषताएं (lambdas और ऑटो का उपयोग कर एक उपकरण पट्टी बटन के बंद एक पॉपअप मेनू बनाया; VS2010 और जीसीसी 4.6 के साथ काम करता क्यूटी 5.1.1) के साथ:
void MyQAction::setPopupDialog(QDialog* dialog) {
QWidgetAction* action = new QWidgetAction(NULL);
action->setDefaultWidget(dialog);
QMenu* menu = new QMenu();
menu->addAction(action);
// Fix issues related to the way the dialogbox hide/show. Restablish proper handling,
// based on our requirement.
connect(menu, SIGNAL(aboutToShow()), dialog, SLOT(show()));
connect(dialog, SIGNAL(finished(int)), menu, SLOT(hide()));
setMenu(menu);
}
इस के लिए प्रक्रिया को स्वचालित करेगा:
auto dialog = new QColorDialog();
dialog->setWindowFlags(Qt::Widget);
dialog->setOptions(QColorDialog::DontUseNativeDialog | QColorDialog::ShowAlphaChannel);
auto action = new QWidgetAction(0);
action->setDefaultWidget(dialog);
auto menu = new QMenu();
menu->addAction(action);
// The dialog-as-widget closes on Ok/cancel, but the menu that holds it
// doesn't. We connect the two here. Because the dialog hides itself,
// we need to reshow it when the menu is coming up again.
connect(menu, &QMenu::aboutToShow, [=] { dialog->show(); });
connect(dialog, &QColorDialog::rejected, [=] { menu->hide(); });
connect(dialog, &QColorDialog::colorSelected,
[=](const QColor& color)
{
menu->hide();
OnFillColorChanged(color); // Call the "slot" in this class
});
auto button = new QToolButton();
button->setIcon(QIcon(":/images/whatev.png"));
button->setText(tr("Fill"));
button->setStatusTip(tr("Choose fill color"));
button->setMenu(menu);
button->setPopupMode(QToolButton::InstantPopup);
button->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
toolbar->addWidget(button); // toolbar is defined elsewhere
यह संभव है। नीचे @ विज़ का समाधान देखें। – metal