Results 1 to 6 of 6

Thread: QWizard creating objects dynamically

  1. #1
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QWizard creating objects dynamically

    Hi everyone,

    I'm trying to build a wizard in which I specify a number of something, so that when I press next I should get that number of objects in the next screen. Problem is.. If I go back and change that number of objects and press next.. the number of objects will go unchanged. How do I tell QWizard to do this?

    This is the simplified code, it runs:

    Qt Code:
    1. #ifndef CLASSWIZARD_H
    2. #define CLASSWIZARD_H
    3.  
    4. #include <QWizard>
    5.  
    6. QT_BEGIN_NAMESPACE
    7. class QCheckBox;
    8. class QGroupBox;
    9. class QLabel;
    10. class QSpinBox;
    11. class QLineEdit;
    12. class QVector<QLineEdit>;
    13. QT_END_NAMESPACE
    14.  
    15. //! [0]
    16. class ClassWizard : public QWizard
    17. {
    18. Q_OBJECT
    19.  
    20. public:
    21. ClassWizard(QWidget *parent = 0);
    22. };
    23.  
    24. class NumObjectsPage : public QWizardPage
    25. {
    26. Q_OBJECT
    27.  
    28. public:
    29. NumObjectsPage(QWidget *parent = 0);
    30.  
    31. private:
    32. QLabel *lblNumObjs;
    33. QSpinBox *leNumObjs;
    34. };
    35.  
    36. class ObjectsPage : public QWizardPage
    37. {
    38. Q_OBJECT
    39.  
    40. public:
    41. ObjectsPage(QWidget *parent = 0);
    42.  
    43. protected:
    44. void initializePage();
    45.  
    46. private:
    47. QVector<QSpinBox*> sbObjs;
    48. QVector<QLabel*> lblObjs;
    49. };
    50.  
    51. #endif
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QtGui>
    2. #include "classwizard.h"
    3.  
    4. ClassWizard::ClassWizard(QWidget *parent)
    5. : QWizard(parent)
    6. {
    7. addPage(new NumObjectsPage);
    8. addPage(new ObjectsPage);
    9.  
    10. setWindowTitle(tr("Wizard"));
    11. }
    12.  
    13.  
    14. NumObjectsPage::NumObjectsPage(QWidget *parent)
    15. : QWizardPage(parent)
    16. {
    17. lblNumObjs = new QLabel(tr("Number of Objects:"));
    18. leNumObjs = new QSpinBox();
    19. lblNumObjs->setBuddy(leNumObjs);
    20.  
    21. registerField("numObjs*", leNumObjs);
    22.  
    23. QGridLayout *layout = new QGridLayout;
    24. layout->addWidget(lblNumObjs, 0, 0);
    25. layout->addWidget(leNumObjs, 0, 1);
    26.  
    27. setLayout(layout);
    28. }
    29.  
    30. ObjectsPage::ObjectsPage(QWidget *parent)
    31. : QWizardPage(parent)
    32. {
    33. }
    34.  
    35. void ObjectsPage::initializePage()
    36. {
    37. QGridLayout *layout = new QGridLayout;
    38. QSpinBox* sbVel = NULL;
    39. QLabel* lblVel = NULL;
    40.  
    41. QString numLayers = field("numObjs").toString();
    42. int num = numLayers.toInt();
    43.  
    44. for (int i = 0; i < num; ++i) {
    45. sbVel = new QSpinBox();
    46. lblVel = new QLabel;
    47. lblVel->setText("Capa " + QString::number(i+1));
    48. sbObjs.push_back(sbVel);
    49. lblObjs.push_back(lblVel);
    50. layout->addWidget(lblVel, i, 0);
    51. layout->addWidget(sbVel, i, 1);
    52. }
    53.  
    54. setLayout(layout);
    55.  
    56. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include <QApplication>
    2. #include "classwizard.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. ClassWizard wizard;
    8. wizard.show();
    9. return app.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2008
    Posts
    33
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QWizard creating objects dynamically

    You Should implement the virtual function cleanupPage() ,and remove all the widget for the first time, I test, It's OK!

    initializePage() is called to initialize the page's contents when the user clicks the wizard's Next button. If you want to derive the page's default from what the user entered on previous pages, this is the function to reimplement.
    cleanupPage() is called to reset the page's contents when the user clicks the wizard's Back button.
    validatePage() validates the page when the user clicks Next or Finish. It is often used to show an error message if the user has entered incomplete or invalid information.
    nextId() returns the ID of the next page. It is useful when creating non-linear wizards, which allow different traversal paths based on the information provided by the user.

    void ObjectsPage::cleanupPage()
    {
    QLayout* playout = layout();
    delete playout;
    }

  3. The following user says thank you to bangqianchen for this useful post:

    afflictedd2 (19th March 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QWizard creating objects dynamically

    Hi,



    Lykurg

  5. #4
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWizard creating objects dynamically

    Quote Originally Posted by Lykurg View Post
    Hi,



    Lykurg
    Hmm do you have a snippet of code / example for this. I just can't picture what you're telling me in code.

  6. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QWizard creating objects dynamically

    Ok, i try.
    You have 2 pages in an QWizard. Already full designed (in designer) and on the second page is an empty layout for the "something" which number gets defined on page1.

    Qt Code:
    1. XYZ::initializePage(int id)
    2. {
    3. if (id==1) // page2
    4. {
    5. QWidget *w = page(id);
    6. // finding the layout or when hand coded use the pointer
    7. // to the layout direct
    8. QLayout *layout = w->findChild<QLayout *>("nameSetInDesigner");
    9. // and now populate the "something": cnt is the number;
    10. if (cnt < layout->findChild<Something *>())
    11. {
    12. //remove
    13. Something *smt = layout->findChild<Something *>().last();
    14. layout->removeWidget(smt);
    15. delete smt;
    16. }
    17. else
    18. {
    19. //add
    20. layout->addWidget(new Something(layout));
    21. }
    22. }
    23. }
    To copy to clipboard, switch view to plain text mode 

    Not proofed!

  7. The following user says thank you to Lykurg for this useful post:

    afflictedd2 (24th March 2009)

  8. #6
    Join Date
    Jul 2008
    Posts
    48
    Thanks
    14
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QWizard creating objects dynamically

    Hi everyone,

    This time I'm facing a new problem :\. When I register a field, in the initializePage() I cannot go back and forth again in the wizard because it will attempt to register that field again. Is it possible to unregister a field when I go back?

    Qt Code:
    1. void ObjectsPage::initializePage()
    2. {
    3. layout = new QGridLayout;
    4. QSpinBox* sbVel = NULL;
    5. QLabel* lblVel = NULL;
    6.  
    7. QString numLayers = field("numObjs").toString();
    8. int num = numLayers.toInt();
    9.  
    10. for (int i = 0; i < num; ++i) {
    11. sbVel = new QSpinBox();
    12. sbVel->setMinimum(500);
    13. sbVel->setMaximum(20000);
    14. sbVel->setSingleStep(500);
    15. sbVel->setValue((i+1)*500);
    16.  
    17. lblVel = new QLabel;
    18. lblVel->setText("Capa " + QString::number(i+1));
    19. sbObjs.push_back(sbVel);
    20. lblObjs.push_back(lblVel);
    21. layout->addWidget(lblVel, i, 0);
    22. layout->addWidget(sbVel, i, 1);
    23. registerField("sbVel" + QString::number(i), sbObjs[i]);
    24. }
    25.  
    26. setLayout(layout);
    27. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 8
    Last Post: 20th April 2007, 00:37
  2. creating objects in a for()
    By phil_ in forum Newbie
    Replies: 3
    Last Post: 21st January 2006, 13:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.