2

I have a weird issue with a Qt application.

I set a custom font to be used across the entire application using:

int main(int argc, char **argv) {
    QApplication app(argc, argv);
    app.setFont(QFont("Spectral", 12));
    ...
}

I also have a widget—a class that inherits from QWidget—that adds some text to QGraphicsScene like this:

auto t0 = this->scene->addText("Hello, World!");
t0->setPos(10, 10);
t0->setDefaultTextColor(textColor);

It works perfectly on a Debian 12 machine with Qt 6. However, the same code, compiled for Raspberry Pi (Debian 11, Qt 5) has a weird issue. The first time the widget renders, it uses the correct font. However, any successive redraw uses the default system font.

If I log the font associated with the scene object during the redraw, like this:

auto font = this->scene->font();
qDebug().nospace() << "Using font " << font.toString();

this is what I get:

Using font "Spectral,12,-1,5,50,0,0,0,0,0"
Using font "PibotoLt,12,-1,5,50,0,0,0,0,0"
Using font "PibotoLt,12,-1,5,50,0,0,0,0,0"
Using font "PibotoLt,12,-1,5,50,0,0,0,0,0"
...

showing that the scene, indeed, reverts to the system font on the second redraw.

What am I doing wrong? Why is the scene forgetting the custom font?

Arseni Mourzenko
  • 50,338
  • 35
  • 112
  • 199
  • 1
    Maybe related: https://stackoverflow.com/questions/63035326/change-button-font-of-qmessagebox-in-pyqt5-when-using-setstylesheet mentions that applying style sheets can prevent font settings being applied in Qt5. – Marijn Aug 05 '23 at 20:00

0 Answers0