Results 1 to 4 of 4

Thread: QText, how to only change font family?

  1. #1
    Join Date
    Apr 2019
    Posts
    8
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default QText, how to only change font family?

    Hello. Sorry for the stupid question, but I've written up some code that I expected to only change font family, but it also changes the font size.

    Qt Code:
    1. int id = QFontDatabase::addApplicationFont(font_path);
    2. if(id != -1)
    3. {
    4. QStringList family = QFontDatabase::applicationFontFamilies(id);
    5. ui_vp_message->setFont(family.at(0));
    6. }
    To copy to clipboard, switch view to plain text mode 

    I have tried specifically using setFontFamily, but for some reason this does not change the font at all.
    Am I approaching this the wrong way? Is there some other way I could change the font family?
    Last edited by UriTK; 9th April 2019 at 11:43. Reason: QText fits better than QFont in this context.

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QText, how to only change font family?

    You are creating a new font object as setFont() takes on of these.

    If you only want to modify one property of a font, you first retrieve the current font object, the modify the value you want to change and then set the modified object back on the widget.

    Qt Code:
    1. QFont font = widget->font();
    2. // modify font
    3. widget->setFont(font);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    UriTK (9th April 2019)

  4. #3
    Join Date
    Apr 2019
    Posts
    8
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QText, how to only change font family?

    Qt Code:
    1. QFont font = widget->font();
    2. // modify font
    3. widget->setFont(font);
    To copy to clipboard, switch view to plain text mode 

    Thanks for the answer, it's a big ass leap in the right direction, but to verify if I'm majorly fucking up or not, this piece of code should then, in theory, only really change the font family, correct?

    Qt Code:
    1. //add font to database
    2. int id = QFontDatabase::addApplicationFont(font_path);
    3. if(id != -1)
    4. {
    5. //get the font family name from said font
    6. QStringList family = QFontDatabase::applicationFontFamilies(id);
    7. //change currentfont's family to ^^^
    8. currentfont.setFamily(family.at(0));
    9. ui_vp_message->setFont(currentfont);
    10. }
    To copy to clipboard, switch view to plain text mode 

    Maybe I'm not modifying it the right way but for some reason it keeps changing pointSize to the default value, instead of keeping the previous one.
    As a sidenote, I do have QFont currentfont defined a bit above the code I showed.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QText, how to only change font family?

    This is strange but maybe the new font doesn't have that particular size.

    If the pointSize is the only thing that changes with the family you could "remember" it as well

    Qt Code:
    1. currentfont = ui_vp_message->font();
    2. const int pointSize = currentFont.pointSize();
    3. currentFont.setFamily(...);
    4. currentFont.setPointSize(pointSize);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    UriTK (9th April 2019)

Similar Threads

  1. Replies: 3
    Last Post: 11th October 2011, 18:58
  2. Requested font family failed
    By wirasto in forum Qt Programming
    Replies: 0
    Last Post: 20th November 2010, 05:30
  3. Font family for non-latin text.
    By chandan in forum Newbie
    Replies: 0
    Last Post: 25th May 2010, 14:01
  4. QTextEdit and font family
    By giusepped in forum Qt Programming
    Replies: 6
    Last Post: 4th June 2008, 11:17
  5. Replies: 1
    Last Post: 25th December 2007, 11:35

Tags for this Thread

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.