QT中带有图像的QListView透明滚动条

发布于 2024-12-11 06:49:08 字数 107 浏览 0 评论 0原文

我有 QListWidget,我想要一个带图像的透明滚动条。 最初,滚动条应该在滚动时隐藏,只有它应该显示。 我如何在 Qt 中实现这一目标? 欢迎任何例子或想法。 我如何将图像应用到列表视图滚动条。

I am having QListWidget, i want to have a Transparent Scrollbar with Image.
Initially that scrollbar should be hidden on scroll only it should show.
How i Can achieve this in Qt ?
Any examples or ideas are welcomed.
How i can apply image to Listview Scrollbar.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断爱 2024-12-18 06:49:08

以便能够控制 Qt 中大多数类型的小部件的滚动条:

为了能够跟踪用户如何与 QListWidget 或任何 Widget 进行交互,您需要对其进行子类化并实现 QWheelEvent 以及可能的 QKeyEvent

滚动通常使用鼠标滚轮和键盘箭头键,有时使用向上翻页、向下翻页和空格键来完成。我没有对 QListWidget 做太多事情,但是您应该仔细检查哪些键盘事件/鼠标事件触发滚动。

即使您将小部件的一个或两个 ScrollBarPolicies 设置为 Qt::ScrollBarAlwaysOff,这些事件也会导致滚动事件。

首先,您应该放入小部件的构造函数

    this->setVerticalScrollBar(Qt::ScrollBarAlwaysOff);
    this->setHorizontalScrollBar(Qt::ScrollBarAlwaysOff);

,因此您只需要为小部件设置 setMouseTracking(true) (以便它跟踪的不仅仅是点击)并至少重新实现 wheelEvent(),当发生滚轮​​事件时,将垂直/水平滚动条策略设置为 true 并调用更新你的小部件。

如果您想在滚动条开始滚动几毫秒后将其关闭,则需要在构造函数中为子类小部件创建一个 QTimer,并将其连接到一个设置滚动条超时策略的插槽。然后,每次用户执行wheelEvent() 时,您都会启动/重新启动该计时器。

至于将图像应用到 ListView 滚动条,如果您想在其上实际放置图像或更改其外观,则应该考虑子类化 QAbstractScrollBar。如果您尝试将带有不同图标的按钮代替滚动条,那么设置一些工具按钮也可能是一种可行的方法。

Here are the three things you can look at to be able to control the scroll bar for most kinds of widgets in Qt:

To be able to track how the user interacts with your QListWidget or any Widget for that matter you need to subclass it and implement the virtual methods from the QWheelEvent and possibly the QKeyEvent.

Scrolling is typically done with the mouse wheel and with the keyboard arrow keys and sometimes page-up and page-down and spacebar. I haven't done a lot with QListWidget, but you should double check which keyboard events/mouse events trigger scrolling.

These events will cause scrolling event even after you set either or both of the ScrollBarPolicies for the widget to be Qt::ScrollBarAlwaysOff.

First you should put in the constructor of your widget

    this->setVerticalScrollBar(Qt::ScrollBarAlwaysOff);
    this->setHorizontalScrollBar(Qt::ScrollBarAlwaysOff);

So you just need to setMouseTracking(true) for the widget (so that it tracks more than just the clicks) and reimplement at the very least wheelEvent(), and when a wheel event occurs, set the vertical/horizontal scroll bar policies to true and call update on your widget.

If you want to turn the scrollbars back off after a few milliseconds after they have started scrolling, you will need to create a QTimer in your constructor for your subclassed widget and connect it to a slot that sets the scroll bar polices on the timeout. Then you start/restart that timer every time the user does a wheelEvent().

As far as applying an image to the ListView Scrollbar, you should look into subclassing QAbstractScrollBar, if you want to actually put an image on it or change the way it looks. Setting up some tool buttons may also be the way to go if you are trying to put buttons with different icons in place of the scrollbar.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文