Results 1 to 13 of 13

Thread: heightforWidth

  1. #1
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default heightforWidth

    Hi All,
    How to get the heightForWidth for a widget. I always get the value as -1. Is there anything I'm missing?

    The following is my code.

    Qt Code:
    1. TextEdit::TextEdit(QWidget *parent)
    2. :QTextEdit(parent)
    3. {
    4. setGeometry( geometry().x(), geometry().y(), parent->width(), 20 );
    5. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    6. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    7.  
    8. QSizePolicy sizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    9. sizePolicy.setHeightForWidth(true);
    10. setSizePolicy(sizePolicy);
    11. connect( this, SIGNAL( textChanged() ), this, SLOT( slotTextChanged() ) );
    12. setMouseTracking( true );
    13. }
    14. TextEdit::~TextEdit()
    15. {}
    16.  
    17. void TextEdit::slotTextChanged()
    18. {
    19. qDebug() << heightForWidth ( width() );
    20. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication a(argc, argv);
    4. QWidget widget;
    5. widget.resize( 400, 20 );
    6. TextEdit* textEdit = new TextEdit( &widget );
    7. widget.show();
    8. a.connect(&a, SIGNAL(lastWindowClosed()), &a, SLOT(quit()));
    9. return a.exec();
    10. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: heightforWidth

    Did you implement heightForWidth() method for your widget? You get -1 because that's what the default implementation returns.

  3. #3
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: heightforWidth

    Quote Originally Posted by wysota View Post
    Did you implement heightForWidth() method for your widget? You get -1 because that's what the default implementation returns.
    I had used QTextEdit::heightForWidth() in Qt3. I cannot find an alternative in Qt4.

    BTW: If I have to implement my own heightForWidth() then I dont see any reasons Trolls defining it for us.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: heightforWidth

    Quote Originally Posted by vermarajeev View Post
    I had used QTextEdit::heightForWidth() in Qt3. I cannot find an alternative in Qt4.
    Quote Originally Posted by Qt4 reference
    int QWidget::heightForWidth ( int w ) const [virtual]
    Returns the preferred height for this widget, given the width w.
    If this widget has a layout, the default implementation returns the layout's preferred height. if there is no layout, the default implementation returns -1 indicating that the preferred height does not depend on the width.
    BTW: If I have to implement my own heightForWidth() then I dont see any reasons Trolls defining it for us.
    I'm afraid I don't understand.

  5. #5
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: heightforWidth

    Quote Originally Posted by wysota View Post
    I'm afraid I don't understand.
    What I meant is, Do I really need to implement heightForWidth() in QTextEdit. If I have to do so then why do Trolls define QWidget::heightForWidth()?
    I'm looking for some built in function provided by Qt4 rather than writting my own functionality.


    f this widget has a layout, the default implementation returns the layout's preferred height. if there is no layout, the default implementation returns -1 indicating that the preferred height does not depend on the width.
    Though I defined a layout for my widget in the above sample, why I always get the value as -1?

    Can you please correct the sample that I wrote, above.

    Thanks

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: heightforWidth

    Quote Originally Posted by vermarajeev View Post
    What I meant is, Do I really need to implement heightForWidth() in QTextEdit.
    Depends what you want to achieve.

    If I have to do so then why do Trolls define QWidget::heightForWidth()?
    So that you can use the "heightForWidth" feature... I seem still not to understand your question. Do you know what heightForWidth is for?

    Though I defined a layout for my widget in the above sample, why I always get the value as -1?
    You defined a layout for QTextEdit? heightForWidth returns the size of the layout of the widget, not of the widget's parent.

  7. #7
    Join Date
    Sep 2006
    Posts
    339
    Thanks
    15
    Thanked 21 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: heightforWidth

    I want to resize the QTextEdit so I don't get a vertical scroll bar, based on the
    height of the formatted text. I had used QTextEdit::heightForWidth() in Qt3. The following code was written in Qt3. I want to implement the same in Qt4. Can you please help?

    Qt Code:
    1. void TextEdit::slotTextChanged()
    2. {
    3. int height = heightForWidth(width());
    4. resize(width(), height);
    5. }
    To copy to clipboard, switch view to plain text mode 

    The slotTextChanged() is connected as follows:
    Qt Code:
    1. connect(this, SIGNAL(textChanged()), this, SLOT( slotTextChanged()));
    To copy to clipboard, switch view to plain text mode 

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: heightforWidth

    Qt Code:
    1. QTextEdit *te = new ...;
    2. //...
    3. QTextDocument *d = te->document();
    4. QSize docSize = d->size();
    5. int l, t, r, b;
    6. te->getContentsMargins(&l, &t, &r, &b);
    7. docSize+=QSize(l+r, t+b);
    8. docSize+=QSize(10,10); // just in case ;)
    9. te->resize(docSize);
    To copy to clipboard, switch view to plain text mode 
    Something like that should do the trick. heightForWidth is for something completely different - it tells the layout that the widget can sacrifice some of its width exchanging that for an additional height (like a label with wordwrapping turned on).

  9. The following user says thank you to wysota for this useful post:

    vermarajeev (11th September 2007)

  10. #9
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: heightforWidth

    wysota, nice solution

    How about arbitrary widget which defines hasHeightForWidth()?
    For example word wrapped QLabel?
    Do you know any solution in this case? I'd greatly appressiate.
    I experience a whole lot of problems if I have word-wrapped QLabel in my layout. (like all size policies being ignored)

  11. #10
    Join Date
    Apr 2007
    Location
    Bangalore,India
    Posts
    25
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Unhappy Re: heightforWidth

    Hello guys,,

    In what scenario the heightForWidth will return some valid value..

    Even after setting the layout,SizePolicies....

    heightForWidth function always return -1.
    Veda

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: heightforWidth

    Quote Originally Posted by dimuz View Post
    wysota, nice solution

    How about arbitrary widget which defines hasHeightForWidth()?
    For example word wrapped QLabel?
    Do you know any solution in this case? I'd greatly appressiate.
    I experience a whole lot of problems if I have word-wrapped QLabel in my layout. (like all size policies being ignored)
    Take a look at the source code of QLabelPrivate::sizeForWidth. It is the place where the preferred height of the label is calculated. As you can see QLabel uses a QTextDocument behind the scenes (through QTextControl), so you can use the exact same approach with QLabel as with QTextEdit.

    Using QTextDocument::setTextWidth you can tell the document to calculate the size for this particular width and then query for the size using size().

    But if your policies are ignored then it could mean your layout is messed up, as you shouldn't experience the effects you described with a word wrapping label.

  13. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: heightforWidth

    Quote Originally Posted by veda View Post
    Hello guys,,

    In what scenario the heightForWidth will return some valid value..

    Even after setting the layout,SizePolicies....

    heightForWidth function always return -1.
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include <QtDebug>
    4.  
    5. class W : public QWidget {
    6. public:
    7. W(QWidget *p=0) : QWidget(p){
    8. QSizePolicy poli(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding);
    9. poli.setHeightForWidth(true);
    10. setSizePolicy(poli);
    11. }
    12. int heightForWidth(int w) const { return w; }
    13. QSize sizeHint() const { return QSize(100,100); }
    14. protected:
    15. void paintEvent(QPaintEvent *pe){
    16. QPainter p(this);
    17. p.drawRect(rect().adjusted(0, 0, -1, -1));
    18. }
    19. };
    20.  
    21. int main(int argc, char **argv){
    22. QApplication app(argc, argv);
    23. QVBoxLayout *l = new QVBoxLayout(&w);
    24. for(int i=0;i<5;i++)
    25. l->addWidget(new W);
    26. w.show();
    27. qDebug() << w.heightForWidth(w.width());
    28. return app.exec();
    29. }
    To copy to clipboard, switch view to plain text mode 

  14. #13
    Join Date
    Aug 2007
    Posts
    16
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: heightforWidth

    wysota
    Thanks! Cool. I have seen sizeForWidth() but i gave it a fast look, not dwelling further - and i didn't noticed that it uses QTextEdit.

    Thanks for the hint!

    As for messed up layout - nope. it isn't messed up.
    Even trolls aknowledged that this is a bug, but this is a wont fix for some reason I didn't fully understand.
    To reproduce in designer - simply create widget with QVBoxLayout, put qlabel with some wordwrapped text (expanding on more than one line) and put qlineedit in this layout too. now press 'ctrl-r' and try to resize to minimum and you'll see the Truth (TM)
    Qt 4.3.1

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.