[Solved] Qt Wizard and WizardPages UI Inheritance



  • Hello,

    I’m new with Qt and I want to have some ui inheritance. I know that I can:

    1.- Create a Father.h who inherites from QWidget.
    2.- Put a button on Father ui.
    3.- Create a Child1.h who inherites from QWidget but then, manually put the inheritance from Father (class Child1: public Father).
    4.- on main.cpp throw child class and it appears with the button put on step 2.

    Here its all okey.

    Now what I want its quite similar but with a Wizard structure with WizardPages. I need about 20 pages and ALL of them have to have 2 labels. The first one its always the same with the same text. The second one, its text change according to the wizardpage where the user is. What I do is:

    1.- Create a wizard class (mywizard).
    2.- create a qwidget class (named Form1) and make manually an inheritance to mywizard ( class Form1 : public mywizard).
    3.- put a button and a label on Form1 ui.
    4.- create a wizard page (named wp1) and make th einheritance (class wp1 : public Form1)

    What I see then its the wizard with the wizard page, but no button, or label or nothing.

    Thank you for all,


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Since you are creating a wizard, why not use "QWizard":http://qt-project.org/doc/qt-5/qwizard.html and "QWizardPage":http://qt-project.org/doc/qt-5/qwizardpage.html ?



  • Hi SGaist,

    Thank you for your welcome and your answer.
    As I said with the second case I tried it but i don't see how can I have a single label for each QWizardPage, because I need one label that shows ''Exercices'' always in all the WizardPages and a second label that changes depending the wizard page (for example: "Maths", "social", "social", "history"...).

    If I put a label in the QWizard class It doesn't appear on the QWizardPages, and if I put it on the fisrt QWizardPage, the label just appears on that page... And that is why I tried to use a middle class to inherit from it, but I got the same result.

    Regards!


  • Lifetime Qt Champion

    Subclass QWidgetPage, put your two labels in it and add a setter that you will use to put whatever your want it your second label.



  • Hi,
    I didn't know there were a QWidgetPage and I've been searching about it. What do you mean when you say "add a setter" ? To use the connect() function?
    Despite that, I think that I need a QWizard -QWizardPages structure because I'm trying to do a configuration windows application, more-or-less linear and with its buttons (Prev, Next, etc).

    Isn't there any way of declare just once a

    @ QLabel* mylabel = new QLabel("This text will be the same in every wizardpage"); @

    and then the second label

    @ QLabel* sublabel = new QLabel("This text will change"); @
    so in the Page1 I can do something like:
    @sublabel->SetText("First Page"); @

    which will show "First Page" while you will see "Second Page" if you go to the Page2 if a put the next code on it:
    @sublabel->SetText("Second Page"); @

    Maybe it's easier to understand what I need with that example.

    Thank you!


  • Lifetime Qt Champion

    @
    class MyPage: public QWizardPage
    {
    public:
    MyPage(QWidget *parent = 0);
    void setSubLabelText(const QString& text);

    private:
    QLabel *_subLabel;
    }

    MyPage::MyPage(QWidget *parent):
    QWizardPage(parent),
    _subLabel(new QLabel)

    {
    QLabel *mainLabel = new QLabel(tr("Common Text to all pages"));
    QVBoxLayout *layout = new QVBoxLayout(this);
    layout->addWidget(mainLabel);
    layout->addWidget(_subLabel);
    }

    void MyPage::setSubLabelText(const QString& text)
    {
    _subLabel->setText(text);
    }

    main.cpp:
    int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWizard wizard;

    MyPage *page1 = new MyPage;
    page1->setSubLabelText("Text1");
    wizard.addPage(page1);

    MyPage *page2 = new MyPage;
    page2->setSubLabelText("Text2");
    wizard.addPage(page2);

    wizard.show();

    return app.exec();
    };
    @

    [edit: corrected typo]



  • Wow!This solution works perfect!!! Thank you so much SGaist!! Now I understand the logic of Qt a little more :D

    One thing for those who need this information in the future: where there is
    @page1->setSubLabelText("Text2");@

    it should be
    @page2->setSubLabelText("Text2");@

    Ok, now I have another problem /question. As all pages are created from the same (MyPage) I can't have a pushbutton on the first one, no button on the second and a button with a different text in the third page unless I declare that button and its functions on MyPage as you can see here:

    @
    class MyPage: public QWizardPage
    {
    public:
    MyPage(QWidget *parent = 0);
    void setSubLabelText(const QString& text);
    void setbut1visible(const bool value);
    void setButText(const QString& text);
    //Here all the functions for every element AND property needed....
    private:
    QLabel *_subLabel;
    QPushButton *_but;
    };@

    @
    MyPage::MyPage(QWidget *parent):
    QWizardPage(parent),
    _subLabel(new QLabel)

    {
    QLabel *mainLabel = new QLabel(tr("Common Text to all pages"));
    QVBoxLayout *layout = new QVBoxLayout(this);
    _but = new QPushButton(this);
    layout->addWidget(mainLabel);
    layout->addWidget(_subLabel);
    layout->addWidget(_but);
    }

    void MyPage::setSubLabelText(const QString& text)
    {
    _subLabel->setText(text);
    }

    void MyPage::setbut1visible(const bool value)
    {
    _but->setVisible(value);
    }

    void MyPage::setButText(const QString& text)
    {
    _but->setText(text);
    }
    @

    @int main(int argc, char *argv[])
    {
    QApplication app(argc, argv);
    QWizard wizard;

    MyPage *page1 = new MyPage;
    page1->setSubLabelText("Text1");
    wizard.addPage(page1);
    
    MyPage *page2 = new MyPage;
    page2->setSubLabelText("Text2");
    page2->setbut1visible(false);
    wizard.addPage(page2);
    
    MyPage *page3 = new MyPage;
    page3->setButText("Push me");
    page3->setSubLabelText("Text3");
    wizard.addPage(page3);
    
    wizard.show();
    
    return app.exec();
    

    }
    @

    That can works for 1 button but in my case I need pages with 30 buttons, others with just a label and others with elements such as line edits (1 or 20 or....) so... do I have to declare all that elemnts in MyPage or is there any other solution for it? Because it will be really ugly to have something like:

    @
    class MyPage;
    }

    class MyPage: public QWizardPage
    {
    public:
    MyPage(QWidget *parent = 0);
    void setSubLabelText(const QString& text);
    void setbut1visible(const bool value);
    void setButText(const QString& text);
    private:
    QLabel *_subLabel;
    QPushButton *_but2;
    QPushButton *_but3;
    QPushButton *_but4;
    QPushButton *_but5;
    QPushButton *_but6;
    QPushButton *_but7;
    QPushButton *_but8;
    QPushButton *_but9;
    QPushButton *_but10;
    QPushButton *_but11;
    [...]
    QLineEdit *m_line1;
    QLineEdit *m_line2;
    QLineEdit *m_line3;
    QLineEdit *m_line4;
    QLineEdit *m_line5;
    QLineEdit *m_line6;
    [...]
    @

    So.... is there any solution more nice than that?

    Thank you :)


  • Lifetime Qt Champion

    You should subclass QWizardPage/MyPage further to adapt it to your need. There's no reason to pollute your base class for one page that will be different.

    What will you need to do with all these buttons and line edits ? When the numbers of widget get's that high you should rather consider using containers than one member variable for each of them.



  • That is what I was trying to do but I couldn't succeed (to have a class page3 : public MyPage). Dunno what I was doing wrong but now it works :)

    Well maybe not 30 but 10-15 yes XD.

    Thank you so much for your time SGaist! :D


  • Lifetime Qt Champion

    You're welome !

    Even for 10-15, it's still valid

    You're welcome, if that answers your questions, then please update the thread title prepending [solved] so other forum users may know a solution has been found :)



  • Done, thank you!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.