QScrollArea中使用QScroller,如何不传递给子控件
时间: 2024-03-16 17:44:47 浏览: 22
在QScrollArea中使用QScroller时,可以使用以下方法来防止将滚动事件传递给子控件:
```python
scrollArea = QScrollArea()
scrollArea.setWidget(widget)
scrollArea.setWidgetResizable(True)
scroller = QScroller.scroller(scrollArea.viewport())
scroller.grabGesture(scrollArea.viewport(), QScroller.LeftMouseButtonGesture)
scroller.setScrollerProperties(QScrollerProperties().setScrollMetric(QScrollerProperties.DragVelocitySmoothingFactor, 0.6))
```
在这里,我们使用QScroller的grabGesture方法来捕获滚动手势,并将其附加到QScrollArea的视口上。这样,QScroller将不会将滚动事件传递给子控件。同时,我们还可以使用setScrollerProperties方法来设置QScroller的属性,例如滑动速度平滑因子等。
相关问题
QScrollArea中使用QScroller,阻止viewport内的控件鼠标左键拖动也触发滚动,而是只在除控件外的空白地方触发滚动
要实现这个功能,你可以在QScrollArea的viewport上安装一个事件过滤器来过滤鼠标事件,只有在空白地方的鼠标左键拖动事件才会触发滚动。
具体的实现步骤如下:
1. 在QScrollArea的构造函数中创建QScroller,并将其绑定到viewport上,如下所示:
```python
self.scroll_area = QScrollArea()
self.scroller = QScroller.scroller(self.scroll_area.viewport())
self.scroller.setScrollerProperties(QScrollerProperties().setScrollMetric(QScrollerProperties.DragVelocitySmoothingFactor, 0.6))
self.scroller.grabGesture(self.scroll_area.viewport(), QScroller.LeftMouseButtonGesture)
```
这里创建了一个QScroller,并将其绑定到scroll_area的viewport上,然后设置了一些滚动属性和手势。
2. 在QScrollArea的构造函数中安装一个事件过滤器,并重写过滤器的eventFilter方法,如下所示:
```python
self.scroll_area.viewport().installEventFilter(self)
```
```python
def eventFilter(self, obj, event):
if obj == self.scroll_area.viewport() and event.type() == QEvent.MouseMove and event.buttons() == Qt.LeftButton:
# 获取当前鼠标位置,判断是否在控件内
pos = event.pos()
for child in obj.children():
if child.geometry().contains(pos):
return False
# 如果不在控件内,则拦截鼠标左键拖动事件,阻止控件触发滚动
event.ignore()
return True
return False
```
这里安装了一个事件过滤器,并重写了其eventFilter方法。在eventFilter方法中,如果鼠标左键拖动事件发生在viewport内,并且不在控件内,则拦截该事件,阻止控件触发滚动,否则不做处理,返回False。
通过这样的实现,就可以在QScrollArea中使用QScroller,阻止viewport内的控件鼠标左键拖动也触发滚动,而是只在除控件外的空白地方触发滚动了。
QScroller如何不传递给子控件
QScroller是一个用于处理滚动的Qt类,它可以用于滚动任何继承自QAbstractScrollArea的控件,例如QTextEdit、QListView等。默认情况下,QScroller会将滚动事件传递给其关联的控件及其子控件。如果想要QScroller不将滚动事件传递给子控件,可以使用QScroller::scroller()->grabGesture(QAbstractScrollArea::viewport(), QScroller::LeftMouseButtonGesture)方法,将滚动手势捕获到视口(viewport)上,而不是控件本身。这样,QScroller就不会将滚动事件传递给子控件了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)