【问题标题】:How to keep label font size constant irrespective of slider change in Qt?无论Qt中的滑块如何变化,如何保持标签字体大小不变?
【发布时间】:2017-07-14 09:29:42
【问题描述】:

我想通过使用 Qt 中的标签来显示滑块的值。每当更改滑块值时,都会调用一个槽函数。标签值在槽函数中更新。目前它有效,但并不完美。 问题是:我在 label 属性中将字体大小设置为 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);
}

【问题讨论】:

  • 您可以尝试使用Qt Style Sheets
  • 尝试ui->p_label->setText(QString::number(position)); 并删除您插槽中的所有其他内容。您是在 Designer 中还是以编程方式设置标签文本大小和字体?

标签: qt user-interface slider label slot


【解决方案1】:

试试 setStyleSheet..

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

【讨论】:

  • 谢谢,它有效!我在构造函数中插入了 setStyleSheet 方法。我通过参考 mainwindow.ui 中的设置更改了参数” 添加的代码行是: ui->p_label->setStyleSheet("color: black; font-size: 12pt; font-weight:600;") ;
猜你喜欢
  • 2021-12-03
  • 1970-01-01
  • 2022-07-11
  • 2011-01-28
  • 1970-01-01
  • 1970-01-01
  • 2020-05-17
  • 1970-01-01
  • 2011-10-18
相关资源
最近更新 更多