python - QTabWidget 插入一个 QSplitter 不能在拆分器禁用时切换
问题描述
我在 QSplitter 中插入了 QFrame 和 QTabWidget。我想禁止在 QSplitter 中调整元素的大小。所以我在QSplitter中调用'setDisabled'的方法。它对于禁用调整元素大小很有用。但我也无法切换 QTabWidget 的选项卡。谁能给我一些建议?非常感谢......
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QSplitter, QHBoxLayout, QFrame, QTabWidget
from PyQt5.QtCore import Qt
class Example1(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle("Demo")
self.layout = QHBoxLayout()
top_frame = QFrame()
top_frame.setFrameShape(QFrame.StyledPanel)
bottom_frame = QTabWidget(self)
tab1 = QWidget()
tab2 = QWidget()
bottom_frame.setTabText(0, "Generic")
bottom_frame.setTabText(1, "Other")
bottom_frame.addTab(tab1, "Tab 1")
bottom_frame.addTab(tab2, "Tab 2")
splitter = QSplitter()
splitter.setOrientation(Qt.Vertical)
splitter.addWidget(top_frame)
splitter.addWidget(bottom_frame)
splitter.setSizes([300, 300])
**splitter.setDisabled(True)**
self.layout.addWidget(splitter)
self.setLayout(self.layout)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example1()
sys.exit(app.exec_())
程序的运行结果
解决方案
当你禁用一个小部件时,你也禁用了它的孩子,所以禁用 QSplitter 也会禁用 QTabWidget。
一个可能的解决方案是启用或禁用句柄:
import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import (
QApplication,
QFrame,
QHBoxLayout,
QSplitter,
QSplitterHandle,
QTabWidget,
QWidget,
)
class CustomSplitter(QSplitter):
@property
def enabled(self):
if not hasattr(self, "_enabled"):
self._enabled = True
return self._enabled
@enabled.setter
def enabled(self, d):
self._enabled = d
for i in range(self.count()):
self.handle(i).setEnabled(self.enabled)
def createHandle(self):
handle = super().createHandle()
handle.setEnabled(self.enabled)
return handle
class Example1(QWidget):
def __init__(self):
super().__init__()
self.setGeometry(0, 0, 600, 600)
self.setWindowTitle("Demo")
self.layout = QHBoxLayout()
top_frame = QFrame()
top_frame.setFrameShape(QFrame.StyledPanel)
bottom_frame = QTabWidget(self)
tab1 = QWidget()
tab2 = QWidget()
bottom_frame.setTabText(0, "Generic")
bottom_frame.setTabText(1, "Other")
bottom_frame.addTab(tab1, "Tab 1")
bottom_frame.addTab(tab2, "Tab 2")
splitter = CustomSplitter()
splitter.setOrientation(Qt.Vertical)
splitter.addWidget(top_frame)
splitter.addWidget(bottom_frame)
splitter.setSizes([300, 300])
splitter.enabled = False
self.layout.addWidget(splitter)
self.setLayout(self.layout)
self.show()
if __name__ == "__main__":
app = QApplication(sys.argv)
ex = Example1()
sys.exit(app.exec_())
推荐阅读
- laravel - 如何从laravel中的文件夹中读取所有文件夹名称?
- assembly - 用汇编语言输出两次 8086
- css - 在父 div 中定位 div
- ios - 如何检测 PKAddPassesViewController 中的添加按钮按下
- html - 使表头固定且 tbody 可滚动(x 和 y)
- javascript - 将 Leaflet Geojson 图层转换为 Leaflet 矩形矢量的最佳方法
- python - python函数返回语句是如何工作的?
- excel - 在外部自动界面中单击“组合框”鼠标时出错
- angular - 在 TypeScript 中获取类的父上下文
- flutter - 无法使用真实设备在 vs 代码中运行颤振应用程序