提问人:redstart 提问时间:11/1/2023 更新时间:11/1/2023 访问量:44
被信号调用时无法抓取 LeftMouseButtonGesture
Cannot grab LeftMouseButtonGesture when called by a signal
问:
我有项目列表,单击该项目时需要查看该项目的详细信息。由于项目的数量及其详细信息非常庞大,我需要能够使用鼠标左键滚动它们(由于 UI 偏好,无法使用滚动条)。
我可以滚动两者,也可以在显式调用该方法时滚动。QTableWidget
QTextEdit
view_details
问题是;当我用左键将信号连接到滚动时不再起作用。cellClicked
view_details
我怀疑拦截手势事件并创建了一个事件过滤器,但发现根本没有生成该事件。我提供了一个最小的样本来重现该行为:QTableWidget
# PySide 5.15.2.1
from PySide2.QtWidgets import (
QApplication,
QTableWidget,
QTextEdit,
QVBoxLayout,
QWidget,
QTableWidgetItem,
QScroller,
QDialog,
)
from PySide2.QtCore import Qt
DUMMYTEXT = """
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque sagittis venenatis metus eget lobortis. Quisque purus neque,
faucibus ac molestie sit amet, mollis ut lectus. Cras interdum volutpat lacus dignissim rutrum. Curabitur non magna et justo
condimentum mattis. Sed nec congue mi. Duis lacinia enim sed enim consequat, facilisis sagittis nunc malesuada. Sed nec
commodo orci. In feugiat efficitur elit, vel finibus urna gravida in. Duis a nunc in dolor rhoncus egestas vel ac leo. Nullam
scelerisque facilisis lorem in sodales. Curabitur mattis, lacus commodo lobortis pulvinar, lorem neque malesuada massa, nec
elementum lectus metus lobortis ante. Nulla sed lorem lacus.
Nullam pharetra finibus sem. Nulla diam enim, accumsan tempus lacinia a, mattis sed sapien. Suspendisse dapibus neque eget
purus pellentesque, vel porttitor lectus eleifend. Integer sed ante lacus. Pellentesque pretium maximus enim ut suscipit.
Curabitur eu urna elit. Nunc eget feugiat odio, non posuere mi. Nam lacinia tellus sed massa efficitur vestibulum. Maecenas
pharetra mattis lectus, et consequat magna finibus sit amet. Mauris non urna hendrerit, mattis eros et, rutrum sapien.
Praesent tortor nunc, fringilla ac neque quis, feugiat tristique libero. Pellentesque ultrices tristique augue vitae tristique.
"""
class MyWidget(QWidget):
def __init__(self):
super().__init__()
self.setMinimumSize(200, 300)
self.table_widget = QTableWidget(self)
self.table_widget.setColumnCount(1)
self.table_widget.setRowCount(10)
self.table_widget.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
for i in range(10):
item = QTableWidgetItem()
item.setText("Item")
self.table_widget.setItem(i, 0, item)
layout = QVBoxLayout()
layout.addWidget(self.table_widget)
self.setLayout(layout)
self.setScroller(self.table_widget)
### Gesture is successfully grabbed by QDialog's QTextEdit when directly called
self.view_details()
### Gesture is not grabbed by QDialog's QTextEdit when called by the signal of QTableWidget which also grabs the gesture
self.table_widget.cellClicked.connect(self.view_details)
def setScroller(self, widget):
scroller = QScroller.scroller(widget)
scroller.grabGesture(widget, QScroller.LeftMouseButtonGesture)
def view_details(self):
dialog = QDialog(self)
text_edit = QTextEdit(dialog)
text_edit.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
text_edit.setTextInteractionFlags(Qt.NoTextInteraction)
text_edit.setText(DUMMYTEXT)
self.setScroller(text_edit)
layout = QVBoxLayout(dialog)
layout.addWidget(text_edit)
dialog.exec()
if __name__ == "__main__":
app = QApplication()
main_widget = MyWidget()
main_widget.show()
app.exec_()
在此示例中,使用鼠标左键滚动将在第一个显示的对话框中起作用,但在单击表格单元格时将不起作用。在这两种情况下,有什么方法可以实现此功能吗?
答: 暂无答案
评论
itemPressed