I am working on a fairly simply PyQt program which basically is just a QTableWidget
full of items. My goal is for the width and height of the items to automatically resize based on the size of the parent QTableWidget
. For example, if I resize the window smaller, the items width and height should decrease in size but there should remain the same number of items and the items should still completely fill the parent QTableWidget
. As you see, I am currently using setColumnWidth
and setRowHeight
to manually set the width and height.
I have tried the suggestions in the following Stack Overflow questions, as well as many other questions and websites, none of which do what I am attemping to do.
1. How to make qtablewidgets columns assume the maximum space
2. pyqt how to maximize the column width in a tableview
Here is the code I am using currently:
from PyQt4 import QtGui, QtCore
import PyQt4.Qt
class Window(QtGui.QWidget):
def __init__(self, rows, columns):
super(Window, self).__init__()
self.setWindowState(QtCore.Qt.WindowMaximized)
self.table = QtGui.QTableWidget(rows, columns, self)
self.table.verticalHeader().setVisible(False)
self.table.horizontalHeader().setVisible(False)
# this is what I am currently using to set the row and column size
for x in range(columns):
self.table.setColumnWidth(x, 13)
for x in range(rows):
self.table.setRowHeight(x, 13)
for row in range(rows):
for column in range(columns):
item = QtGui.QTableWidgetItem()
self.table.setItem(row, column, item)
layout = QtGui.QGridLayout(self)
layout.addWidget(self.table, 0, 0, 1, 6)
self.show()
def main():
import sys
app = QtGui.QApplication(sys.argv)
window = Window(54, 96)
sys.exit(app.exec_())
if __name__ == "__main__":
main()
I appoligize for the length of this question, but I wanted to make my question very clear. Thank you all in advance for your help!