PDA

View Full Version : QWidget as widget and as dialog



Ciurica
29th August 2012, 07:32
Hello guys,

I have a

class MyWidget : public QWidget
{
Q_OBJECT;
[...]
};

and I want to use this widget modal in one dialog and embedded in another bigger widget in the same time.
When I add this widget in another dialog everything is fine and looks great, but to display it as a popup in an single dialog I do:


MyWidget *myWidgetDlg = new MyWidget ();
addCompanyDlg->setWindowModality (Qt::WindowModal);

QDialog mainDialog;
mainDialog.setLayoutDirection (Qt::LayoutDirection::LeftToRight);

QBoxLayout mainDialogLayout(QBoxLayout::LeftToRight);
mainDialogLayout.addWidget(myWidgetDlg );
mainDialogLayout.setMargin (0);

mainDialog.setLayout(&mainDialogLayout);

mainDialog.setWindowState(mainDialog.windowState() | Qt::WindowMaximized);
mainDialog.setWindowFlags (Qt::Dialog);
mainDialog.exec();

This works on my computer on Linux (Ubuntu 12.4 x64), but on Windows (x64) and on my tester computer (Ubuntu 12.4 x86), the dialog is shown without borders and title bar is like an splash screen, not minimize and maximize buttons.

What is wrong in my dynamical QDialog creation?

Thanks in advance.

amleto
29th August 2012, 09:21
oh god, why are you mixing stack and heap allocation for (what will be) the same widget? **

If you're just going to make MyWidget a child of Dialog, then there is no point in setting the modality, is there?

As to your problem, it is probably down to state/flags/margin. Why not just play around with those and see what happens?

** edit:
after some thought maybe this is needed but not sure.

high_flyer
29th August 2012, 11:14
What is wrong in my dynamical QDialog creation?

Well, first, its not dynamic.
Your QWidget is dynamically allocated, but not your QDialog.



QDialog mainDialog;
MyWidget *myWidgetDlg = new MyWidget (&mainDialog); //mainDialog will take care of destroying your widget, avoiding a mem leak.

mainDialog.setWindowModality (Qt::WindowModal); //you want your dialog modal not your widget.

mainDialog.setLayoutDirection (Qt::LayoutDirection::LeftToRight);

QBoxLayout mainDialogLayout(QBoxLayout::LeftToRight);
mainDialogLayout.addWidget(myWidgetDlg );
mainDialogLayout.setMargin (0);

mainDialog.setLayout(&mainDialogLayout);

mainDialog.setWindowState(mainDialog.windowState() | Qt::WindowMaximized);
//mainDialog.setWindowFlags (Qt::Dialog); //Not needed. QDialog already sets this flag.
mainDialog.exec();

Ciurica
29th August 2012, 12:29
Hello,

the issue was in: mainDialog.setWindowModality (Qt::WindowModal); //you want your dialog modal not your widget.

Thanks for help

amleto
29th August 2012, 23:32
Well, first, its not dynamic.
Your QWidget is dynamically allocated, but not your QDialog.



QDialog mainDialog;
MyWidget *myWidgetDlg = new MyWidget (&mainDialog); //mainDialog will take care of destroying your widget, avoiding a mem leak.



parenting is changed when setting layouts so there is/was no leak anyway.