PDA

View Full Version : initialize child widgets within parent?



ucomesdag
5th June 2006, 23:03
Hi, i was wondering if it is possible to initalize a child widget within the parent?
I tried to add it in the constructor of the parent, but then the child widgets is not shown...

In this case i want to initialize fx1 in the constructor of floorBoard... this to keep clean main.


#include "stompBox.h"
#include "floorBoard.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

floorBoard floor;

stompBox fx1(&floor);
fx1.setId(0);
fx1.setImage(":/images/fx1.png");
fx1.setPos(floor.getPos(0));

QComboBox *fx1_comboBox = new QComboBox(&fx1);
fx1_comboBox->addItem( "Test 1" );
fx1_comboBox->addItem( "Test 2" );
fx1_comboBox->addItem( "Test 3" );

fx1_comboBox->setGeometry(8, 31, 79, 13);
fx1_comboBox->setEditable(false);
fx1_comboBox->setFont(font);
fx1_comboBox->setPalette(pal);
fx1_comboBox->setFrame(false);

customButton fx1_button(false, QPoint::QPoint(4, 110), &fx1);
customLed fx1_led(false, QPoint::QPoint(41, 4), &fx1);
QObject::connect(&fx1_button, SIGNAL(valueChanged(bool)),
&fx1_led, SLOT(setValue(bool)));

floor.show();

return app.exec();
};

wysota
6th June 2006, 01:08
If stompBox is a widget, then it shouldn't be any problem. If it is a dialog, though, you'll have to explicitely call show() or exec() for it. But you can still do the initialisation in the parent -- that's what is usually done, anyway...

ucomesdag
6th June 2006, 01:44
It is a widget. But when I do so the widget isn't shown. And if I add .show() it just shown on initialization and disapears inmediatly after :confused: :crying:


#include "stompBox.h"
#include "floorBoard.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

floorBoard floor;
floor.show();

return app.exec();
};


#include "floorBoard.h"

floorBoard::floorboard((QWidget *parent, ... )
: QWidget(parent)
{
...

stompBox fx1(this);
fx1.setId(0);
fx1.setImage(":/images/fx1.png");
fx1.setPos(this->getPos(0));

QComboBox *fx1_comboBox = new QComboBox(&fx1);
fx1_comboBox->addItem( "Test 1" );
fx1_comboBox->addItem( "Test 2" );
fx1_comboBox->addItem( "Test 3" );

fx1_comboBox->setGeometry(8, 31, 79, 13);
fx1_comboBox->setEditable(false);
fx1_comboBox->setFont(font);
fx1_comboBox->setPalette(pal);
fx1_comboBox->setFrame(false);

customButton fx1_button(false, QPoint::QPoint(4, 110), &fx1);
customLed fx1_led(false, QPoint::QPoint(41, 4), &fx1);
QObject::connect(&fx1_button, SIGNAL(valueChanged(bool)),
&fx1_led, SLOT(setValue(bool)));
};

munna
6th June 2006, 04:43
I think it should be something like




stompBox *fx1 = new stompBox(this);
fx1->setId(0);
fx1->setImage(":/images/fx1.png");
fx1->setPos(this->getPos(0));

ucomesdag
6th June 2006, 05:01
Both do the samething, I guess... But that ain't the problem. The widget just flashes 1 time on the screen on initialization and thats it.... :eek:

munna
6th June 2006, 05:20
Yes both do the samething but in your way of declaration the object is destroyed once the program is out of the constructor. Either you have to declare fx1,fx1_button..etc in your class declaration or use them the way you are using fx1_comboBox(as a pointer).

wysota
6th June 2006, 08:11
It's sometimes worth it to read the FAQ (http://www.qtcentre.org/forum/faq.php?faq=qt_general_category#faq_qt_stack_creat ed_widgets)