Jason Liu
Jason Liu

Reputation: 23

QTabWidget insert a QSplitter can't switch when the splitter disabled

I insert a QFrame and QTabWidget in the QSplitter. And I wanna forbidden to adjust the size of elements in QSplitter. So I call method of 'setDisabled' in QSplitter. It's useful for disabling resizing the elements. But I also can't switch tab of QTabWidget. Who can give me some suggestions? Thanks very much......

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

Upvotes: 2

Views: 455

Answers (3)

eyllanesc
eyllanesc

Reputation: 243955

When you disable a widget you also disable its children, so disabling the QSplitter also disables the QTabWidget.

A possible solution is enable or disable the handles:

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

Upvotes: 1

ypnos
ypnos

Reputation: 52337

QSplitter lets you get hold of its handles, which are the GUI element visible to the user. If you have two widgets in a splitter, you have a single visible handle; the handle at index 0 is always invisible.

You can manipulate that widget explicitely, e.g. disable it. Try:

splitter.handle(1).enabled = False

This disables only said GUI element, while the rest of the splitter (your two content widgets) will stay enabled.

Upvotes: 0

Welshhobo
Welshhobo

Reputation: 125

I haven't used a QSplitter before but are the .setFixedHeight(300) .setFixedWidth(300), or .setFixedSize(300, 300) methods not applicable here?

Upvotes: 0

Related Questions