appending new widgets to dialog derived from base dialog
-
i have a base class for dialog, that adds widgets to layout:
class BaseDialog { public: BaseDialog(QWidgets *parent) { auto pLabel = new QLabel("base"); auto pLayout = new QVBoxLayout(this); pLayout->addWidget(pLabel); } };then i have a derived dialog class, which needs to add extra widgets to the dialog:
class DerviedDialog : public BaseDialog { public: DerviedDialog(QWidgets *parent) { auto pOtherLabel = new QLabel("derived"); auto pLayout = new QVBoxLayout(this); pLayout->addLayout(layout()); // <-- not possible! pLayout->addWidget(pLabel); } };here i am getting the error:
QWidget::setLayout: Attempting to set QLayout "" on MainWindow "", which already has a layouti know from the docs:
If there already is a layout manager installed on this widget, QWidget won't let you install another. You must first delete the existing layout manager (returned by layout()) before you can call setLayout() with the new layout.but i can't really delete the old layout, because i need to widgets added by base class too.
how should i fix this?
-
Hi,
Why not just add your widget to the current layout of the dialog ?
-
First - you're missing the call to the base constructor in your derived class, which means you're not passing the parent parameter down the inheritance chain. Also it should be QWidget not QWidgets.
As for the question - don't crate another layout. Just use the one already created by the base class by calling
layout()to get it. -
@SGaist thanks. when i add the widget directly in
layout()->addWidget(w);the new widget sits on top of other widgets (coming from base class). what i am doing wrong? -
What if you cast the layout to its original QVBoxLayout type ?