Results 1 to 7 of 7

Thread: QTextEdit and font family

  1. #1
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QTextEdit and font family

    I have a basic quetion.
    I would change the font family of a QTextEdit by means of the following code:

    void Window::chooseFont()
    {
    bool ok;
    QFont initial("Times New Roman", 48);
    QFont font = QFontDialog::getFont(&ok, initial, this);

    if (ok) {
    QTextCharFormat format;
    format.setFont(font);
    txt->mergeCurrentCharFormat(format);


    }

    }


    The strange thing is that I get changed the text if and only if I make a selection inside txt. i.e. the character format does not change if a continue to edit on the right of the current cursor position.
    Any help appreciated

  2. #2
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit and font family

    Why dont u simply use QWidget::setFont ??
    txt->setFont(font) ??

  3. #3
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit and font family

    Simply because the result is the same. I tryed your tip without success.
    G

  4. #4
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit and font family

    In some way I solved.


    void Window::chooseFont()
    {
    bool ok;
    QFont initial("Times New Roman", 48);
    QFont font = QFontDialog::getFont(&ok, initial, this);

    if (ok) {
    QTextCharFormat format;
    format.setFont(font);
    txt->setCurrentFont(font);
    txt->setEnabled(true);

    }
    txt->setWindowState(Qt::WindowActive);
    }

    Anyway, still the behavior is not perfect.
    When I click on the Font button the QFontDialog opens, I select the font, and when I close the Dialog, if I press Tab the focus go on the QTextEdit and I can type with the selected font.
    On the other hand, if I close the QFontDialog and I do not press Tab, bu use the mouse to get the focus on QTextEdit, I cannot type with the selected font.
    There is a way to understand why?
    Regards

  5. #5
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit and font family

    to handle text on QTextEdit , QTextCursor make the job ...

    Qt Code:
    1. void FontText()
    2. {
    3. /* get existing font txt = QTextEdit */
    4. QTextCursor c = txt->textCursor();
    5. QTextCharFormat format = c.charFormat();
    6. QFont f = format.font();
    7.  
    8. bool ok;
    9. QFont fontyour = QFontDialog::getFont(&ok,f,0);
    10. if (ok) {
    11.  
    12. format.setFont(fontyour);
    13. c.setCharFormat(format);
    14.  
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

  6. #6
    Join Date
    May 2008
    Posts
    276
    Thanks
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QTextEdit and font family

    Did you try your code?
    Because the result is the same: if I click insiede the QTextEdit the font selection is lost.

  7. #7
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QTextEdit and font family

    Quote Originally Posted by giusepped View Post
    Did you try your code?
    Because the result is the same: if I click insiede the QTextEdit the font selection is lost.
    Cursor chance if you click on other area..
    Yes i try the code is part from... my GraphicsViewEdit
    http://www.qt-apps.org/content/show....?content=80234

    IMO: mi prendi in giro?

Similar Threads

  1. Problem pasting text into a QTextEdit
    By Spockmeat in forum Qt Programming
    Replies: 8
    Last Post: 14th March 2009, 15:36
  2. QTextEdit API questions (plain text)
    By Gaspar in forum Qt Programming
    Replies: 4
    Last Post: 16th May 2006, 07:03

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.