【发布时间】:2017-07-14 09:29:42
【问题描述】:
我想通过使用 Qt 中的标签来显示滑块的值。每当更改滑块值时,都会调用一个槽函数。标签值在槽函数中更新。目前它有效,但并不完美。 问题是:我在 label 属性中将字体大小设置为 12 并加粗。但每当滑块移动时,标签的字体大小变为 8,而不是粗体。我通过在槽函数中添加setPointSize 和setBold 函数解决了这个问题。但是有没有更优雅的替代方案?以下是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