Reputation: 1887
I would like to use QColorDialog not as a dialog window but as a widget which I could insert into a layout. (More specifically as a custom sub menu in a context menu)
I looked into the QColorDialog sourcecode, and I could probably copy over a part of the internal implementation of the QColorDialog to achieve this, but is there a cleaner way to do this? I am using Qt 4.5.1...
Upvotes: 9
Views: 14429
Reputation: 1
Base on previous answer from "metal" i suggest you to create the following method in a derived class of 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);
}
this will automate the process for any dialog box.
Upvotes: 0
Reputation: 6332
Building on @Wiz's answer, I made mine a popup menu off of a toolbar button using some C++11 features (lambdas and auto; works with VS2010 and gcc 4.6 with 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
Upvotes: 0
Reputation: 2143
QColorDialog is a dialog which means IT IS a widget. All you need to do is set a few window flags and drop it into your layout as you wish. Here is a (tested) example:
#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();
}
Upvotes: 17
Reputation: 13
Use QGraphicsView
and add QDialog
to it. And add QGraphicsView
to the widget if you want to show the dialog.
Upvotes: 0
Reputation: 31
You can do it clean in a very simple way by setting right window flags.
QColorDialog8 colorDialog = new ....
colorDialog->setWindowFlags(Qt::SubWindow);
Upvotes: 3
Reputation: 917
If there's a way to do this cleanly, I'm not aware of it. As I see it, you have a couple of options:
Upvotes: -1
Reputation: 14941
You might want to look at some Qt Solutions, which will do at least part of what you want. For example, see the Color Picker solution, which they note is now available as an LGPL-licensed library also.
As an alternative (and probably less-supported) approach, I recall some work in the Qt-Labs about embedding Qt widgets, including QDialog
s, into a QGraphicsScene
. You could potentially do so, then change the view on your graphics scene so that only the portion of the color picker dialog you are interested in was visible to the user. It sounds very hackish, however.
Upvotes: 2