Results 1 to 9 of 9

Thread: resize QTextEdit

  1. #1
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default resize QTextEdit

    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:
    Qt Code:
    1. textArea = new KTextEdit;
    2.  
    3. textArea->setMinimumSize(256,384);
    4. textArea->resize(400,600);
    5.  
    6. scrollArea = new QScrollArea;
    7. scrollArea->setWidget(textArea);
    8. scrollArea->setBackgroundRole(QPalette::Dark);
    9. scrollArea->setAutoFillBackground(true);
    10.  
    11. setCentralWidget(scrollArea);
    To copy to clipboard, switch view to plain text mode 

    and now resizing gives wrong sizes.

    Qt Code:
    1. void MainWindow::zoomOut()
    2. {
    3. qDebug() << "zoomOut before:" << textArea->viewport()->size(); // is ok
    4. qDebug() << (0.8 * textArea->viewport()->size()); //is ok
    5. textArea->resize(0.8 * textArea->viewport()->size());
    6. qDebug() << "zoomOut after:" << textArea->viewport()->size(); // 6 points to small??
    7. }
    8.  
    9. void MainWindow::zoomIn()
    10. {
    11. qDebug() << "zoomIn before:" << textArea->viewport()->size(); //is ok
    12. qDebug() << (1.25 * textArea->viewport()->size()); //is ok
    13. textArea->resize(1.25 * textArea->viewport()->size());
    14. qDebug() << "zoomIn after:" << textArea->viewport()->size(); //6 points to small??
    15. }
    To copy to clipboard, switch view to plain text mode 

    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
    Qt Code:
    1. scrollArea->setBackgroundRole(QPalette::Dark);
    To copy to clipboard, switch view to plain text mode 
    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!

  2. #2
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: resize QTextEdit

    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.

  3. #3
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: resize QTextEdit

    If i try to resize the viewport nothing happens.

    Qt Code:
    1. textArea->viewport->resize(0.8 * textArea->viewport()->size());
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: resize QTextEdit

    i'm not sure if i understand your problem??

    you've said that:
    Qt Code:
    1. qDebug() << "zoomOut after:" << textArea->viewport()->size(); // 6 points to small??
    To copy to clipboard, switch view to plain text mode 
    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.

  5. #5
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: resize QTextEdit

    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:
    Qt Code:
    1. scrollArea->setAlignment(Qt::AlignCenter);
    To copy to clipboard, switch view to plain text mode 
    removing:
    Qt Code:
    1. scrollArea->setAutoFillBackground(true);
    To copy to clipboard, switch view to plain text mode 
    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?

  6. #6
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: resize QTextEdit

    i haven't used it yet, but i think you should use void QFont::setStretch ( int factor )

  7. #7
    Join Date
    Apr 2007
    Posts
    76
    Thanks
    6
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

  8. The following user says thank you to codebehind for this useful post:

    maartenS (21st September 2008)

  9. #8
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: resize QTextEdit

    Quote Originally Posted by codebehind View Post
    Thanks! I'll try that one!

  10. #9
    Join Date
    Sep 2008
    Posts
    17
    Thanks
    4
    Thanked 5 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: resize QTextEdit

    Trying
    Qt Code:
    1. QFont currentFont = textArea->font();
    2. currentFont.setStretch(125)
    To copy to clipboard, switch view to plain text mode 
    doesn't have any effect.
    I tried this:
    Qt Code:
    1. qreal currentFontPointSize = textArea->font().pointSize();
    2. qreal newFontPointSize = currentFontPointSize * 0.8;
    3. qreal fontPointSizeChange = currentFontPointSize - newFontPointSize;
    4. textArea->zoomOut(fontPointSizeChange);
    To copy to clipboard, switch view to plain text mode 
    for zooming out and
    Qt Code:
    1. qreal currentFontPointSize = textArea->font().pointSize();
    2. qreal newFontPointSize = currentFontPointSize * 1.25;
    3. qreal fontPointSizeChange = newFontPointSize - currentFontPointSize;
    4. textArea->zoomIn(fontPointSizeChange);
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. QSizeF currentDocumentSize = textArea->document()->size();
    2. textArea->document()->setPageSize(0.8 * currentDocumentSize);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. textArea->document()->setPageSize(textArea->size());
    To copy to clipboard, switch view to plain text mode 
    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?

Similar Threads

  1. QTextEdit super slow resize
    By bunjee in forum Qt Programming
    Replies: 5
    Last Post: 13th May 2008, 17:09
  2. QTextEdit slow to insert text
    By thomaspu in forum Qt Programming
    Replies: 4
    Last Post: 10th January 2008, 12:05
  3. QTextEdit auto resize
    By bunjee in forum Qt Programming
    Replies: 4
    Last Post: 26th October 2007, 19:59
  4. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 06:03
  5. postponing resize event
    By Honestmath in forum Qt Programming
    Replies: 11
    Last Post: 26th February 2006, 00:32

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.