PDA

View Full Version : resize QTextEdit



maartenS
20th September 2008, 14:05
Hi,

I'm trying to resize a QTextEdit to make it possible to zoom in and out.
I'm not satisfied with the result and I'm wondering if I should do it this way.
This is what I did:


textArea = new KTextEdit;

textArea->setMinimumSize(256,384);
textArea->resize(400,600);

scrollArea = new QScrollArea;
scrollArea->setWidget(textArea);
scrollArea->setBackgroundRole(QPalette::Dark);
scrollArea->setAutoFillBackground(true);

setCentralWidget(scrollArea);


and now resizing gives wrong sizes.



void MainWindow::zoomOut()
{
qDebug() << "zoomOut before:" << textArea->viewport()->size(); // is ok
qDebug() << (0.8 * textArea->viewport()->size()); //is ok
textArea->resize(0.8 * textArea->viewport()->size());
qDebug() << "zoomOut after:" << textArea->viewport()->size(); // 6 points to small??
}

void MainWindow::zoomIn()
{
qDebug() << "zoomIn before:" << textArea->viewport()->size(); //is ok
qDebug() << (1.25 * textArea->viewport()->size()); //is ok
textArea->resize(1.25 * textArea->viewport()->size());
qDebug() << "zoomIn after:" << textArea->viewport()->size(); //6 points to small??
}


1. After every resize there are 6 points lost, and I don't know where they've gone.
Any ideas?

2. Is it possible to put the QTextEdit widget in the middle of the QScrollArea.

3. The background of the scrollbars is the background set with

scrollArea->setBackgroundRole(QPalette::Dark);
I would like to integrate it better with the mainwindow.

In the QT tutorials there is an example (viewimage) that uses a QLabel for this. Is this a better way to zoom a widget? I need to make a QTextDocument and I want to display tables and I couldn't find a possibility for this with QLabel.

I have the feeling I'm on the wrong track. Can someone help me a bit further? Thanks!

codebehind
21st September 2008, 10:13
1. After every resize there are 6 points lost, and I don't know where they've gone.
Any ideas?

on my machine that code does not resize viewport at all. it resize just the textedit widget.

maartenS
21st September 2008, 13:31
If i try to resize the viewport nothing happens.



textArea->viewport->resize(0.8 * textArea->viewport()->size());

codebehind
21st September 2008, 17:24
i'm not sure if i understand your problem??

you've said that:

qDebug() << "zoomOut after:" << textArea->viewport()->size(); // 6 points to small??
but you haven't resize the view port at all. How can it be 6 points to small? in your code you are resizing a texeedit window not its viewport.

maartenS
21st September 2008, 20:04
You're right, I messed up with textArea->size() and textArea->viewport()->size()...
The six points is the difference in size between the textArea and the viewport of it, I guess.

In the meantime I fixed the other problems as well:

I centred the QTextArea in the centre of the QScrollArea by:


scrollArea->setAlignment(Qt::AlignCenter);

removing:


scrollArea->setAutoFillBackground(true);

fixed the problem with the background of the scrollbar.

Now I'm wondering how I can scale the contents of the QTextEdit. I tried using zoomIn and zoomOut but I can't get the scaling the same proportions as the scaling of the QTextEdit. Is it possible to scale text from a QTextEdit to a certain percentage?

codebehind
21st September 2008, 21:19
i haven't used it yet, but i think you should use void QFont::setStretch ( int factor )

codebehind
21st September 2008, 21:23
void QFont::setStretch ( int factor ) (http://http://doc.trolltech.com/4.4/qfont.html#setStretch)

maartenS
21st September 2008, 22:19
void QFont::setStretch ( int factor ) (http://http://doc.trolltech.com/4.4/qfont.html#setStretch)
Thanks! I'll try that one!

maartenS
22nd September 2008, 23:02
Trying

QFont currentFont = textArea->font();
currentFont.setStretch(125)
doesn't have any effect.
I tried this:


qreal currentFontPointSize = textArea->font().pointSize();
qreal newFontPointSize = currentFontPointSize * 0.8;
qreal fontPointSizeChange = currentFontPointSize - newFontPointSize;
textArea->zoomOut(fontPointSizeChange);
for zooming out and


qreal currentFontPointSize = textArea->font().pointSize();
qreal newFontPointSize = currentFontPointSize * 1.25;
qreal fontPointSizeChange = newFontPointSize - currentFontPointSize;
textArea->zoomIn(fontPointSizeChange);
for zooming in and that works!

Only problem I now have is that the fonts of the QtextDocument are also changing and I want to keep that the same, because I'm making a pdf of the QTextDocument.
When I zoomOut the QTextEdit I get very small fonts in the pdf and I want to keep the same proportions. That's why I thought of changing the size of the QTextDocument as well.
I tried:


QSizeF currentDocumentSize = textArea->document()->size();
textArea->document()->setPageSize(0.8 * currentDocumentSize);

and


textArea->document()->setPageSize(textArea->size());

both don't give the good size, they make the QTextEdit a tiny bit to large.

Do you have any idea how I can resize the QTextEdit while keeping the QTextDocument the same size for pdf and printing?
Or am I trying to do things with a QTextWidget that are not possible and should I use an own Widget?