开发者

How to Fit ScrollArea into Grid in QT?

How to Fit ScrollArea into Grid in QT ?

I want to apply QScrollArea to this part of the grid which conta开发者_开发技巧ins first name, lastname...


Here is the sample. The size of QScrollArea is changed inside QGridLayout.

header file

#ifndef WIDGET_H
#define WIDGET_H

#include <QtGui/QWidget>
#include <QScrollArea>
#include <QTextEdit>
#include <QPushButton>
#include <QGridLayout>

class Widget: public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = 0);
    ~Widget();

private:
    QScrollArea *sca;
    QTextEdit *txt;
    QPushButton *btn;

private slots:
    void onClicked();
};

#endif // WIDGET_H

source file

#include "widget.h"
#include <QScrollBar>

Mediator::Widget(QWidget *parent)
    : QWidget(parent)
{
    sca = new QScrollArea(this);
    txt = new QTextEdit;
    txt->setFixedSize(1000,500);
    btn = new QPushButton("Button",this);
    btn->setFixedSize(75,30);

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(sca);
    layout->addWidget(btn);

    sca->setWidget(txt);

    setLayout(layout);

    connect(btn,SIGNAL(clicked()),this,SLOT(onClicked()));
}

Widget::~Widget()
{

}

void Widget::onClicked()
{
    txt->setText(txt->toPlainText() + "abcd ");
}


QScrollArea is an ordinary widget and its size is controlled by the layout that contains it. You can change it size by calling its resize method.

However if you need the object contaied in QScrollArea to be resized as well than you should use QScrollArea::widgetResizable property. Take a look at Image Viewer example to see how the QLabel is resized along with QScrollArea.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜