Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to set the row height of QTableWidget



  • Hi,

    I am trying to set the row height of my QTableWidget but no matter what I try they are not getting smaller. Are there some hard coded values that qt is using not to respect the assigned height.

    Here is the PySide code:

    class NodeTableWidget(QtWidgets.QTableWidget):
        allFiles = []
        
        def __init__(self, parent=None):
            super(NodeTableWidget, self).__init__(parent)
            self.installEventFilter(self)
            
            #delegate = AlignDelegate(self)
            #self.setItemDelegate(delegate)
            
            self.setItemDelegate(QtWidgets.QItemDelegate())
            self.setColumnCount(2)
            #self.setMinimumHeight(2)
            #self.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustToContents)
            self.setSelectionBehavior(QtWidgets.QAbstractItemView.SelectRows);
            self.setSelectionMode(QtWidgets.QAbstractItemView.SingleSelection)
            self.setHorizontalHeaderLabels(("Embedded Nodes","Context"))
            self.setAlternatingRowColors(True)
            self.setShowGrid(False)
            self.setRowHeight(0, 10);
            
            hh = self.horizontalHeader()
            hh.setSectionResizeMode(QtWidgets.QHeaderView.ResizeToContents)
            #hh.setFixedHeight(5)
            #hh.setSectionResizeMode(0, QtGui.QHeaderView.Stretch)
            #self.setColumnWidth(1, 0)
            #hh.hide()
            
            vh = self.verticalHeader()
            vh.setSectionResizeMode(QtWidgets.QHeaderView.Fixed)
            
            print self.rowHeight(0)
            #vh.resizeSection(0, 5)
            #vh.resizeSection(1, 5)
            #vh.setDefaultSectionSize(5)
            
            nodes = hou.node("/").allNodes()
            nodes = [n for n in nodes if not n.isNetwork() and n.name() != "localscheduler"]
            
            nodenames = ["{0} ({1})".format(n.name(), n.type().name()) for n in nodes]
            nodetypes = [n.type().name() for n in nodes]
            nodecontexts = [n.type().category().name().upper() for n in nodes]
            
            nodeicons = []
            boxIcon = findNodeByType("Sop", "box").icon()
            
            iconsize = hou.ui.scaledSize(16)
            icons = [n.type().icon() for n in nodes]
            for icon in icons:
                try:
                    qtIcon = hou.qt.Icon(icon, width=iconsize, height=iconsize)
                except:
                    qtIcon = hou.qt.Icon(boxIcon, width=iconsize, height=iconsize)
                    
                nodeicons.append(qtIcon)
            
            
            
            #filedates = [time.ctime(os.path.getmtime(file)) for file in files]
            
            
                    
            #self.allFiles = files
            for index, node in enumerate(nodenames):
                self.insertRow(index)
                self.setRowHeight(index, 10)
                
                nodeicon = QtWidgets.QTableWidgetItem()
                nodeicon.setFlags(nodeicon.flags() & ~QtCore.Qt.ItemIsEditable)
                #nodeicon.setTextAlignment(QtCore.Qt.AlignCenter)
                nodeicon.setIcon(nodeicons[index])
                #self.setItem(0, 0, nodeicon)
                #self.setCentralWidget(self)
            
            
    
                        
                
                nodename = QtWidgets.QTableWidgetItem(node)
                nodename.setFlags(nodename.flags() & ~QtCore.Qt.ItemIsEditable)
                #nodename.setTextAlignment(QtCore.Qt.AlignCenter)
                nodename.setIcon(nodeicons[index])
                
                #nodetype = QtWidgets.QTableWidgetItem(nodetypes[index])
                #nodetype.setFlags(nodetype.flags() & ~QtCore.Qt.ItemIsEditable)
                #nodetype.setTextAlignment(QtCore.Qt.AlignCenter)
                
                nodecontext = QtWidgets.QTableWidgetItem(nodecontexts[index])
                nodecontext.setFlags(nodecontext.flags() & ~QtCore.Qt.ItemIsEditable)
                nodecontext.setTextAlignment(QtCore.Qt.AlignCenter)
                
                #self.setItem(index, 0, nodeicon)
                self.setItem(index, 0, nodename)
                #self.setItem(index, 1, nodetype)
                self.setItem(index, 1, nodecontext)
    

    I want half the height of these:

    https://i.imgur.com/iawx3h4.png

    Thanks in advance.



  • setMinimumSectionSize worked but it seems to clip the vertical row headers a bit? Is there a way to fix this?

    https://i.imgur.com/CAHUC5A.png



  • @lachdanan
    Make the minimum size a bit bigger!?



  • You mean for the QTableWidgetItem? Because I don't want to change its size, it's good now. But the vertical header size is clipped.



  • @lachdanan
    All I know is looking at your screenshot the height seems to be enough for the content items but not quite enough vertically for the row headers/labels. Maybe it's a font thing, I don't know.



  • @JonB Yes I reduced the size of the font just for the row headers. But to me this seems strange, like I have to tweak every little thing myself. I would have expected setting the row height would ensure everything looks correctly.


Log in to reply