Hey guys.
I need a scroll view which can add children one by one from top to bottom. and it can be auto-resized. and also the children labels will hold the whole view space. Here is the code.
{
Q_OBJECT
public:
{
setLayout( layout );
layout->addWidget( scrollArea );
viewLayout->setAlignment( Qt::AlignTop );
view->setLayout(viewLayout);
scrollArea->setWidget( view );
scrollArea->setWidgetResizable(true);
scrollArea->verticalScrollBar()->setSingleStep( 20 );
for (int i=0; i<1; ++i)
{
label
= new QLabel("<font color='#000000' size='2'>Abc defg hi..END_OF_LABEL</font>", view
);
label->setWordWrap(true);
label->setAutoFillBackground( true );
// QSizePolicy sp = label->sizePolicy();
// sp.setVerticalPolicy(QSizePolicy::Expanding);
// label->setSizePolicy(sp);
label->setPalette(p);
viewLayout->addWidget( label );
}
}
protected:
};
class QScrollView : public QWidget
{
Q_OBJECT
public:
QScrollView( QWidget* parent = 0 )
{
QHBoxLayout* layout = new QHBoxLayout(this);
setLayout( layout );
scrollArea = new QScrollArea(this);
layout->addWidget( scrollArea );
view = new QWidget(scrollArea);
viewLayout = new QVBoxLayout(view);
viewLayout->setAlignment( Qt::AlignTop );
view->setLayout(viewLayout);
scrollArea->setWidget( view );
scrollArea->setWidgetResizable(true);
scrollArea->verticalScrollBar()->setSingleStep( 20 );
QLabel* label = NULL;
for (int i=0; i<1; ++i)
{
label = new QLabel("<font color='#000000' size='2'>Abc defg hi..END_OF_LABEL</font>", view );
label->setWordWrap(true);
label->setAutoFillBackground( true );
// QSizePolicy sp = label->sizePolicy();
// sp.setVerticalPolicy(QSizePolicy::Expanding);
// label->setSizePolicy(sp);
QPalette p = label->palette();
p.setColor( QPalette::Window, QColor(255,0,255) );
label->setPalette(p);
viewLayout->addWidget( label );
}
}
protected:
QScrollArea* scrollArea;
QWidget* view;
QVBoxLayout* viewLayout;
};
To copy to clipboard, switch view to plain text mode
it works well when the label has just a few characters.
But when the label has a larger number of characters, i'm in a mess.
To show the label's text, i must set it's Word Wrap property, right? but this will cause a problem, when i narrow the main window, the label don't show all the text. it has been cut off! finally I found that I can set the label's size policy by using "Expanding" or 'MinimumExpanding', it will solve the display problem when narrowing. but on the other side, the label dont has the fit height when i resize the window to a larger width scale. After trying all the parameters, i can't still find the answer. seeking for anyone's help now. I will greatly appreciate that!!!!
Bookmarks