Results 1 to 17 of 17

Thread: Height and position of QTextEdit

  1. #1
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Height and position of QTextEdit

    Please take a look at the below screenshot of my app:



    The program consists of a QTextEdit txt.

    I ran two functions as you can see from the screenshot:
    1) txt->y() // This gives the y coordinate of txt
    2) txt->height() // This gives the height of txt

    Now the outcomes are pretty confusing for me :

    1) txt->y() gives the outcome 100

    while

    2) txt->height() gives the outcome 30


    How can 1) be greater than 2). You can see clearly from the screenshot that this can't be possible.

    Any explanation would be greatly appreciated.

    EDIT: Here is the code as requested:

    Qt Code:
    1. {
    2. resize(880, 580);
    3.  
    4. txt = new QTextEdit("qt", this);
    5. txt-> move(0,100);
    6.  
    7. std::cout<<txt->height()<<"::"<<txt->y();
    8.  
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    Regards,
    Last edited by el33t; 16th February 2011 at 15:02.

  2. #2
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    I don't think it's possible for us to help you unless you give us some code to look at.

  3. #3
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by agarny View Post
    I don't think it's possible for us to help you unless you give us some code to look at.
    Added the code. Please look at the end of the first post.

  4. #4
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by el33t View Post
    Added the code. Please look at the end of the first post.
    Well, I have just tried your code and, like you, do get 100 and 30 for txt->y() and txt->height(), respectively. However, my QTextEdit widget really is 30 pixels high unlike in your screenshot. So, I am not sure what is going in your case. We clearly need more information about what you have done / tried to do...

  5. #5
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    I assume you calculate the height before the layouting is finished:

    Qt Code:
    1. QTextEdit* edit = new QTextEdit(this);
    2. qDebug() << "HIDDEN with parent:" << edit->height();
    3. edit->show();
    4. qDebug() << "VISIBLE with parent:" << edit->height();
    5. edit->hide();
    6. edit->setParent(0);
    7. qDebug() << "HIDDEN without parent:" << edit->height();
    8. edit->show();
    9. qDebug() << "VISIBLE without parent:" << edit->height();
    To copy to clipboard, switch view to plain text mode 
    gives for example:
    HIDDEN with parent: 30
    VISIBLE with parent: 30
    HIDDEN without parent: 480
    VISIBLE without parent: 192

  6. #6
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by agarny View Post
    Well, I have just tried your code and, like you, do get 100 and 30 for txt->y() and txt->height(), respectively. However, my QTextEdit widget really is 30 pixels high unlike in your screenshot. So, I am not sure what is going in your case. We clearly need more information about what you have done / tried to do...
    So you mean to say that txt is appearing bigger than how much it should actually appear?

    I'm so confused. I did not do anything that would affect the size of txt.

  7. #7
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by el33t View Post
    So you mean to say that txt is appearing bigger than how much it should actually appear?
    No, since I said that my txt is exactly 30 pixels high, while yours is clearly much bigger.

  8. #8
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    Yep, maybe you have an Layout set on the parent of the QTextEdit, while he has no layout set.

    Qt Code:
    1. QDialog dialog(this);
    2. dialog.setLayout(new QHBoxLayout()); // remove this line and the text edit will be big
    3. dialog.setFixedSize(500,500);
    4.  
    5. QTextEdit* edit = new QTextEdit(&dialog);
    6.  
    7. dialog.exec();
    To copy to clipboard, switch view to plain text mode 

  9. #9
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by nightghost View Post
    Yep, maybe you have an Layout set on the parent of the QTextEdit, while he has no layout set.
    Good thinking. That would explain things indeed.

  10. #10
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Height and position of QTextEdit

    Ok, something REALLY freaky! I edited my source and look at the result.

    Qt Code:
    1. resize(880, 580);
    2.  
    3. txt = new QTextEdit("qt", this);
    4. txt-> move(0,100);
    5.  
    6. txt->resize(100,30); ////////////////////NOTICE THIS RESIZE
    7.  
    8. std::cout<<txt->height()<<"::"<<txt->y();
    9. txt->setReadOnly(true);
    To copy to clipboard, switch view to plain text mode 

    Ok, after the resize I get a versy small perfectly 30 pixel QTextEdit. (See screenshot below). Also, txt->height() gives the correct output 30. However before I added this resize, txt->height() used to output 30 but the resultant QTextEdit used to look wierdly big for 30 pixel. Anyways, here is the screenshot after I added resize.



    What the hell !?!?!?!

  11. #11
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Thanks
    11
    Thanked 16 Times in 16 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    As far as I understand the qwidget source the size is set directly and a QResizeEvent is created, therefore you the the right results.

    You could try this (not tested, just a guess):

    Qt Code:
    1. {
    2. resize(880, 580);
    3.  
    4. txt = new QTextEdit("qt", this);
    5. txt-> move(0,100);
    6.  
    7. QApplication::processEvents();
    8.  
    9. std::cout<<txt->height()<<"::"<<txt->y();
    10.  
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

  12. #12
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Height and position of QTextEdit

    ummm... I guess I'll just resize it explecitely to 30 pixels since that serves my purpose.

  13. #13
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by el33t View Post
    ummm... I guess I'll just resize it explecitely to 30 pixels since that serves my purpose.
    I don't know what your application is all about, but personally I would never do something like that. That's a recipe for potential 'disaster'.

  14. #14
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by agarny View Post
    I don't know what your application is all about, but personally I would never do something like that. That's a recipe for potential 'disaster'.
    Really? Can you explain why? (lol, just a question. Don't come into the misunderstanding that I'm doubting your knowledge)

    Also, can you please suggest anything for my situation.

    Regards

  15. #15
    Join Date
    Mar 2010
    Posts
    319
    Thanks
    1
    Thanked 14 Times in 12 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Height and position of QTextEdit

    Quote Originally Posted by el33t View Post
    Really? Can you explain why? (lol, just a question. Don't come into the misunderstanding that I'm doubting your knowledge)
    It's just that hard-coding GUI things like this might cause problems if, for example, your user was to decide to change some of its display settings (e.g. DPI). Also, if you want your application to be cross-platform (this is one of the things I don't know about your application), then it might or not behave as expected on other platforms. In fact, seeing that you are on Windows, your application might look fine on Windows 7 (and most likely Windows Vista, or vice versa), but maybe not on Windows XP or a future version of Windows.

    Quote Originally Posted by el33t View Post
    Also, can you please suggest anything for my situation.
    Well, have you checked everything that has been said in this thread? What about the point made by nightghost about layouts?

    Anyway, personally, I would make sure to get to the bottom of your problem rather than hard-coding a fix which may or not work in all cases. (To go for a hard-coded fix is what I call (very) bad and lazy programming... )

  16. #16
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Height and position of QTextEdit

    I don't know what your 'situation' is, but it is bad form to use explicit size declarations in GUI code. Move your application to a different screen resolution, and things will break.

    You ought to be using layouts to size your components, so they'll resize properly on different displays and as the user manipulates their containing window.

    Note, also, that any size information obtained within a constructor is suspect; the object isn't "finished" yet, and many dimensions are garbage at that point.

  17. #17
    Join Date
    Jul 2010
    Posts
    34
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Height and position of QTextEdit

    Ok, thanks a lot for the explanations. I'll try to use the layout to size my widget as suggested by some of you.

    Have a nice day.

Similar Threads

  1. Changing line height in QTextEdit (Qt4)
    By ultr in forum Qt Programming
    Replies: 0
    Last Post: 19th February 2010, 15:59
  2. Replies: 2
    Last Post: 24th January 2009, 18:30
  3. QTextEdit preferred height
    By darksaga in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2007, 02:03
  4. QTextEdit - dynamic height
    By munna in forum Qt Programming
    Replies: 3
    Last Post: 26th March 2007, 04:03
  5. QTextEdit height using Qt3.3.5
    By vermarajeev in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2006, 08:43

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
  •  
Qt is a trademark of The Qt Company.