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);
p1.setText("Hello");
QPushButton *p2 = new QPushButton(&test);
p2->setGeometry(30, 20, 50, 40);
p2->setText("HG");
p1.show();
p2->show();
test.show();@I think that you don't set the position of yours buttons.
-
Hi,
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.