View Full Version : insert a widget into a group "on-the-fly"
soul_rebel
6th January 2006, 18:49
i need to insert objects into a groupbox dynamically, but somehow they dont appear...
i tried this so far:
first:
KURLLabel *label = new KURLLabel(secGroup, "test");
label->setText("woooha")); then
KURLLabel *label = new KURLLabel(secGroup, "test");
QGridLayout *layo = new QGridLayout(secGroup->layout());
layo->addWidget( label, 0, 0 );
label->setText("woooha")); the gridlayout actually is there already and has one member before the code is executed...
how would i insert the label, so that it actually is there?
thanks!
katrina
6th January 2006, 19:44
not sure if this is what you are looking for but something like this might work:
try implementing your groupbox along theses lines:
(not sure if this code even works, just wrote it off the top of my head) (note the sytax is qt 4.1, 3.x might be different)
----------------groupbox.h---------------
#ifndef GROUPONE_H
#define GROUPONE_H
#include <QGroupBox>
class QLabel;
class QHBoxLayout;
class groupbox : public QGroupBox
{
Q_OBJECT
public:
groupbox(QWidget *parent = 0);
groupbox(QWidget *parent,const QString &text);
public slots:
void addWidget(QWidget *newChild);
private slots:
void display();
private:
QHBoxLayout *mainLayout;
QLabel *labelOne;
};
#endif
-------------groupbox.cpp--------------------
#include <QGroupBox>
#include <QLabel>
#include <QHBoxLayout>
#include <QString>
#include <QWidget>
#include "groupbox.h"
groupbox::groupbox(QWidget *parent) : QGroupBox(parent)
{
display();
}
groupbox::groupbox(QWidget *parent, const QString &text) : QGroupBox(text,parent)
{
display();
}
void groupbox::display()
{
labelOne = new QLabel("wooha");
mainLayout = new QHBoxLayout(this);
mainLayout->addWidget(labelOne);
}
void groupbox::addWidget(QWidget *newChild)
{
mainLayout->addWidget(newChild);
}
----------------------------------------------------------
then the parent widget of your groupbox can send a signal containing the widget you want to add to your groupbox. (or of course call the groupBox::addWidget() function directly)
katrina
6th January 2006, 21:25
Also it seems best to either:
1) Create parentless widgets and them add them to a layout using addWidget
2) Create the layout first then specify the layout as the parent in the widget creation.
ex #1)
QLabel *bob = new QLabel;
QLayout *fred = new QLayout(this);
QLayout->addWidget(bob);
ex #2)
QLayout *fred = new QLayout(this);
QLabel *bob = new QLabel(fred);
if instead you create a widget with a parent and then add it to a layout you will see "Widget in wrong parent" notices when you run your application.
ex #bad)
QLabel *bob = new QLabel(this);
QLayout *fred = new QLayout;
QLayout->addWidget(bob);
personally I prefer to create all of my widgets first then create the layouts then add the widgets to the layouts, for organizational purposes.
Katrina
soul_rebel
9th January 2006, 20:43
thanks for your replies first of all and sorry for not answering quicker...
it worked real easy actually... :o
i just had to add label->show(); behind my code....
thanks for your help anyway!
katrina
15th January 2006, 00:29
thanks for your replies first of all and sorry for not answering quicker...
it worked real easy actually... :o
i just had to add label->show(); behind my code....
thanks for your help anyway!
lol ok then! :-)
glad I could try to help and then be shot down ;-)
j/k!
Katrina
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.