PDA

View Full Version : QDialog not centering when showEvent() implemented



CoderMan
23rd February 2012, 21:45
I have several dialogs subclassed from QDialog and all are created and shown the same way:
SomeDialog dlg(this);
dlg.exec();

However, only one of the dialogs behaved in the way I expected based on the reference: "A dialog is always a top-level widget, but if it has a parent, its default location is centered on top of the parent's top-level widget (if it is not top-level itself)." The others appeared in more or less random locations. After some effort I found out the proximate cause: the randomly appearing ones overrode QDialog::showEvent(QShowEvent* event) while the properly appearing dialog did not.

I worked around this by deleting the showEvents from the offending classes, but supposing that I do need to do something in that function, how do I replicate the desired default behaviour regarding positioning?

mentalmushroom
24th February 2012, 08:12
I suppose there is some mistake in the overridden method.

Do it this way:


class MyDialog: public QDialog
{
protected:
void showEvent(QShowEvent *e)
{
// some custom code

// call inherited method
QDialog::showEvent(e);
}
}

CoderMan
24th February 2012, 20:37
Ah! Merely calling accept() for the event is not enough: you have to manually call QDialog's showEvent.