The best way to inherit from form in qt [closed]
What is the most preferable way of inheriting from form created in qt designer?
There is no good direct way to inherit form itself, you'd better inherit class created for form.
class testBase : public QWidget
{
Q_OBJECT
public:
testBase (QWidget *parent = 0);
~testBase ();
protected: // here was private
Ui::testBaseClass baseUi; // rename this
};
testBase ::testBase (QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
}
also, if you want to add some other form, which is also possible, you should do some additional work:
1) Specify a placeholder for child ui in base class (some container)
2) create your child form with wizard. DO NOT pass your base class as ancestor, in wizard you should say that you inherit from QWidget.
3) after creating form for your derived class, rewrite derived to be descedant of your base.
change its constructor, line ui.setupUi(this)
should be changed to ui.setupUi(baseUi.placeholder)
class testDerived : public testBase
{
Q_OBJECT
public:
testDerived (QWidget *parent = 0);
~testDerived ();
private:
Ui::testDerivedClass ui;
};
testDerived::testDerived(QWidget *parent)
: testBase (parent)
{
ui.setupUi(baseUi.placeholder);
}
also note, that derived class does not change base class form, it extends it. You would not be able to add or remove items to base form in form constructor, but container you specify as placeholder will be filled with form data of derived class.
精彩评论