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

What is the significance of following constructor myView::myView(MyMainWindow* parent):QWidget(NULL)



  • If I have a class

    class myMainWindow: public QMainWindow() {
    }
    class myView: public QWidget {
    myView(myMainWindow* parent);
    }

    what is difference between three constructors

    myView:myView(myMainWindow* parent): QWidget(parent) {
    }

    or
    myView:myView((myMainWindow* parent):):QWidget(NULL) {
    }
    or
    myView:myView(myMainWindow* parent) .

    Could you let me know what is significance of each


  • Lifetime Qt Champion

    myView:myView(myMainWindow* parent): QWidget(parent) {

    Give the parent to the Base class

    myView:myView((myMainWindow* parent):):QWidget(NULL)

    Give no parent to base class. THIS YOU SHOULD NEVER DO. EVER. not like that

    if u want no parent, call like this
    new myView(NULL);



  • So If I have call like

    class myMainWindow:QMainWindow{
    }

    class mytableView: public QtableView {
    protected:
    virtual void keyPressEvent(QKeyEvent *event);

    };
    class myView: public QWidget {
    myView(A*,QString title, myMainWindow*)
    private:
    A* d_a;
    MyMainWindow* d_parent ;
    mytableView* table;
    }

    myView::myView(Aa ,QString title,myMainWindow parent ) : d_a(a),d_parent(parent)
    {
    setWindowTitle(title);
    table = new mytableView;
    }
    mytableView:keyPressEvent(QKeyEvent *event) {
    ...
    ...
    }

    What is the problem in this code for parent child relation


  • Lifetime Qt Champion

    why would you ever do that ??
    Just call your baseclass constructor as you should.
    Its how it works and you are also free to have parent * as variable but
    you should always call BaseClass constructor with the given parent.


Log in to reply