Skip to content Skip to sidebar Skip to footer

How Do I Add A Layout To A Qtablewidget In Pyqt?

I have my qtablewidget defined like this: def __init__(self, parent = None): super(Window, self).__init__(parent) QtGui.QWidget.__init__(self) QtGui.QTableW

Solution 1:

add {your table}.table.horizontalHeader().setStretchLastSection(True) and/or {your table}.verticalHeader().setStretchLastSection(True)

import sys

from PyQt4 import QtGui
from PyQt4 import QtCore


classWindow(QtGui.QWidget):
    def__init__(self, parent=None):
        super(Window, self).__init__(parent=parent)
        QtGui.QTableWidget.setMinimumSize(self, 500, 500)
        QtGui.QTableWidget.setWindowTitle(self, "Custom table widget")
        self.table = QtGui.QTableWidget()
        rowf = 3
        self.table.setColumnCount(3)
        self.table.setRowCount(rowf)
        self.table.setHorizontalHeaderItem(0, QtGui.QTableWidgetItem("col1"))
        self.table.setHorizontalHeaderItem(1, QtGui.QTableWidgetItem("col2"))
        self.table.setHorizontalHeaderItem(2, QtGui.QTableWidgetItem("col3"))
        self.table.horizontalHeader().setStretchLastSection(True)
        # self.table.verticalHeader().setStretchLastSection(True)

        self.buttonBox = QtGui.QDialogButtonBox(self)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok)

        self.verticalLayout = QtGui.QVBoxLayout(self)
        self.verticalLayout.addWidget(self.table)
        self.verticalLayout.addWidget(self.buttonBox)

        self.buttonBox.accepted.connect(self.close)
        self.buttonBox.rejected.connect(self.close)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    w = Window()
    w.show()
    sys.exit(app.exec_())

Only Horizontal:

enter image description here

Only Vertical:

enter image description here

Vertical and Horizontal:

enter image description here

Post a Comment for "How Do I Add A Layout To A Qtablewidget In Pyqt?"