Results 1 to 2 of 2

Thread: QTextFrameFormat issues.

  1. #1
    Join Date
    Aug 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QTextFrameFormat issues.

    I'm having a problem setting the margins and putting a border around them in a QTextEdit. I want my program to behave similar to OpenOffice, where the margins and text size scale along with the resizing of the window. I have the following code to calculate the margins as a percentage of the widget size and adjust the text so that 65 characters takes up a line of the available space.

    Qt Code:
    1. void NCTextEdit::autoScale() {
    2. margins = this->geometry().width() * 0.2;
    3. float ww = this->geometry().width() - margins;
    4. QFont font;
    5. font.setFamily("Courier");
    6. font.setPointSize(12);
    7. float fw = QFontMetrics(font).width("M") * 65; // 65 characters per line;
    8. if (fw == 0)
    9. fw = ww;
    10.  
    11. fontZoom = ww / fw;
    12. margins /= 2;
    13. setFontPointSize(12*fontZoom);
    14.  
    15. format.setTopMargin(margins);
    16. format.setBottomMargin(margins);
    17. format.setLeftMargin(margins);
    18. format.setRightMargin(margins);
    19. format.setBorder(2);
    20. format.setBorderBrush(QBrush(QColor(0, 0, 0, 50)));
    21. format.setBorderStyle(QTextFrameFormat::BorderStyle_Solid);
    22. document()->rootFrame()->setFrameFormat(format);
    23. }
    To copy to clipboard, switch view to plain text mode 

    I call this function from the resizeEvent code. Everything almost works as expected. The only hiccup is when the program first starts. When the QTextEdit is empty and the program initially starts up, the border does not appear. Neither does the cursor. Even when I click on the QTextEdit to make it active, the cursor doesn't appear. When I start typing, or resize the window, the border comes up and the cursor will be visible. This is less than ideal, because the cursor is an important visual cue to the user that it is okay to start typing.

    I know that the code above is being called on startup, but I figured that maybe there was something about it getting called before the initialization function of the QMainWindow is done that it didn't like. So I put a QTimer to call the code after a small amount of time. That didn't solve the two problems above.

    The whole thing works if I put some text in there ahead of time, but I don't want text in there, the user isn't going to expect their blank text document to not be blank. I'm not sure why it suddenly starts working if I resize the window manually, but not when the resizeEvent is called when the window is initially setup, and not when the scaling function is called after the window is setup.

    Is there something I'm missing to make the widget update?

  2. #2
    Join Date
    Aug 2012
    Posts
    11
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QTextFrameFormat issues.

    I accidentally stumbled upon the answer. I added a QTextBlockFormat change to the code and it all started working. So if anyone else is having the same problem, updating the QTextBlockFormat should fix it.

Similar Threads

  1. Issues with Uic.
    By queelius in forum Qt Programming
    Replies: 4
    Last Post: 2nd December 2010, 13:37
  2. 4.6.0 issues on OS X 10.6 with GL
    By treaves in forum Qt Programming
    Replies: 2
    Last Post: 2nd December 2009, 17:12
  3. Debug issues
    By lyucs in forum Qt Tools
    Replies: 4
    Last Post: 13th November 2009, 22:59
  4. QTableView Issues with Qt 4.5.3
    By tntcoda in forum Qt Programming
    Replies: 0
    Last Post: 5th October 2009, 20:58
  5. CPU Loading issues
    By arunvv in forum Qt Programming
    Replies: 0
    Last Post: 21st September 2009, 19:22

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.