PDA

View Full Version : Changing Fonts



QbelcorT
10th April 2009, 04:03
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.

faldzip
10th April 2009, 06:58
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:


QString stylesheet = QString("QPlainTextEdit { font-size: %1pt; }").arg(m_editFontSize);
qApp->setStyleSheet(stylesheet);

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.

wysota
10th April 2009, 07:54
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.

QbelcorT
10th April 2009, 10:29
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.



QString stylesheet = QString("QLineEdit { font-size: %1pt; font-weight:bold; color: rgb(93,93,93); background-color: rgb(197,198,199); }").arg(24);
// 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);}"));
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);}"));
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);}"));
stylesheet.append(QString("QLabel {font-size: 24pt; font-weight:bold; color: rgb(136,137,138);}"));
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);}"));
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);

wysota
10th April 2009, 14:45
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.

QbelcorT
12th April 2009, 13:43
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.

QbelcorT
13th April 2009, 12:57
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.