I try to create a simple (literally) PyQt5 QTableWidget. But I am not able to show all the data all at once. Instead, I got either cropped with data values or need to scroll across to be able to see all columns:
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QDesktopWidget, QPushButton, QHBoxLayout, QVBoxLayout, QApplication
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem, QHeaderView
class Example(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
tableWidget = self.createTable()
box = QHBoxLayout()
box.addWidget(tableWidget)
self.setLayout(box)
self.setWindowTitle('Progress')
self.show()
def createTable(self):
# Create table
tableWidget = QTableWidget()
tableWidget.setColumnCount(5)
tableWidget.setRowCount(4)
tableWidget.setHorizontalHeaderLabels(['start','end','count','elapsed','average'])
tableWidget.setItem(0,0, QTableWidgetItem("2022-11-23-12:34:56"))
tableWidget.setItem(0,1, QTableWidgetItem("2022-11-23-12:34:56"))
tableWidget.setItem(0,2, QTableWidgetItem("2022-11-23-12:34:56"))
tableWidget.setItem(0,3, QTableWidgetItem("2022-11-23-12:34:56"))
tableWidget.setItem(1,0, QTableWidgetItem("2022-11-23-12:34:56"))
tableWidget.setItem(1,1, QTableWidgetItem("2022-11-23-12:34:56"))
tableWidget.setItem(2,0, QTableWidgetItem("Cell (3,1)"))
tableWidget.setItem(2,1, QTableWidgetItem("Cell (3,2)"))
tableWidget.setItem(3,0, QTableWidgetItem("Cell (4,1)"))
tableWidget.setItem(3,1, QTableWidgetItem("Cell (4,2)"))
tableWidget.move(0,0)
tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.ResizeToContents)
tableWidget.horizontalHeader().setSectionResizeMode(QHeaderView.Stretch)
return tableWidget
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
I went through some discussions on this topic but really couldn't wrap my head around it. I am all fine to use ResizeToContents but how come the layout is not showing the entire table like below but give a scrolling area?
Appreciate your help and guidance as always. Thanks