具有自动缩放功能的 QLabel
QLabel with automatic scaling
我需要一个Qt小部件,它显示自动缩放的纯文本。这意味着当我调整布局中包含此小部件的窗口大小时,字体大小会调整为小部件的大小,以尽可能大的字体显示文本,以适应布局指示的大小。自动换行可能是一个奖励。
我想,有人已经实现了这样的小部件,但我无法谷歌它。
您可以在窗口的调整大小事件上执行此操作:
void MainWindow::resizeEvent(QResizeEvent*)
{
QFont f = label->font(); //Get label font
QFontMetrics metrics(f);
QSize size = metrics.size(0, label->text()); //Get size of text
float factorw = label->width() / (float)size.width(); //Get the width factor
float factorh = label->height() / (float)size.height(); //Get the height factor
float factor = qMin(factorw, factorh); //To fit contents in the screen select as factor
//the minimum factor between width and height
f.setPointSizeF(f.pointSizeF() * factor); //Set font size
label->setFont(f); //Set the adjusted font to the label
}
相关文章:
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 多态性和功能结合
- 带内存和隔离功能的SQLite
- 在CMakeLists.txt的安装功能中使用.cmake文件有什么用
- 类模板的成员功能的定义在单独的TU中完全专业化
- 有没有一种方法可以创建一个带有哈希表的数据库,该哈希表具有恒定时间查找功能
- 如何在C++中获得"静态纯虚拟"功能?
- 两个文件使用彼此的功能-如何解决
- 我应该实现右值推送功能吗?我应该使用std::move吗
- QML按钮点击功能执行顺序
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 有没有可能有一个只有ADL才能找到的非好友功能
- 功能样式转换从 'int' 到 'ItemType' 的匹配转换
- 文件系统:复制功能的速度秘诀是什么
- 在用于格式4的arm模拟器中实现功能时的一个问题
- 如何在Directwrite中获得给定字体的可用OpenType功能
- 为什么MainWindow的成员功能可以在QT中调用Qlabel的成员
- 具有自动缩放功能的 QLabel
- QT C++ - 信号和插槽:"No such slot QLabel..."甚至插槽功能也存在于我的类中
- 旋转QLabel并保留其功能/样式表