PDA

View Full Version : QPlainTextEdit ScrollBar problem



linuxsong
21st September 2010, 17:04
I had a question, see these code first:

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.resize(500,500);
QPlainTextEdit *text = new QPlainTextEdit(&w);
QString str;
for (int i = 0; i < 50; i++) {
str += "text text text\n";
}
text->setGeometry(QRect(30,30, 200,200));
text->setPlainText(str);
qDebug() << "minimum: " << text->verticalScrollBar()->minimum();
qDebug() << "maximum" << text->verticalScrollBar()->maximum();
qDebug() << text->verticalScrollBar()->value();
text->verticalScrollBar()->setValue(30);
qDebug() << "show before value:" << text->verticalScrollBar()->value();

w.show();
qDebug() << "show after value:" << text->verticalScrollBar()->value();

return a.exec();
}

The output result is :
minimum: 0
maximum 50
0
show before value: 30
show after value: 0

So, why the text->verticalScrollBar()->value() changed after the window show() ?

tbscope
21st September 2010, 17:06
Because a widget is resized before it is shown

linuxsong
21st September 2010, 17:16
@tbscope Thank you for your answer.
Is your meen the text widget is resized before it is shown?
I changed the code like this:
...
qDebug() << text->size();
w.show();
qDebug() << "show after value:" << text->verticalScrollBar()->value();
qDebug() << text->size();

The output result is:
...
QSize(200, 200)
show after value: 0
QSize(200, 200)

The size is same with before it is shown.
so why?

wysota
21st September 2010, 18:56
Text layout is only calculated when required. It is required for the first time when the layout needs to be shown for the first time. It needs to be shown for the first time when the widget containing the document is shown for the first time. Only then the document size is calculated which influences the value of scrollbar.

linuxsong
21st September 2010, 19:37
Is that mean if I wan't to set the scrollbar value I must do it when the windows is shown?
Is there any solusion to be set the scrollbar value when the windown is not shown?
Thanks.

wysota
21st September 2010, 19:40
Is that mean if I wan't to set the scrollbar value I must do it when the windows is shown?
After it is shown for the first time. It can be hidden when you do it, but it must have had been shown earlier.