[PySide] How to Resize Columns in QTableView

Lipson, Ed ed.lipson at bnymellon.com
Wed Dec 18 15:51:27 CET 2019


I am building a simple data display application, to show several columns from a table. They appear fine, but I can't get the columns to fit the data correctly. All the columns appear the same width.
I did not see much by searching for this. I've tried several different options. None seem to make any difference. How should one do this? PyQt and PySide2 have the same results
Using QtDesigner
QtDesigner 5.13.0
Python 3.7.4
PyQt5              5.13.0
PyQt5-sip          4.19.18
PyQt5Designer      5.10.1
PySide2            5.13.1

I'm restricted in what I can post without obfuscating information, I hope the snippets will help.

Snippet from uic converted .ui

        self.tableViewArch = QtWidgets.QTableView(self.centralwidget)
        self.tableViewArch.setGeometry(QtCore.QRect(20, 70, 771, 471))
        self.tableViewArch.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
        self.tableViewArch.setEditTriggers(QtWidgets.QAbstractItemView.NoEditTriggers)
        self.tableViewArch.setTabKeyNavigation(False)
        self.tableViewArch.setProperty("showDropIndicator", False)
        self.tableViewArch.setDragDropOverwriteMode(False)
        self.tableViewArch.setSortingEnabled(True)
        self.tableViewArch.setObjectName("tableViewArch")

Class method loading the QTableView

    def loadTableViewArch(self):
        archlist,archcol = self.dbh.get_arch_for_mnm(self.comboBoxAppMnm.currentText())
        cols = [col[0] for col in archcol]
        curmodel = self.tableViewArch.model()
        model = QStandardItemModel()
        model.setHorizontalHeaderLabels(cols)
        self.tableViewArch.setModel(model)
        self.tableViewArch.resizeColumnsToContents()
        collen = [0] * len(cols)
        for row in archlist:
            items = []
            for cix, col in enumerate(row):
                if collen[cix] < len(col.strip()):
                    collen[cix] = len(col.strip())
                citem = QStandardItem(col) #.setText(col)
                Qs = QtCore.QSize()
                Qs.setWidth(len(col.strip()))
                Qs.setWidth(100)
                citem.setSizeHint(Qs)
                items.append(citem)
            model.appendRow(items)
        for cix,clen in enumerate(collen):
            self.tableViewArch.setColumnWidth(cix,100)
        del curmodel




Thanks,
Ed Lipson
Director
BNY Mellon | 2 Hanson Pl AIM 111-0800 | Brooklyn NY 11217
Information Lifecycle Management
T 718.315.4763 |  F 724.540.6622 | C 917.859.5180 | ed.lipson at bnymellon.com<mailto:ed.lipson at bnymellon.com>


The information contained in this e-mail, and any attachment, is confidential and is intended solely for the use of the intended recipient. Access, copying or re-use of the e-mail or any attachment, or any information contained therein, by any other person is not authorized. If you are not the intended recipient please return the e-mail to the sender and delete it from your computer. Although we attempt to sweep e-mail and attachments for viruses, we do not guarantee that either are virus-free and accept no liability for any damage sustained as a result of viruses. 

Please refer to https://disclaimer.bnymellon.com/eu.htm for certain disclosures relating to European legal entities. We take our data protection and privacy responsibilities seriously and our privacy notice explains how we collect, use and share personal information in the course of our business activities. It can be accessed at the privacy section of  www.bnymellon.com.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/pyside/attachments/20191218/73b6ed4b/attachment-0001.html>


More information about the PySide mailing list