开发者

The best way to inherit from form in qt [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its开发者_Python百科 current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜