[QTabWidget插入禁用拆分器后无法切换QSplitter

问题描述 投票:0回答:3

我在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_())

the running result of the program

python pyqt5 qtabwidget qsplitter
3个回答
0
投票

我以前没有使用过QSplitter,但是.setFixedHeight(300) .setFixedWidth(300).setFixedSize(300, 300)方法在这里不适用吗?


0
投票

QSplitter让您掌握其句柄,这是用户可见的GUI元素。如果拆分器中有两个小部件,则只有一个可见的句柄。索引0的句柄始终不可见。

您可以明确地操作该小部件,例如禁用它。试试:

splitter.handle(1).enabled = False

这将仅禁用所述GUI元素,而拆分器的其余部分(您的两个内容小部件)将保持启用状态。


0
投票

[禁用小部件时,也会禁用其子级,因此禁用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_())
© www.soinside.com 2019 - 2024. All rights reserved.