使用下面的代码,我在滚动区域内获得了一个带标签的小部件。
这些标签在水平方向上较长,迫使用户使用滚动条移至右侧,然后单击最右侧的标签选择器,以便从标签1转到例如标签5-我想避免使用鼠标滚轮。
但是,我得到以下行为:
...即:
因此,简而言之-当鼠标悬停在选项卡区域上时,我希望鼠标滚轮上/下事件在选项卡之间仅切换,而不进行任何滚动-并且鼠标一旦在选项卡区域之外并且在“页面”区域内,即当我要在鼠标滚轮上滚动时。我该如何实现?
代码test-tabs.py
:
from PyQt5 import QtCore, QtGui, QtWidgets
import sys, os
class DataItem(object):
def __init__(self):
self.label = ""
self.type = "" # SECTION or LINE
self.children = []
class SectionWidget(QtWidgets.QWidget):
def __init__(self, indataitem, parent=None):
QtWidgets.QWidget.__init__(self, parent)
self.setLayout(QtWidgets.QFormLayout())
child_count = len(indataitem.children)
if child_count < 1:
return
tabs = None
for child in indataitem.children:
label = child.label
child_type = child.type
if child_type == "SECTION":
if not tabs:
tabs = QtWidgets.QTabWidget()
self.layout().insertRow(0, tabs)
tabs.addTab(SectionWidget(child), label)
elif child_type == "LINE":
self.layout().addRow(label.upper(), QtWidgets.QPushButton(label, self))
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setMinimumSize(600, 400)
self.frame = QtWidgets.QFrame(self)
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.framelayout = QtWidgets.QHBoxLayout(self.frame)
self.framelayout.setSpacing(0);
self.framelayout.setContentsMargins(0,0,0,0);
self.scroll = QtWidgets.QScrollArea(self)
self.scroll.setWidgetResizable(False)
self.scroll.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scroll.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.framelayout.addWidget(self.scroll)
self.widget = QtWidgets.QWidget()
self.widget.setLayout(QtWidgets.QGridLayout())
self.widget.setStyleSheet("""
.QWidget { border: 2px solid blue; border-radius: 2px; background-color: rgb(30, 255, 30); }
""")
self.uidata = DataItem()
LINESPERROUND=10
for ix in range(5):
theading = DataItem()
theading.label = "Very-very-very-very-very-long-heading-{:02d}".format(ix+1)
theading.type = "SECTION"
for iy in range(LINESPERROUND*(ix+1)):
tline = DataItem()
tline.label = (ix+1)*"Entry-{:02d}-{:02d}".format(ix+1, iy+1)
tline.type = "LINE"
theading.children.append(tline)
self.uidata.children.append(theading)
self.uisection = SectionWidget(self.uidata)
self.widget.layout().addWidget(self.uisection, 0, 0, 1, 3)
self.widget.layout().setColumnStretch(0, 1)
self.scroll.setWidget(self.widget)
self.setCentralWidget(self.frame)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())