Results 1 to 5 of 5

Thread: insert a widget into a group "on-the-fly"

  1. #1
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default insert a widget into a group "on-the-fly"

    i need to insert objects into a groupbox dynamically, but somehow they dont appear...
    i tried this so far:
    first:
    Qt Code:
    1. KURLLabel *label = new KURLLabel(secGroup, "test");
    2. label->setText("woooha"));
    To copy to clipboard, switch view to plain text mode 
    then
    Qt Code:
    1. KURLLabel *label = new KURLLabel(secGroup, "test");
    2. QGridLayout *layo = new QGridLayout(secGroup->layout());
    3. layo->addWidget( label, 0, 0 );
    4. label->setText("woooha"));
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Berlin, Germany
    Posts
    64
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Lightbulb Re: insert a widget into a group "on-the-fly"

    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)

  3. #3
    Join Date
    Jan 2006
    Location
    Berlin, Germany
    Posts
    64
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: insert a widget into a group "on-the-fly"

    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

  4. #4
    Join Date
    Jan 2006
    Location
    germany
    Posts
    75
    Thanks
    9
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: insert a widget into a group "on-the-fly"

    thanks for your replies first of all and sorry for not answering quicker...
    it worked real easy actually...
    i just had to add label->show(); behind my code....
    thanks for your help anyway!

  5. #5
    Join Date
    Jan 2006
    Location
    Berlin, Germany
    Posts
    64
    Thanks
    1
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: insert a widget into a group "on-the-fly"

    Quote Originally Posted by soul_rebel
    thanks for your replies first of all and sorry for not answering quicker...
    it worked real easy actually...
    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

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.