PDA

View Full Version : Creating a new widget and apply it on another widget



seux
8th September 2011, 22:14
Hello everyone,
imagine that i have something like this in a class


//...
private:
QWidget *widget;
and in one of the public functions i want to add other widgets like maybe a QPushButton and a QVBoxLayout. How can i do that?
For the QPushButton i tried it this way:

QPushButton *btn = new QPushButton("Hello");
widget->layout()->addWidget(btn);
But the widget has currently no layout, so i cannot try it out...
Is it possible to do it this way, and how can i apply a layout to the widget?

Hostel
9th September 2011, 01:42
Write more code. You should use a name of class that have a widget. Where you use a code with QPushButton?

ChrisW67
9th September 2011, 06:19
But the widget has currently no layout, so i cannot try it out...
It isn't magic, you have to set one: QWidget::setLayout()

Lots more in the trusty manual

nish
9th September 2011, 07:40
the widget has no layout, so probably your child widgets are manually managed for resize. For widgets without layouts, you should pass the parent widget pointer to the child widget's constructor

pushButton = new QPushButton("Hello",this);