[PyQt] Resize the height of the header in QTableWidget

Starglider Developer starglider.dev at gmail.com
Thu Oct 23 23:10:33 BST 2014


Hi,

I need to resize the header of a QTableWidget, rows and columns is easy.

Here is the sample code I use for testing, the
self.grid.horizontalHeader().setResizeMode(QHeaderView.Fixed) and
self.grid.horizontalHeader().resizeSection( 1, 20 ) should work but it
don't.

Thank you in advance,


    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    import sys

    from PyQt4.QtCore import *
    from PyQt4.QtGui import *

class TagsInputDialog(QDialog):def __init__(self,  parent = None):
    super(TagsInputDialog,  self).__init__(parent)
    self.setWindowTitle('test')
    masterLayout = QVBoxLayout(self)
    self.tags_list = []


    t = QCheckBox()
    # t = QLineEdit()
    t.setMaximumWidth(30)
    d = QHBoxLayout()
    d.addStretch()
    d.addWidget(t)
    d.addStretch()

    masterLayout.addLayout(d)

    btn = QPushButton('test')
    masterLayout.addWidget(btn)
    self.connect(btn, SIGNAL("clicked()"), self.run_click)

    self.grid = QTableWidget()
    masterLayout.addWidget(self.grid)
    self.fill_table()
def run_click(self):
    for linha in range(0, self.grid.rowCount()):
        if self.grid.cellWidget(linha, 0) is not None:
            self.grid.cellWidget(linha, 0).children()
            # for n in dir(self.grid.cellWidget(linha, 0)):
            #     print n
            print '\n\n'
            if self.grid.cellWidget(linha, 0).isChecked() :
                print int(self.grid.item(linha, 0).text())
def fill_table(self):
    self.grid.setSelectionBehavior(QTableWidget.SelectRows)
    self.grid.setSelectionMode(QTableWidget.SingleSelection)
    self.grid.setEditTriggers(QTableWidget.NoEditTriggers)
    self.grid.verticalHeader().setDefaultSectionSize(20)
    self.grid.setAlternatingRowColors (True)
    self.grid.verticalHeader().setVisible(False)
    self.grid.setColumnCount(3)
    self.grid.setHorizontalHeaderLabels(['Data0','Data1','Data2'])
    self.grid.setRowCount(5)

    line = 0
    self.grid.setRowHeight(0,20)
    for n in ['data1','data2','data3','data4','data5']:

        self.grid.setCellWidget(line, 0, self.checkBoxGrid('mark'))
        self.grid.setCellWidget(line, 1, QCheckBox())

        item = QTableWidgetItem()
        self.grid.setItem(line, 2,item )
        item.setText(n)

        line +=1
    # this should work
    self.grid.horizontalHeader().setResizeMode(QHeaderView.Fixed)
    self.grid.horizontalHeader().resizeSection( 1, 20 );

def checkBoxGrid(self, label = ''):
    w = QWidget()
    l = QHBoxLayout(w)
    l.setContentsMargins(0,0,0,0)
    l.addStretch()
    c = QCheckBox(label)
    l.addWidget(c)
    l.addStretch()
    return wdef main():
    app = QApplication(sys.argv)
    form = TagsInputDialog()
    form.show()
    app.exec_()
if __name__ == '__main__':
    main()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20141023/5f686ee8/attachment-0001.html>


More information about the PyQt mailing list