i know nothing
i know nothing

Reputation: 1002

Qt stylesheet and QString::arg()

To keep a Stylesheet dynamic regarding DPI settings, I want to set certain parts of it from code.

This works:

my_label->setStyleSheet( QString( "font-size: 30px;" ) );

Yet, this doesn't:

my_label->setStyleSheet( QString( "font-size: %1px;" ).arg( 30 ) );

Can anyone enlighten me, why this is? Running Qt 5.7.

Upvotes: 0

Views: 1915

Answers (2)

i know nothing
i know nothing

Reputation: 1002

The issue was a combination of two things:

  1. I needed to convert the value to a string first
  2. The actual value passed to the arg() during runtime had a decimal place, the "30" was just for testing

While I did try different combinations (integer values (30), converting to QString first), I did not try using an integer value AND converting it to QString together. So everyone was kinda right. Thanks for the patience.

Upvotes: 2

mohabouje
mohabouje

Reputation: 4050

Conver the number to a string, QString::number:

my_label->setStyleSheet(QString("QLabel{font-size: %1 px;}").arg(QString::number(30));

Or try it by using QFont, use this generic function for this purpose:

void updateFontSize(QLabel* label, int fontSize) {
    QFont font = label->font();
    if (font.pointSize() != fontSize) {
        font.setPointSize(fontSize);
        label->setFont(font);
    }
}

Upvotes: -1

Related Questions