在运行时调整 QTableWidget / QHBoxLayout 的大小

resize QTableWidget / QHBoxLayout at runtime

我使用 Python 和 PyQt5 按照 GUI 编程。
现在我想自定义程序,这样我就可以通过单击右 table 边框(浅蓝色线)并移动鼠标(参见图中的箭头)来更改左 table 宽度。在更改 table 宽度的同时,右侧也应更改。 我希望我的描述是清楚的。

GUI 输出:

GUI布局部分代码:

# table for writeOutput
    self.tableWidget = QTableWidget(0, 2)
    self.tableWidget.verticalHeader().setVisible(False);
    self.tableWidget.setMinimumHeight(170)
    self.tableWidget.setMinimumWidth(600)

    # generate chart
    self.main_widget = QWidget(self)
    self.chart = QtBarChart(self.main_widget)

    # Layout for input elements
    inputLayout = QHBoxLayout()
    inputLayout.addWidget(self.typeCBox)
    inputLayout.addWidget(self.yearSpinBox)
    inputLayout.addWidget(self.button)
    inputLayout.addStretch(1)

    # right inner layout for output elements e.g chart
    innerOutLayout = QVBoxLayout()
    innerOutLayout.addWidget(self.chart)
    innerOutLayout.addWidget(self.outEdit)

    # outer layout for output elements e.g. table
    outLayout = QHBoxLayout()
    outLayout.addWidget(self.tableWidget)
    outLayout.addLayout(innerOutLayout)

    # top layout who groups all elements together
    topLevelLayout = QVBoxLayout()
    topLevelLayout.addLayout(inputLayout)
    topLevelLayout.addLayout(outLayout)

我需要什么样的功能或代码更改才能实现我的目标?
--> QSplitter 是一个很好的提示

当我尝试实现 QSplitter 并如下更改我的程序时,出现错误。

# Layout for input elements
inputLayout = QHBoxLayout()
inputLayout.addWidget(self.typeCBox)
inputLayout.addWidget(self.yearSpinBox)
inputLayout.addWidget(self.button)
inputLayout.addStretch(1)

# right inner layout for output elements e.g chart
innerOutLayout = QVBoxLayout()
innerOutLayout.addWidget(self.chart)
innerOutLayout.addWidget(self.outEdit)

# create Splitter between Table and innerOutLayout() --> (Chart and outEdit)
tableSplitter = QSplitter(Qt.Horizontal)
tableSplitter.addWidget(self.tableWidget)
tableSplitter.addLayout(innerOutWidget)

# outer layout for output elements e.g. table
outLayout = QHBoxLayout()
outLayout.addWidget(self.tableWidget)
outLayout.addWidget(tableSplitter)

# top layout who groups all elements together
topLevelLayout = QVBoxLayout()
topLevelLayout.addLayout(inputLayout)
topLevelLayout.addLayout(outLayout)

当我 运行 程序时出现以下错误:

tableSplitter.addLayout(innerOutWidget)
AttributeError: 'QSplitter' object has no attribute 'addLayout'

如何解决此错误以及如何更改代码才能正常工作。

解决方案是使用QSplitter,这些类似于QLayout,目的是控制小部件的大小。

在下面的例子中展示了它的用法。

from PyQt5 import QtCore, QtGui, QtWidgets

class Widget(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(Widget, self).__init__(parent=parent)

        self.verticalLayout = QtWidgets.QVBoxLayout(self)

        self.splitter = QtWidgets.QSplitter(self)
        self.splitter.setOrientation(QtCore.Qt.Horizontal)

        self.createTable(self.splitter, 2, 2)
        self.splitter1 = QtWidgets.QSplitter(self.splitter)
        self.splitter1.setOrientation(QtCore.Qt.Vertical)

        self.createTable(self.splitter1, 3, 3)
        self.createTable(self.splitter1, 4, 4)

        self.verticalLayout.addWidget(self.splitter)



    def createTable(self, parent, nrows, ncols):
        tableWidget = QtWidgets.QTableWidget(parent)

        tableWidget.setRowCount(nrows)
        tableWidget.setColumnCount(ncols)

        for i in range(tableWidget.rowCount()):
            for j in range(tableWidget.columnCount()):
                item = QtWidgets.QTableWidgetItem(f"{i}{j}")
                tableWidget.setItem(i, j, item)

        return tableWidget


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = Widget()
    w.show()
    sys.exit(app.exec_())