PDA

View Full Version : Fix proportion of width and height in QGridLayout



Wong
5th January 2011, 02:12
I run into trouble recent when use QGridLayout

This is my mainwindow. A QTabWidget, and add two QWidget tabs.
Tab use QGridLayout to arrange some labels.
http://i627.photobucket.com/albums/tt360/wuchuanwang/1-1.jpg

The label can drop, and show after accept text.
But when text is too long, label stretch as same as text's length. I'm very worry, I want to fix proportion of each label's width and height. I try to set wordWrap true. But failed.
http://i627.photobucket.com/albums/tt360/wuchuanwang/2-1.jpg

How can I do that?
My english is poor, so maybe it's difficult to understand, sorry!:)

Lykurg
5th January 2011, 08:07
What are the size policies of the labels? Try to set all of them to expanding with the same stretch value.

Further, please do attach images on the board and do not link them from 3rd party sites.

Wong
5th January 2011, 10:35
I don't set size policy of labels.
Before that, I try to setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding), and unsuccessful.

Wong
6th January 2011, 03:26
I try to set Chinese characters, and it's ok!
So I think set wordWrap is valid. But uninterrupted english characters will stretch label

Lykurg
6th January 2011, 06:47
I guess it is becaus word wrap does not mean wrap at any place. You could subclass QLabel and do the painting yourself using Qt::TextWrapAnywhere.

Wong
7th January 2011, 03:00
Thanks a lot!:)

I use this->setText before time
And it's ok when replaced with painter.drawText


void ViewLabel::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.drawText(this->rect(), Qt::AlignCenter|Qt::TextWrapAnywhere, "...");
}


Very excited !

Lykurg
7th January 2011, 07:24
Not sure if you do it already, but you still can use setText and in the paint simply do:
painter.drawText(this->rect(), Qt::AlignCenter|Qt::TextWrapAnywhere, text());And you can even adjust the sizeHint function for the new "space requirement".