我使用QLabel向用户显示更大,动态变化的QPixmap的内容。 根据可用的空间使这个标签更小/更大是很好的。 屏幕截图并不总是像QPixmap一样大。
如何修改QLabel的QSizePolicy和sizeHint()来调整QPixmap的大小,同时保持原始QPixmap的宽高比?
我不能修改QLabel的sizeHint() ,设置minimumSize()所以零没有帮助。 设置"hasScaledContents()"的QLabel允许增长,但打破纵横比的东西...
子类化QLabel确实有帮助,但这个解决方案增加了很多代码只是一个简单的问题...
任何聪明的提示如何实现这个没有子类化? 非常感谢!
I use a QLabel to display the content of a bigger, dynamically changing QPixmap to the user. It would be nice to make this label smaller/larger depending on the space available. The screen size is not always as big as the QPixmap.
How can I modify the QSizePolicy and sizeHint() of the QLabel to resize the QPixmap while keeping the aspect ratio of the original QPixmap?
I can't modify sizeHint() of the QLabel, setting the minimumSize() to zero does not help. Setting hasScaledContents() on the QLabel allows growing, but breaks the aspect ratio thingy...
Subclassing QLabel did help, but this solution adds too much code for just a simple problem...
Any smart hints how to accomplish this without subclassing?
最满意答案
为了更改标签尺寸,您可以为标签选择适当的大小策略,例如展开或最小展开。
您可以通过在每次更改时保持其纵横比来缩放像素图:
QPixmap p; // load pixmap // get label dimensions int w = label->width(); int h = label->height(); // set a scaled pixmap to a w x h window keeping its aspect ratio label->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio));有两个地方你应该添加这个代码:
当pixmap更新时 在包含标签的小部件的resizeEvent中In order to change the label size you can select an appropriate size policy for the label like expanding or minimum expanding.
You can scale the pixmap by keeping its aspect ratio every time it changes:
QPixmap p; // load pixmap // get label dimensions int w = label->width(); int h = label->height(); // set a scaled pixmap to a w x h window keeping its aspect ratio label->setPixmap(p.scaled(w,h,Qt::KeepAspectRatio));There are two places where you should add this code:
When the pixmap is updated In the resizeEvent of the widget that contains the label更多推荐
发布评论