PDA

View Full Version : Add scrollbar to widget dynamically



phillip_Qt
19th August 2013, 08:00
Dear All,

have a widget (QWidgt, part of my mainwidow) . i'm creating textedit dynamically and adding to widget. I want to add a scrollbar into widget. Below is my code.


#ifndef CCREATETEXTEDIT_H
#define CCREATETEXTEDIT_H

#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QTextEdit>

class CCreateTextEdit : public QWidget
{
public:
CCreateTextEdit(QWidget* parent=0);

void AddTextEdit(QString str);

private:
QHBoxLayout* buttonLay;
QVBoxLayout* mainLay ;
};

#endif // CCREATETEXTEDIT_H



//! .cpp file

#include "ccreatetextedit.h"

CCreateTextEdit::CCreateTextEdit(QWidget* parent)
: QWidget(parent)
{

buttonLay = new QHBoxLayout;
mainLay = new QVBoxLayout(this);
mainLay->addLayout(buttonLay);
}

void CCreateTextEdit::AddTextEdit(QString str)
{
QTextEdit *p = new QTextEdit(str);
p->setFixedSize(100, 30);
mainLay->addWidget(p);
}


inside mainwindow ctor


p = new CCreateTextEdit(ui->widget);

p->move(10, 10);

for(int i = 1; i <= 10; ++i)
p->AddTextEdit(QString::number(i));

where p is a CCreateTextEdit pointer;

plase help me.

pkj
19th August 2013, 10:00
QTextEdit inherits QAbstractScrollArea. The member function of your interest is QAbstractScrollArea::setHorizontalScrollBarPolicy and QAbstractScrollArea::setVerticalScrollBarPolicy . In your case the policy will be Qt::ScrollBarAsNeeded

wysota
19th August 2013, 10:02
I think that what OP wants is a scrollable widget that contains dynamically created text edits. Thus my suggestion is to wrap CCreateTextEdit into a QScrollArea using QScrollArea::setWidget(). Remember to set the widgetResizable property of the scroll area to true.

phillip_Qt
19th August 2013, 10:19
I think that what OP wants is a scrollable widget that contains dynamically created text edits. Thus my suggestion is to wrap CCreateTextEdit into a QScrollArea using QScrollArea::setWidget(). Remember to set the widgetResizable property of the scroll area to true.

Hi,

i ve to dynamically created On click a tool button textedit and have to add in a widget, part of mainwindow.

widget size is fixed to 100 x100.
and scrollbar should be display automatically if the added to widget.

pkj
19th August 2013, 11:49
Rather than dynamically creating, why not use QWidget::setVisible or QWidget::setEnabled. It will do the same.

phillip_Qt
19th August 2013, 12:41
Rather than dynamically creating, why not use QWidget::setVisible or QWidget::setEnabled. It will do the same.

No it didnot work. Actually in mainwidow i've dragged and added a widget by using creator. Now I've a class(CMyTextEdit) which will create a textedit.
In mainwindow i've a push button. On clicking a push button, i'm creating pointer of the class (CMyTextEdit *) which create the text edit, and for CMyTextEdit ui->widget is the parent. I want to add a scrollbar to ui->widget if the item is increaing.