mepi0011
mepi0011

Reputation: 55

resize QTableWidget / QHBoxLayout at runtime

With Python and PyQt5 I programmed follow GUI.
Now I want customize the program so that I'm able to change the left table width by click on the right table border (light blue line) and moving the mouse (see arrows in picture). while changing the table width, the right side should also change. I hope my description is clear.

GUI output:

Part of the code for GUI layout:

# 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)

What kind of function or code changes do I need to achieve my goal?
--> QSplitter is a good hint

When I try to implement a QSplitter and change my program as below I get an error.

# 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)

When I ran the program I get following error:

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

How can I solve this error and how to change to code to work.

Upvotes: 1

Views: 2210

Answers (1)

eyllanesc
eyllanesc

Reputation: 244282

The solution is to use QSplitter, these are similar to QLayout, the goal is to control the size of the widgets.

In the following example example shows its use.

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

enter image description here

Upvotes: 1

Related Questions