如何绘制图片而不是QSlider的滑块?
我创建了一个继承自 QSlider 的类,我想在滑块(抓取器)上绘制一张图片,而不是显示普通的图片。
怎么做呢?
I have created a class that inherits from QSlider and I want to draw a picture on the slider (grabber) instead of showing the plain one.
How to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以在小部件的 paintEvent 方法中执行此操作。这允许您重绘所有或仅部分小部件。
You can do it into the paintEvent method of the widget. This allows you to redraw all or only a part of the widget.
样式表和 PaintEvent 的组合可用于自定义句柄,而无需重新实现鼠标事件。
如果绘图区域超出手柄的几何形状,它将不会接收鼠标单击,因此我使用样式表使其大小与正在绘制的区域相同。
下面是一个加宽手柄并使其成为圆角矩形的示例,要对其进行调整,需要修改样式表和
paintEvent
:结果:
注意: 此解决方案未在水平滑块上进行测试。
了解更多:
A combination of stylesheets and
paintEvent
could be used to customize the handle, without reimplementing mouse events.If the drawing area exceeds the handle's geometry, it will not pick up mouse clicks, therefore I'm using stylesheets to make it the same size as the area being drawn.
Here's an example that widens the handle and makes it a rounded rect, to adjusted it, it's needed to modify both stylesheets and
paintEvent
:Result:
Note: This solution is not tested on horizontal sliders.
For more:
越南提供的答案:
Answer provided by Viet: