PDA

View Full Version : Height and position of QTextEdit



el33t
16th February 2011, 15:00
Please take a look at the below screenshot of my app:

http://i55.tinypic.com/2ni368k.jpg

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:


{
resize(880, 580);

txt = new QTextEdit("qt", this);
txt-> move(0,100);

std::cout<<txt->height()<<"::"<<txt->y();


}

Regards,

agarny
16th February 2011, 15:17
I don't think it's possible for us to help you unless you give us some code to look at.

el33t
16th February 2011, 16:03
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.

agarny
16th February 2011, 16:11
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...

nightghost
16th February 2011, 16:31
I assume you calculate the height before the layouting is finished:



QTextEdit* edit = new QTextEdit(this);
qDebug() << "HIDDEN with parent:" << edit->height();
edit->show();
qDebug() << "VISIBLE with parent:" << edit->height();
edit->hide();
edit->setParent(0);
qDebug() << "HIDDEN without parent:" << edit->height();
edit->show();
qDebug() << "VISIBLE without parent:" << edit->height();

gives for example:


HIDDEN with parent: 30
VISIBLE with parent: 30
HIDDEN without parent: 480
VISIBLE without parent: 192

el33t
16th February 2011, 16:33
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.

agarny
16th February 2011, 16:39
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.

nightghost
16th February 2011, 16:46
Yep, maybe you have an Layout set on the parent of the QTextEdit, while he has no layout set.



QDialog dialog(this);
dialog.setLayout(new QHBoxLayout()); // remove this line and the text edit will be big
dialog.setFixedSize(500,500);

QTextEdit* edit = new QTextEdit(&dialog);

dialog.exec();

agarny
16th February 2011, 16:49
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.

el33t
16th February 2011, 16:54
Ok, something REALLY freaky! I edited my source and look at the result.


resize(880, 580);

txt = new QTextEdit("qt", this);
txt-> move(0,100);

txt->resize(100,30); ////////////////////NOTICE THIS RESIZE

std::cout<<txt->height()<<"::"<<txt->y();
txt->setReadOnly(true);

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.

http://i53.tinypic.com/24d4rw8.jpg

What the hell !?!?!?!

nightghost
16th February 2011, 17:16
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):



{
resize(880, 580);

txt = new QTextEdit("qt", this);
txt-> move(0,100);

QApplication::processEvents();

std::cout<<txt->height()<<"::"<<txt->y();


}

el33t
17th February 2011, 05:42
ummm... I guess I'll just resize it explecitely to 30 pixels since that serves my purpose.

agarny
17th February 2011, 08:36
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'.

el33t
17th February 2011, 09:03
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

agarny
17th February 2011, 09:18
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.


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... :))

SixDegrees
17th February 2011, 09:19
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.

el33t
17th February 2011, 09:25
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.