[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");
}
@ -
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");
}
@