开发者

Best way to implement a view with multiple widgets in QT?

I would like to have a view where I show the user various QLabels, a .jpg image, and a list of items (eg. a QListView). I would like all of them to be vertically scrollable together. The data for filling the various widgets will be set by the function that shows this view. I prefer implementing it in code, without using the GU开发者_如何学GoI Designer.

I thought about creating my custom widget inheriting from QWidget, but I am finding it hard to implement it. Is this the best way to do it?

Could you provide me with an example of how I should proceed?

Thanks in advance


//Using QScrollArea

#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QScrollArea>
#include <QVBoxLayout>

class MyWidget: public QWidget
{
    Q_OBJECT
public:
    explicit MyWidget(QWidget *parent = 0);

private:
    QScrollArea *scrollArea;
    QWidget *widget;
    QLabel *label1;
    QLabel *label2;
    QLabel *label3;
    QVBoxLayout *vLayout;
};

#endif // MYWIDGET_H


#include "mywidget.h"

MyWidget::MyWidget(QWidget *parent) :
    QWidget(parent)
{
    scrollArea=new QScrollArea(this);
    widget=new QWidget;
    label1=new QLabel("Label1");
    label2=new QLabel("Label2");
    label3=new QLabel("Label3");

    label1->setFixedSize(200,100);
    label2->setFixedSize(200,100);
    label3->setFixedSize(200,100);
    vLayout=new QVBoxLayout;

    vLayout->addWidget(label1);
    vLayout->addWidget(label2);
    vLayout->addWidget(label3);
    widget->setLayout(vLayout);

    scrollArea->setWidget(widget);
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    scrollArea->resize(200,150);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜