[Solved] QFont doesn't seem to work properly



  • In the constructor below, I set the default font for all QLabels with the setStyleSheet function. I want the first label, faultLabel, to have a font other than the default, so I set it specifically. When I run the application, though, both labels have the default font set in the stylesheet. What am I doing wrong?
    @
    StatusBar::StatusBar(QWidget *parent) :
    QWidget(parent)
    {
    QFrame *statusFrame = new QFrame(parent);
    statusFrame->resize(1280, 42);
    statusFrame->setStyleSheet("QLabel{ font: 24px "MS Shell Dlg 2", sans-serif; }");

    QLabel *faultLabel = new QLabel(statusFrame);
    faultLabel->setFont(QFont("Arial", 8));
    faultLabel->move(300, 2);
    faultLabel->setText("Fault:");
    
    QLabel *dateLabel = new QLabel(statusFrame);
    dateLabel->move(1080, 2);
    dateLabel->setText("Date field");
    

    }
    @



  • If you use style sheets, member changes for font and palette are ignored.
    You must the set the font on the specific label also via style sheet.



  • That makes sense, but when I add the following line to my setStyleSheet call, I get the same result:
    @
    "QLabel#faultLabel{ font: 10px "Arial", sans-serif; }");
    @



  • That is because you have no object of that name, only a local variable :-)

    @
    StatusBar::StatusBar(QWidget *parent) :
    QWidget(parent)
    {
    QFrame *statusFrame = new QFrame(parent);
    statusFrame->resize(1280, 42);
    statusFrame->setStyleSheet("QLabel{ font: 24px "MS Shell Dlg 2", sans-serif; }"
    "QLabel#faultLabel{ font: 10px "Arial", sans-serif; }");

    QLabel *faultLabel = new QLabel(statusFrame);
    faultLabel->setFont(QFont("Arial", 8));
    faultLabel->setObjectName("faultLabel"); // <-- this is the trick
    faultLabel->move(300, 2);
    faultLabel->setText("Fault:");
    
    QLabel *dateLabel = new QLabel(statusFrame);
    dateLabel->move(1080, 2);
    dateLabel->setText("Date field");
    

    }
    @



  • Thanks - that really helped!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.