Results 1 to 7 of 7

Thread: Changing Fonts

  1. #1
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Changing Fonts

    Hi,
    I have successfully implemented Dynamic Translations, the problem now is for some languages the font must change. i.e Asian Fonts , Latin Fonts.
    What is the easiest/cleanest way to change the system font so that my ui forms, qgraphicsview uses this font?
    I used qApp->setFont().. however, in most of my forms there are specific font settings for comboboxes, lineedits, etc. The family remains the same, but for Asian fonts I must increase the pointsize.
    Should I make the Ui form inherit the system font setting and then change each widget (combobox, linedit.etc) pointsize to the appropriate value? (seems kind of tedious).

    Thanks for you input.

  2. #2
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Changing Fonts

    maybe you can try doing it with style sheets. For example in my app I have font settings fo QPlainTextEdit for opened file in tabs, so instead of applying it for every QPlainTextEdit in every tab, I'm just applying as a style sheet for application:
    Qt Code:
    1. QString stylesheet = QString("QPlainTextEdit { font-size: %1pt; }").arg(m_editFontSize);
    2. qApp->setStyleSheet(stylesheet);
    To copy to clipboard, switch view to plain text mode 

    when you put QWidget there it will change font for every QWidget. Then you can use specific font settings for comboboxes with style sheet also - in that case the QComboBox will use QComboBox style sheet than just QWidget style sheet.
    Last edited by faldzip; 10th April 2009 at 07:03.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

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

    QbelcorT (10th April 2009)

  4. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Changing Fonts

    Each widget receives a LanguageChangeEvent when the language changes. You can react on this event and adjust the font of each widget. You can install an event filter on each of the widgets you want to monitor (or on the application object - then you'll intercept events for all objects) and change the fonts from this filter using an earlier prepared mapping.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. The following user says thank you to wysota for this useful post:

    QbelcorT (10th April 2009)

  6. #4
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Changing Fonts

    Thanks Faldzip, I tried as you suggested. I put all my stylesheets here ( QLineEdits, Comboboxes, etc). Your implementation made me look at my stylesheet implementation again, so I redid it and it's much simpler than before. However the bad news is I setStyleSheet on qApp when a language is changed, in my system when that happened it went into the weeds, displaying alot of disconnect/connect warnings. It worked once on powerup, but then I manually changed the language the errors occurred. I decided to set my stylesheet in main, only one time.
    Wysota, thanks, I already react to this event in one of my windows, and all my windows are being updated.
    Seems to be working. I have a function in each window (retranslate) that is called on a language change event, in that function I also prepare my fonts. I just thought there would be an easier way so I don't have to customize each of my windows in the retranslate function.

    Qt Code:
    1. QString stylesheet = QString("QLineEdit { font-size: %1pt; font-weight:bold; color: rgb(93,93,93); background-color: rgb(197,198,199); }").arg(24);
    2. // stylesheet.append(QString("QAbstractItemView {border:2px solid rgb(182,184,185); border-radius: 3px; padding-left:9px; padding-right:9px; selection-background-color: rgb(255, 174, 0);gridline-color:gray; color: rgb(93,93,93);}"));
    3. stylesheet.append(QString("QListView { show-decoration-selected: 1;} QListView::item:alternate { background: transparent;} QListView::item:selected:active{ background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #cccccc, stop: 1 #555555);} QListView::item:selected:!active{ background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,stop: 0 #cccccc, stop: 1 #555555);}"));
    4. stylesheet.append(QString("QSlider::groove:horizontal {border-image: url(icons:guage_cursor_bg1.png);border-width: 1px; height: 31px;}QSlider::handle:horizontal {margin-top:0px; margin-bottom:-1px; image: url(icons:guage_cusor_n.png);}"));
    5. stylesheet.append(QString("QLabel {font-size: 24pt; font-weight:bold; color: rgb(136,137,138);}"));
    6. stylesheet.append(QString("QCheckBox {spacing: 15px; font-weight:bold; font-size: 24px; color: rgb(136,137,138);} QCheckBox::indicator {width:28px; height:28px;} QCheckBox::indicator:unchecked {image:url(icons:box_n.png);} QCheckBox::indicator:checked {image: url(icons:box_c.png);}"));
    7. stylesheet.append(QString("QRadioButton {spacing: 15px; font-weight:bold; font-size: 24px; color: rgb(136,137,138);} QRadioButton::indicator {width:35px; height:35px;} QRadioButton::indicator:unchecked {image:url(icons:radio_n.png);} QRadioButton::indicator:checked {image: url(icons:radio_s.png);}"));qApp->setStyleSheet(stylesheet);
    To copy to clipboard, switch view to plain text mode 

  7. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,364
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Changing Fonts

    Quote Originally Posted by QbelcorT View Post
    I just thought there would be an easier way so I don't have to customize each of my windows in the retranslate function.
    You don't have to. Just install an event filter on the application object and implement the font update in one place.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #6
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Changing Fonts

    Thanks Wysota, I should have thought about that, it worked perfectly.
    I tried updating the stylesheet when the language was changed qApp->setStyleSheet("somestyleSheet"); however as I mentioned in my first comment the system crashed and showed alot of warning, disconnect and connect, so I did this qApp->setStyleSheet(QString()); then set my own stylesheet qApp->setStyleSheet("somestylesheet"); and it worked. Seems you have to set a blank one first before you reload a new sheet, but there is a performance hit, it takes about 2-3 seconds to update the language, so I removed the updated of the stylesheet when changing languages.
    Some strange sideeffects of updating the language, is that some of my labels on my widgets are missing characters.. i.e "My Label" will appear as "My L bel", then if I touch the widget (touchscreen) .. I guess painting update, the label appears fine. I noticed also some characters are bunched together, until I refresh.

  9. #7
    Join Date
    Jul 2008
    Posts
    139
    Thanks
    9
    Thanked 18 Times in 15 Posts
    Qt products
    Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Changing Fonts

    Hi,
    Just an update.
    I fixed the character refreshing problem, and the occasionally segmentation fault. Instead of using the TTF font file I created a qpf2 font file and now it seems to be ok.

Similar Threads

  1. QwtPlot - Changing Title fonts
    By MrGarbage in forum Qwt
    Replies: 11
    Last Post: 26th April 2012, 08:16
  2. Windows fonts not working in QT?
    By frenetic in forum Installation and Deployment
    Replies: 2
    Last Post: 10th January 2009, 02:17
  3. How to use qpf fonts
    By yagabey in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 18th September 2008, 07:56
  4. Replies: 0
    Last Post: 7th April 2008, 14:27
  5. Changing colour thru qss file
    By phillip_Qt in forum Qt Programming
    Replies: 3
    Last Post: 27th March 2008, 05:36

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.