0

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:

Use ResizeToContents to show full data values but need to scroll for other columns

Use Stretch to show all columns but data cropped

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?

enter image description here

Appreciate your help and guidance as always. Thanks

chapter3
  • 914
  • 2
  • 12
  • 21
  • 1. calling `self.show()` in the `__init__` is usually considered bad practice; 2. the layout has to consider the contents, meaning that its *size hint* must be able to know the hints of its children; 3. calling `setSectionResizeMode()` more than once is pointless and wrong, since only the last call will be finally considered and any previous call only adds unnecessary overhead (especially if based on contents); 4. improve your "import skills", both for your code and for SO examples (aka: always provide a [mre] and remove parts that are not relevant or necessary for the code/example). – musicamante Nov 25 '22 at 05:59

0 Answers0