我想使用QColorDialog不是作为对话窗口,而是作为一个我可以插入布局的小部件。 (更具体地说,作为一个上下文菜单中的自定义子菜单)如何在另一个小部件中使用QColorDialog而不是作为单独的对话框?
我看着QColorDialog源代码,我可能复制QColorDialog的内部实现的一部分来实现这一点,但是有没有更清晰的方法做这个?我使用Qt 4.5.1 ...
我想使用QColorDialog不是作为对话窗口,而是作为一个我可以插入布局的小部件。 (更具体地说,作为一个上下文菜单中的自定义子菜单)如何在另一个小部件中使用QColorDialog而不是作为单独的对话框?
我看着QColorDialog源代码,我可能复制QColorDialog的内部实现的一部分来实现这一点,但是有没有更清晰的方法做这个?我使用Qt 4.5.1 ...
如果有办法做到这一点干净,我不知道它。在我看来,你有两个选择:
尝试继承QColorDialog
你可能想看看一些Qt的解决方案,这将做你想做的至少一部分。例如,请参阅Color Picker解决方案,他们注意到该解决方案现在也可以作为LGPL许可的库。我记得Qt实验室的一些关于将Qt小部件嵌入到QGraphicsScene
中的工作,包括QDialog
s。您可能会这样做,然后更改图形场景中的视图,以便只有用户感兴趣的颜色选择器对话框的一部分对用户可见。但是,这听起来很骇人听闻。
此解决方案不再有效。是否有更新的产品链接? – metal 2013-10-23 12:22:35
使用QGraphicsView
,并添加QDialog
它。如果你想显示对话框,并将QGraphicsView
添加到小部件。
您可以通过设置正确的窗口标志以非常简单的方式将其清理干净。
QColorDialog8 colorDialog = new ....
colorDialog->setWindowFlags(Qt::SubWindow);
QColorDialog是一个对话框,意味着它是一个小部件。您只需设置几个窗口标志并将其放入您的布局中即可。这里有一个(测试)例如:
#include <QApplication>
#include <QMainWindow>
#include <QColorDialog>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
/* setup a quick and dirty window */
QMainWindow app;
app.setGeometry(250, 250, 600, 400);
QColorDialog *colorDialog = new QColorDialog(&app);
/* set it as our widiget, you can add it to a layout or something */
app.setCentralWidget(colorDialog);
/* define it as a Qt::Widget (SubWindow would also work) instead of a dialog */
colorDialog->setWindowFlags(Qt::Widget);
/* a few options that we must set for it to work nicely */
colorDialog->setOptions(
/* do not use native dialog */
QColorDialog::DontUseNativeDialog
/* you don't need to set it, but if you don't set this
the "OK" and "Cancel" buttons will show up, I don't
think you'd want that. */
| QColorDialog::NoButtons
);
app.show();
return a.exec();
}
工作就像一个魅力对我来说。有关使其在菜单中工作的更多提示,请参阅下面的答案。 – metal 2013-10-22 17:13:12
大厦@奇才的答案,我做了我的弹出式菜单关闭使用一些C++ 11功能(lambda表达式和经销商的工具栏按钮;用VS2010和gcc 4.6工程使用Qt 5.1.1):
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
只需注意您的修改后的代码不再是Qt4,因为此问题带有标记。 – Wiz 2013-10-22 21:35:18
此基础上从“金属”前面的回答我建议你在派生类中的QAction的创建方法如下:
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);
}
这会为这个过程自动化任何对话框。
这是可能的。请参阅下面的@ Wiz解决方案。 – metal 2013-10-22 15:20:51