PDA

View Full Version : correctly embed QColorDialog in QWidget



Ashish
11th July 2019, 16:33
Hi,

I am trying to embed QColorDialog in a QWidget. After I add the QColorDialog, it shows up correctly, but the layouting doesn't work correctly.
When I stretch the QWidget by dragging from the bottom right corner, the QColorDialog doesn't occupy the entire resized QWidget. It only stays in the center.
The minimal code to reproduce this is as follows:



#include <QApplication>
#include <QWidget>
#include <QColorDialog>
#include <QHBoxLayout>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QWidget *w = new QWidget(nullptr);

QColorDialog *cDialog = new QColorDialog(w);
cDialog->setWindowFlags(Qt::Widget);
cDialog->setOptions(QColorDialog::DontUseNativeDialog
| QColorDialog::NoButtons);

QHBoxLayout *hl = new QHBoxLayout(w);
hl->addWidget(cDialog);
w->setLayout(hl);

w->show();
return a.exec();
}


If I replace the QColorDialog with QFontDialog, this works correctly. Please see the attached screenshot that shows the issue
13186

Can anyone help me fix the issue?

Thanks,
Ashish

anda_skoa
12th July 2019, 08:51
What is the resize behavior of the dialog content when you use the dialog directly?
Maybe its layout is such that the content stays centered.

Checking the code (https://code.woboq.org/qt5/qtbase/src/widgets/dialogs/qcolordialog.cpp.html#_ZN19QColorDialogPrivate11in itWidgetsEv) it has a fixed size.

Cheers,
_

Ashish
12th July 2019, 20:37
Indeed, it has a fixed size constraint.
Any hints on how to achieve resizing without having to rewrite the entire class again?

Thanks,
Ashish

anda_skoa
13th July 2019, 08:46
I would try unsetting the size constraint of the dialog's layout(), i.e. either setting it to QLayout::SetNoConstraint or QLayout::SetDefaultConstraint

Cheers,
_

Ashish
15th July 2019, 22:15
Unfortunately, both SetNoConstraint and SetDefaultConstraint do not work. Please see the attached screenshot. There is a spacer in the original dialog that creates the problem.
13190

Thanks,
Ashish