Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

form on create function



  • HI
    i am looking for something like an on create, meaning when the widget / page etc is created this function is called to perfom tasks ...

    specific

    i have a wizzard page

    WizardPage2::WizardPage2(QWidget *parent) :
       QWizardPage(parent),
       ui(new Ui::WizardPage2)
    {
       ui->setupUi(this);
    
       //Hide Advanced Boxes
       ui->ed->hide();
    .....
    }
    
    WizardPage2::~WizardPage2()
    {
       delete ui;
    }
    
    void WizardPage2::on_rb_wifi_clicked()
    {
      if (ui->rb->isChecked()) {
          ui->ed->setEnabled(true);
          ...
          wizard()->button(QWizard::NextButton)->setDisabled(false);
      }
    }
    

    im am now trying to do

     wizard()->button(QWizard::NextButton)->setDisabled(false);
    

    when the page is loaded /created ....
    but when i try to do this in the wizard page section, like all other thinks like hiding the textboxes my program just crashes without an error

    what am i doing wrong ? or is there an other way to do this

    (i am fairly new to qt, just used vs c# and delphi before)

    how would i generaly do an on create on a widget, to call functions for eg fetching form data when its showing up


  • Qt Champions 2019

    Isn't constructor what you are looking for?
    You can disable the button in the WizardPage2 constructor (after ui->setupUi(this);).
    Regarding the crash: you're probably dereferencing an invalid pointer.
    What is wizard() in your example?



  • @jsulm

    yea a constructor is what i am looking for.

    i already tried this, but it keeps crashing

    wizard is a method i can cool in this class ...
    as you can see below in the functions it is working.
    But not in the constructor and i dont get why ..


  • Qt Champions 2019

    @freevrs Did you try to debug to see where exactly it crashes?



  • @jsulm

    i got a sigsegv

    it is segfaulting at the wizard() in the constructor

    But i dont get why, because i call it the same way later on (in the click funktions) and everything is fine


  • Qt Champions 2019

    @freevrs As far as I can see wizard() returns a pointer. Is it possible that it returns an invalid pointer? And does it crash inside wizard()?



  • @jsulm

    this is the complete stack trace

    Thread 1 (Thread 9460.0x2030):
    #0  0x0accbeac in QScopedPointer<QObjectData, QScopedPointerDeleter<QObjectData> >::data (this=0x4) at ../../include/QtCore/../../src/corelib/tools/qscopedpointer.h:135
    No locals.
    #1  0x0aae7e74 in qGetPtrHelper<QScopedPointer<QObjectData> > (p=...) at ../../include/QtCore/../../src/corelib/global/qglobal.h:983
    No locals.
    #2  0x0acf8d57 in QWizard::d_func (this=0x0) at dialogs/qwizard.h:193
    No locals.
    #3  0x0aae5b9f in QWizard::button (this=0x0, which=QWizard::NextButton) at dialogs\qwizard.cpp:2818
            d = 0x15b6de38
    #4  0x00401df8 in WizardPage2::WizardPage2 (this=0x15b6dc78, parent=0x0) at ..\SPAPP-Screen\wizardpage2.cpp:10
    No locals.
    #5  0x00401711 in qMain (argc=1, argv=0x9c3578) at ..\SPAPP-Screen\main.cpp:26
            a = {<QGuiApplication> = {<QCoreApplication> = {<QObject> = {_vptr.QObject = 0xadf7f88 <vtable for QApplication+8>, static staticMetaObject = {d = {superdata = 0x0, stringdata = 0x6bb73880 <qt_meta_stringdata_QObject>, data = 0x6bb73980 <qt_meta_data_QObject>, static_metacall = 0x6b90941e <QObject::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, d_ptr = {d = 0x9c3628}, static staticQtMetaObject = {d = {superdata = 0x0, stringdata = 0x6bc32900 <qt_meta_stringdata_Qt>, data = 0x6bc3b040 <qt_meta_data_Qt>, static_metacall = 0x0, relatedMetaObjects = 0x0, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0x6bb7340c <QObject::staticMetaObject>, stringdata = 0x6bc47600 <qt_meta_stringdata_QCoreApplication>, data = 0x6bc477c0 <qt_meta_data_QCoreApplication>, static_metacall = 0x6b9693e4 <QCoreApplication::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, static self = 0x28fe3c}, static staticMetaObject = {d = {superdata = 0x6bc47584 <QCoreApplication::staticMetaObject>, stringdata = 0xf7ee00 <qt_meta_stringdata_QGuiApplication>, data = 0xf7f200 <qt_meta_data_QGuiApplication>, static_metacall = 0xb4e2bc <QGuiApplication::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0xf75040 <QGuiApplication::staticMetaObject>, stringdata = 0xad1f4c0 <qt_meta_stringdata_QApplication>, data = 0xad1f740 <qt_meta_data_QApplication>, static_metacall = 0xa8c1134 <QApplication::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}}
            w = {<QMainWindow> = {<QWidget> = {<QObject> = {_vptr.QObject = 0x40cfc8 <vtable for MainWindow+8>, static staticMetaObject = {d = {superdata = 0x0, stringdata = 0x6bb73880 <qt_meta_stringdata_QObject>, data = 0x6bb73980 <qt_meta_data_QObject>, static_metacall = 0x6b90941e <QObject::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, d_ptr = {d = 0x9cdf78}, static staticQtMetaObject = {d = {superdata = 0x0, stringdata = 0x6bc32900 <qt_meta_stringdata_Qt>, data = 0x6bc3b040 <qt_meta_data_Qt>, static_metacall = 0x0, relatedMetaObjects = 0x0, extradata = 0x0}}}, <QPaintDevice> = {_vptr.QPaintDevice = 0x40d0a4 <vtable for MainWindow+228>, painters = 0, reserved = 0x0}, static staticMetaObject = {d = {superdata = 0x6bb7340c <QObject::staticMetaObject>, stringdata = 0xad25b40 <qt_meta_stringdata_QWidget>, data = 0xad265c0 <qt_meta_data_QWidget>, static_metacall = 0xa8fc0ec <QWidget::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, data = 0x9ce020}, static staticMetaObject = {d = {superdata = 0xad1c060 <QWidget::staticMetaObject>, stringdata = 0xade7280 <qt_meta_stringdata_QMainWindow>, data = 0xade75c0 <qt_meta_data_QMainWindow>, static_metacall = 0xabf8e1c <QMainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0xade776c <qt_meta_extradata_QMainWindow>, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0xade7218 <QMainWindow::staticMetaObject>, stringdata = 0x40c4c4 <qt_meta_stringdata_MainWindow>, data = 0x40c4e0 <qt_meta_data_MainWindow>, static_metacall = 0x402220 <MainWindow::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, ui = 0x9ceb50}
            setupcomplete = false
            wizard = {<QDialog> = {<QWidget> = {<QObject> = {_vptr.QObject = 0xae074c8 <vtable for QWizard+8>, static staticMetaObject = {d = {superdata = 0x0, stringdata = 0x6bb73880 <qt_meta_stringdata_QObject>, data = 0x6bb73980 <qt_meta_data_QObject>, static_metacall = 0x6b90941e <QObject::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, d_ptr = {d = 0x15b52298}, static staticQtMetaObject = {d = {superdata = 0x0, stringdata = 0x6bc32900 <qt_meta_stringdata_Qt>, data = 0x6bc3b040 <qt_meta_data_Qt>, static_metacall = 0x0, relatedMetaObjects = 0x0, extradata = 0x0}}}, <QPaintDevice> = {_vptr.QPaintDevice = 0xae075c4 <vtable for QWizard+260>, painters = 0, reserved = 0x0}, static staticMetaObject = {d = {superdata = 0x6bb7340c <QObject::staticMetaObject>, stringdata = 0xad25b40 <qt_meta_stringdata_QWidget>, data = 0xad265c0 <qt_meta_data_QWidget>, static_metacall = 0xa8fc0ec <QWidget::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}, data = 0x15b52340}, static staticMetaObject = {d = {superdata = 0xad1c060 <QWidget::staticMetaObject>, stringdata = 0xad5b0c0 <qt_meta_stringdata_QDialog>, data = 0xad5b240 <qt_meta_data_QDialog>, static_metacall = 0xaab1464 <QDialog::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}}, static staticMetaObject = {d = {superdata = 0xad5afcc <QDialog::staticMetaObject>, stringdata = 0xad64a80 <qt_meta_stringdata_QWizard>, data = 0xad65040 <qt_meta_data_QWizard>, static_metacall = 0xaae748c <QWizard::qt_static_metacall(QObject*, QMetaObject::Call, int, void**)>, relatedMetaObjects = 0x0, extradata = 0x0}}}
    #6  0x00403b50 in WinMain@16 () at qtmain_win.cpp:113
            argc = 1
            argvW = 0xa3c200
            argv = 0x9c3578
            exitCode = 119
    #7  0x004066ed in main ()
    No symbol table info available.
    

    is it maby that because the wizard is created in the main.cpp

    QWizard wizard;
        wizard.setWindowTitle("Setup");
        wizard.addPage(new WizardPage1());
        wizard.addPage(new WizardPage2());
        wizard.addPage(new WizardPage3());
        wizard.show();
    

    and the wizardpacge2 is compiled before that , that wthe wizard() does not exist at this moment ?


  • Qt Champions 2019

    @freevrs

    #3  0x0aae5b9f in QWizard::button (this=0x0, which=QWizard::NextButton) at dialogs\qwizard.cpp:2818
            d = 0x15b6de38
    #4  0x00401df8 in WizardPage2::WizardPage2 (this=0x15b6dc78, parent=0x0) at ..\SPAPP-Screen\wizardpage2.cpp:10
    No locals.
    

    Can you show the code in wizardpage2.cpp around the line 10?
    In this line I can see that this is null:

    #3  0x0aae5b9f in QWizard::button (this=0x0, which=QWizard::NextButton) at dialogs\qwizard.cpp:2818
            d = 0x15b6de38
    

    And can you please explain what wizard() is doing? Wizard itself already exists when you create WizardPage2.


  • Qt Champions 2019

    @freevrs "and the wizardpacge2 is compiled before that , that wthe wizard() does not exist at this moment" I don't understand this sentence.
    First, wizardpacge2 is not compiled - it is instantiated/created.
    Second, "wizard() does not exist at this moment" - wizard() is a method, so it exists.



  • @jsulm

    wizard() is a method i can use after the wizardpage is created.

    with that i can access elements of the wizard box (like buttons)

    the wizard is created in the main function and than the wizard pages get added.

    so i dont understand why i cant use it in the creator

    http://doc.qt.io/qt-4.8/qwizard.html
    http://doc.qt.io/qt-5/qwizardpage.html
    http://www.qtcentre.org/threads/13844-How-to-disable-NextButton-in-QWizard


  • Qt Champions 2019

    @freevrs Please read the documentation (http://doc.qt.io/qt-5/qwizardpage.html#wizard):

    Returns the wizard associated with this page, or 0 if this page hasn't been inserted into a QWizard yet.
    
    QWizard wizard;
    wizard.addPage(new WizardPage2());
    

    new WizardPage2() creates a new instance and calls its constructor - at that time the wizard is not yet set, it is set when wizard.addPage(...) is called which happens after the constructor was executed. That's why wizard() returns a null pointer in constructor.



  • @jsulm

    ok .. but how can i solve this ?

    can i create a slot that is called on the addPage event an then call wizard() ?
    Or do you have a better idea / solution for that.

    What i need to achive is, that when the wizzard page is shown the next button is disabled


  • Qt Champions 2019


  • Qt Champions 2017

    @freevrs

    You can intercept the widget parent's swap by handling the QEvent::ParentChange. Also you can queue the initialization for later when control returns to the event loop, which would by my preference, e.g.:

    class WizardPage2
    {
    public:
        WizardPage2(QWidget * parent);
    
    private:
         Q_INVOKABLE void initMyPage();
    };
    
    WizardPage2::WizardPage2(QWidget * parent)
        : QWizardPage(parent)
    {
        QMetaObject::invokeMethod(this, "initMyPage", Qt::QueuedConnection);
    }
    
    void WizardPage2::initMyPage()
    {
        // wizard() should be pointing to an object now.
    }
    

  • Lifetime Qt Champion

    Hi,

    But why disable the wizard button in the constructor of the second page ? That's rather the role of the page validation. See QWizard::validateCurrentPage documentation.



  • @kshegunov

    Your solution works pretty good.

    I implemented it on page2
    but insted it is only setting the button on page 1 (not implemented there)
    and not on page2

    Do i need to use another event ?


Log in to reply