Nominate our 2022 Qt Champions!

QPushButton initialization

  • When I initialize a QPushButton Object, I can do it this way:
    @QPushButton p1;@

    I can also do it this way:
    @QPushButton *p2 = new QPushButton;@

    The first way initialize an object while the later way initialize a pointer.
    But when I add either of them to the widget and show this widget, the first way doesn't show.
    I wonder why? As far as I know, the QPushButton class has a constructor:QPushtButton(QWidget *parent = 0);, so I don't think the construction process will go wrong...

  • For me, it works. My code :
    @ QWidget test;
    QPushButton p1(&test);
    p1.setGeometry(1, 1, 50, 40);
    QPushButton *p2 = new QPushButton(&test);
    p2->setGeometry(30, 20, 50, 40);

    I think that you don't set the position of yours buttons.

  • Lifetime Qt Champion


    If you do it e.g. in the constructor, p1 will be destroyed at the end of the constructor since it goes out of scope. That's why you don't see it.

Log in to reply