Unsolved change font of stackwidget
-
I have a GUI program debug for raspberry pi with one central widget and I use this code for change the font that add in resources file:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf"); QFont nastaliq("Nastaliq",20,QFont::Normal); w.setFont(nastaliq); w.show(); return a.exec(); }
and it's work
and I have an other program with stack-widget and use above code it doesn't change font of GUI application why?
-
Hi,
Do you mean you set the font on the stack widget ? It's just a container widget forQStackedLayout so it doesn't have a "body" like QMainWindow.
-
@SGaist I use the stack widget and I want to set the font for the content on it like push button, combobox ....
So why the font doesn't change? -
@zhmh
I useQStackedWidget
and just set application font and it works everywhere,QStackedWidget
is not relevant. -
@JonB you set font application like my code?
I use it on test project with QStackWidget and it works but in my project doesn't work ,so what's the problem ? -
If you want all your widgets to use the same font, then just set it through QApplication::setFont.
-
@SGaist I use this:
QFont newFont(":/new/prefix1/Nastaliq.ttf", 8, QFont::Bold, true); QApplication::setFont(newFont);
The size is set, but the font does not change
-
QFont doesn't take a path as parameter but the family name.
Use QFontDatabase to load your custom font, build the QFont object using the correct family name and then set it on QApplication.
-
@SGaist can you show me with code?
I used the qfontdatabase at first an doesn’t work! -
Then show the current code you use.
-
@zhmh said in change font of stackwidget:
I used the qfontdatabase at first an doesn’t work!
Can you check if
addApplicationFont
function returns -1 ?
as mentioned here -
@Ratzz yes I checked it before, this is
int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf"); qDebug()<< fnt;
returns 0 to me!
-
@zhmh said in change font of stackwidget:
returns 0 to me
Which means the fond is loaded properly.
Use http://doc.qt.io/qt-5/qfontdatabase.html#applicationFontFamilies to get the font and set back to application.
Can you show the code ?
-
int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf"); qDebug() << fnt << QFontDatabase::applicationFontFamilies(fnt);
returns: 0 ("Nastaliq")
@Ratzz said in change font of stackwidget:
Can you show the code ?
this is my main.cpp:
int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf"); qDebug() << fnt << QFontDatabase::applicationFontFamilies(fnt); QFont nastaliq("Nastaliq",20,QFont::Normal); w.setFont(nastaliq); w.show(); return a.exec(); }
-
@zhmh said in change font of stackwidget:
QFont nastaliq("Nastaliq",20,QFont::Normal);
You should set the font which you got from the http://doc.qt.io/qt-5/qfontdatabase.html#applicationFontFamilies
-
@Ratzz how ?
int fnt=QFontDatabase::addApplicationFont(":/new/prefix1/Nastaliq.ttf"); QString family = QFontDatabase::applicationFontFamilies(fnt).at(0); w.setFont(family);
is it true?
-
-
@Ratzz No :(
-
Where did you get that font from ?
-
@SGaist font work with other program