Unsolved QDialog and Inheritance
-
I have a custom
QDialog
- for names sake lets call thisBaseDialog
.This
BaseDialog
is set out using a vertical layout and there are 3 widgets present: a top widget containing a custom dialog title information, a bottom widget containing custom dialog buttons and a central widget which fills the remaining space on the form.The central widget is where I need assistance.
What I want to achieve is this: I want to be able to inherit from this
BaseDialog
and be able to replace the contents of its central widget with the content of the derived dialog.At the moment, if I create a new
QDialog
but derive it from myBaseDialog
class, when I add things likeQPushButton
widgets to the form, at runtime if I'm not careful with placement, they will overlap the widgets which are part ofBaseDialog
.Is there a convenient way for me to achieve this?
UPDATE
What I've done so far is this:
Added a
protected
member to theBaseDialog
class, exposing the central widget as follows:BaseDialog *BaseDialog::centralWidget() { return (BaseDialog *)ui->centralWidget; }
The derived class in its constructor, instead of calling
ui->setupUi(this);
it callsui->setupUi(this->centralWidget());
and so far, it seems fine. The only thing that doesn't seem to work is the resizing of theBaseDialog
to accomodate the derived dialogs size. -
Hi,
you should use layouts - this way UI will take care of spacing and margins between widgets. -
This post is deleted! -
@artwaw Thanks for your reply.
Please can you elaborate on how
layouts
will help me in my situation? I'm already using layouts in my dialog and widget designs but I don't see how I can use them to achieve what I want as per above. -
If the central widget and layout for this dialog was accessible to derived classes this would work. You would need to make sure that all subclass's of your basedialog can access this widget and layout (central widget and layout declared as protected and not private).
Doing it this way might be a bad idea. It requires your subclass's to know something about the parent in order to work. It might be better to create a unique central widget instead and only use one basedialog class. Just a thought.
-
Assuming the layout is set properly and there is enough space any call to layouts setItem or similar should result in proper placement, spacing and margins being set.
-
@Rondog Thanks for your response. I've just updated my original post - is this along the lines of what you were meaning?
-
@artwaw Ah ok, I think I'm following.
So I think what you're saying is create an instance of
BaseDialog
but then callsetItem
passing in the instance of the content of the derived form to fill the layout? -
@webzoid More or less, yes. If you know what size the dialog should have after the change you can always call resize() to set the expected size.
Going further I think that in theory you could compute expected size based on sizeHint() methods of the child items however I never did that personally.