如何保持标签字体大小不变,而不管 Qt 中的滑块变化如何?

How to keep label font size constant irrespective of slider change in Qt?

我想在 Qt 中使用标签显示滑块的值。每当滑块值更改时,都会调用槽函数。标签值在槽函数中更新。目前它有效,但并不完美。 问题 是:我在标签 属性 中将字体大小设置为 12 并加粗。但是每当滑块移动时,标签的字体大小变为 8 并且不是粗体。我通过在插槽函数中添加 setPointSizesetBold 函数解决了这个问题。但是有没有更优雅的选择呢?以下是 mainwindow.h :

private slots:
    void on_p_slider_sliderMoved(int position);

和mainwindow.cpp:

void MainWindow::on_p_slider_sliderMoved(int position)
{
    ui->p_label->setNum(position);

    //more elegant method?
    QFont fontObj;
    fontObj.setPointSize(12);
    fontObj.setBold(true);
    ui->p_label->setFont(fontObj);
}

试一试 setStyleSheet..

 lab = new QLabel(this);
 lab->setStyleSheet("background: rgb(255,255,255); color: #999999;"
                    "font-family: Arial; font-style: bold;  font-size: 12pt;");