PDA

View Full Version : Big trouble: QScrollArea & QLabel with word wrap & Qt::AlignTop layout alignment



HiJack
1st June 2010, 11:58
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.

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;

};

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!!!!

high_flyer
2nd June 2010, 08:59
I am not quite sure I follow.
The idea of a scroll area is that what ever is in the scroll area is not being resized, rather, that the view or window is resized, and the content does not.
This is why you get scroll bars to navigate in the content if the content is larger then the window.
So I am not quite sure what it is you are trying to do with the resizing of the content..

HiJack
2nd June 2010, 15:16
I just want to show the label ( with a large number of characters) and expand the content correctly.



* The content can be expanded just vertically, it has the same width as the main window ( in another words, not allowed to be larger than the width of the main window) when resizing the window, the horizontal slider should be disabled.

* With labels word wrap, when showing lots of characters (e.g. I insert a article to the label )the content should grow vertically, and the label always show all the text.(this will not happen when setting 'QSizePolicy::Minimum', it will lost some characters.)

* All the labels can be shrunk to a fit height vertically, and aligned with the top. ( this wil not happen when setting the label with 'QSizePolicy::Expanding' or 'QSizePolicy::MinimumExpanding' . Do I must use 'viewLayout->setAlignment( Qt::AlignTop );'? )





If I just put a few characters into the label, everything is fine. But it became worse when I put more characters into it. what I have missed?

high_flyer
2nd June 2010, 15:56
Ok.
So if I understand correctly your problem is:

the label dont has the fit height when i resize the window to a larger width scale.
Translated:
Everything works, except, that when you resize the window to have a larger width, the labels will remain narrow, is that correct?

If so, try adding a vertical spacer in the layout that holds the labels, it will "squeeze" the labels to take as much horizontal space as they can.

HiJack
8th June 2010, 07:40
Ok.
So if I understand correctly your problem is:

Translated:
Everything works, except, that when you resize the window to have a larger width, the labels will remain narrow, is that correct?

If so, try adding a vertical spacer in the layout that holds the labels, it will "squeeze" the labels to take as much horizontal space as they can.

Nope. buddy.. in fact my problem is the vertical space. would u please create a rapid demo project in a few seconds? and you will understand it right now. Keep narrowing the windows. it will lost some characters. my test OS is windows XP.


/////////////////////////////////////////////scrollview.h//////////////////////////////////////////////////////////////
#ifndef ScrollView_H
#define ScrollView_H

#include <QtGui>

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..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..Abc defg hi..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;

};

#endif // ScrollView_H

//////////////////////////////////////main.cpp/////////////////////////////////////////////////////////////////
#include "scrollview.h"
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QScrollView w;
w.show();
return a.exec();
}

high_flyer
8th June 2010, 08:27
I ran you code and I still don't understand the problem.
When resizing the scroll view the labels resize to match the width as it should, no chars are lost...
When narrowing down the scroll view the Labels get narrow, but increase their height to accommodate the long string.

Maybe you can post a screenshot of the problem.

HiJack
9th June 2010, 09:38
Sure thing

http://i854.photobucket.com/albums/ab105/qtcentre/qt.jpg?t=1276072569

high_flyer
9th June 2010, 09:51
Ah... you hit the minimum width of the label, so it got scrolled out (as you can see the horizontal scroll bar that appeared).
Just add:

label->setMinimumWidth(1);
at line 44 in your above code.

HiJack
9th June 2010, 10:56
Ah... you hit the minimum width of the label, so it got scrolled out (as you can see the horizontal scroll bar that appeared).
Just add:

label->setMinimumWidth(1);
at line 44 in your above code.

yeah that's it. it works. :) thank u very much.

but I found there's an irrelevant problem here. If I set the 'word wrap' property, and simultaneity there's a word which has a larger width than the label, such as 'hi..END_OF_LABEL', it will be cut off.
http://i854.photobucket.com/albums/ab105/qtcentre/2222.jpg?t=1276077320
how can i deal with it? Can it be wrapped ? or display some'...'? ( Under the circumstances that the view has a fixed width.)

high_flyer
9th June 2010, 13:04
I am not aware of any ready made option you can use, as far as I know, you will have to subclass QLabel and re implement how word wrapping works. (to wrap at any char and not only at end of words).

HiJack
9th June 2010, 14:12
I am not aware of any ready made option you can use, as far as I know, you will have to subclass QLabel and re implement how word wrapping works. (to wrap at any char and not only at end of words).

OK! You helped me a lot.