Results 1 to 7 of 7

Thread: Fix proportion of width and height in QGridLayout

  1. #1
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Unhappy Fix proportion of width and height in QGridLayout

    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.


    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.


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

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Fix proportion of width and height in QGridLayout

    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.

  3. #3
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fix proportion of width and height in QGridLayout

    I don't set size policy of labels.
    Before that, I try to setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding), and unsuccessful.
    Attached Images Attached Images
    • File Type: jpg 5.JPG (23.8 KB, 19 views)
    • File Type: jpg 6.JPG (25.1 KB, 14 views)

  4. #4
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fix proportion of width and height in QGridLayout

    I try to set Chinese characters, and it's ok!
    So I think set wordWrap is valid. But uninterrupted english characters will stretch label
    Attached Images Attached Images
    • File Type: jpg 1.JPG (20.2 KB, 20 views)

  5. #5
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Fix proportion of width and height in QGridLayout

    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.

  6. The following user says thank you to Lykurg for this useful post:

    Wong (7th January 2011)

  7. #6
    Join Date
    Dec 2010
    Posts
    33
    Thanks
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Fix proportion of width and height in QGridLayout

    Thanks a lot!

    I use this->setText before time
    And it's ok when replaced with painter.drawText
    Qt Code:
    1. void ViewLabel::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4. painter.drawText(this->rect(), Qt::AlignCenter|Qt::TextWrapAnywhere, "...");
    5. }
    To copy to clipboard, switch view to plain text mode 

    Very excited !

  8. #7
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Fix proportion of width and height in QGridLayout

    Not sure if you do it already, but you still can use setText and in the paint simply do:
    Qt Code:
    1. painter.drawText(this->rect(), Qt::AlignCenter|Qt::TextWrapAnywhere, text());
    To copy to clipboard, switch view to plain text mode 
    And you can even adjust the sizeHint function for the new "space requirement".

Similar Threads

  1. Replies: 1
    Last Post: 13th August 2010, 06:28
  2. problems with getting height and width of widget
    By martinn in forum Qt Programming
    Replies: 3
    Last Post: 10th March 2010, 03:04
  3. QLayout: different SizeConstraints for width and height?
    By PhilippB in forum Qt Programming
    Replies: 0
    Last Post: 23rd February 2009, 16:33
  4. Pixmap height / width without loading it
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 07:10
  5. width and height of QTabWidget
    By chikkireddi in forum Qt Programming
    Replies: 6
    Last Post: 29th October 2007, 13:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.