views:

253

answers:

1

Easy question (I hope!). This is my first Qt app and I'm struggling with how to have groups of elements on my Gui.

I need about 8 standard QWidgets (labels, edits, buttons) for each File object, which can be added or removed dynamically.

So ultimately I need to put all the File objects inside a QVBoxLayout inside a QScrollArea.

But first I need to group them somehow. I realize I could draw them all on my main window with a lot of offsets, but it wouldn't be very elegant.

How can I make my File object extend some sort of canvas and each one maintains it's own set of widgets?

Thanks for any guidance.

+3  A: 

A QWidget can have a Layout and contain other widgets. So you could subclass QWidget, add whatever you need (along with a Layout) and use that wherever you want. Or even better (I think, based on your needs) is to subclass QScrollArea directly, add what you need including the layout, and then you can drop it in wholesale:

class MyWidget : public QScrollArea
{
Q_OBJECT
public:
    MyWidget(const QString& filename, QWidget* parent = 0);
};

MyWidget::MyWidget(const QString& filename, QWidget* parent) : QScrollArea(parent)
{
    setWidgetResizable(true);

    QWidget* central = new QWidget(this);
    setWidget(central);

    QVBoxLayout* layout = new QVBoxLayout(central);
    central->setLayout(layout);

    layout->addWidget(new QLabel(filename, central));
    layout->addWidget(new QLineEdit("editor 1", central));
    layout->addWidget(new QLineEdit("editor 2", central));
    layout->addWidget(new QLineEdit("editor 3", central));
    layout->addWidget(new QLineEdit("editor 4", central));
    layout->addWidget(new QLineEdit("editor 5", central));
    layout->addStretch();
}

Then to use it:

MyWidget* widget1 = new MyWidget("/file1", parent);
MyWidget* widget2 = new MyWidget("/file2", parent);
layout->addWidget(widget1);
layout->addWidget(widget2);
Adam Batkin
Thanks! Not exactly what I was trying to do, but I adapted this. I made a FilePanel class extending QScrollBar and then I add FileItems to it dynamically. FileItems have Layouts with Widgets in them, so I add that Layout to (layout) held by (central). Only problem now is it doesn't draw anything when I add one after it's already initialized.
graw
OK I got it working now, thanks! Yes it's interesting how you can nest widgets in layouts in layouts in layounts in widgets in layouts, etc. :)
graw