<div dir="ltr"><div class="" itemprop="text">

        <p>Hi,<br></p><p>I need to resize the header of a QTableWidget,
rows and columns is easy.</p>

<p>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.</p><p>Thank you in advance,</p><p><br></p>

<pre style class=""><code><span class="">    </span><span class="">#!/usr/bin/python</span><span class="">
    </span><span class=""># -*- coding: utf-8 -*-</span><span class="">

    </span><span class="">import</span><span class=""> sys

    </span><span class="">from</span><span class=""> </span><span class="">PyQt4</span><span class="">.</span><span class="">QtCore</span><span class=""> </span><span class="">import</span><span class=""> </span><span class="">*</span><span class="">
    </span><span class="">from</span><span class=""> </span><span class="">PyQt4</span><span class="">.</span><span class="">QtGui</span><span class=""> </span><span class="">import</span><span class=""> </span><span class="">*</span><span class="">


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


    t </span><span class="">=</span><span class=""> </span><span class="">QCheckBox</span><span class="">()</span><span class="">
    </span><span class=""># t = QLineEdit()</span><span class="">
    t</span><span class="">.</span><span class="">setMaximumWidth</span><span class="">(</span><span class="">30</span><span class="">)</span><span class="">
    d </span><span class="">=</span><span class=""> </span><span class="">QHBoxLayout</span><span class="">()</span><span class="">
    d</span><span class="">.</span><span class="">addStretch</span><span class="">()</span><span class="">
    d</span><span class="">.</span><span class="">addWidget</span><span class="">(</span><span class="">t</span><span class="">)</span><span class="">
    d</span><span class="">.</span><span class="">addStretch</span><span class="">()</span><span class="">

    masterLayout</span><span class="">.</span><span class="">addLayout</span><span class="">(</span><span class="">d</span><span class="">)</span><span class="">

    btn </span><span class="">=</span><span class=""> </span><span class="">QPushButton</span><span class="">(</span><span class="">'test'</span><span class="">)</span><span class="">
    masterLayout</span><span class="">.</span><span class="">addWidget</span><span class="">(</span><span class="">btn</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">connect</span><span class="">(</span><span class="">btn</span><span class="">,</span><span class=""> SIGNAL</span><span class="">(</span><span class="">"clicked()"</span><span class="">),</span><span class=""> self</span><span class="">.</span><span class="">run_click</span><span class="">)</span><span class="">

    self</span><span class="">.</span><span class="">grid </span><span class="">=</span><span class=""> </span><span class="">QTableWidget</span><span class="">()</span><span class="">
    masterLayout</span><span class="">.</span><span class="">addWidget</span><span class="">(</span><span class="">self</span><span class="">.</span><span class="">grid</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">fill_table</span><span class="">()</span><span class="">

</span><span class="">def</span><span class=""> run_click</span><span class="">(</span><span class="">self</span><span class="">):</span><span class="">
    </span><span class="">for</span><span class=""> linha </span><span class="">in</span><span class=""> range</span><span class="">(</span><span class="">0</span><span class="">,</span><span class=""> self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">rowCount</span><span class="">()):</span><span class="">
        </span><span class="">if</span><span class=""> self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">cellWidget</span><span class="">(</span><span class="">linha</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">)</span><span class=""> </span><span class="">is</span><span class=""> </span><span class="">not</span><span class=""> </span><span class="">None</span><span class="">:</span><span class="">
            self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">cellWidget</span><span class="">(</span><span class="">linha</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">).</span><span class="">children</span><span class="">()</span><span class="">
            </span><span class=""># for n in dir(self.grid.cellWidget(linha, 0)):</span><span class="">
            </span><span class="">#     print n</span><span class="">
            </span><span class="">print</span><span class=""> </span><span class="">'\n\n'</span><span class="">
            </span><span class="">if</span><span class=""> self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">cellWidget</span><span class="">(</span><span class="">linha</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">).</span><span class="">isChecked</span><span class="">()</span><span class=""> </span><span class="">:</span><span class="">
                </span><span class="">print</span><span class=""> int</span><span class="">(</span><span class="">self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">item</span><span class="">(</span><span class="">linha</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">).</span><span class="">text</span><span class="">())</span><span class="">

</span><span class="">def</span><span class=""> fill_table</span><span class="">(</span><span class="">self</span><span class="">):</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setSelectionBehavior</span><span class="">(</span><span class="">QTableWidget</span><span class="">.</span><span class="">SelectRows</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setSelectionMode</span><span class="">(</span><span class="">QTableWidget</span><span class="">.</span><span class="">SingleSelection</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setEditTriggers</span><span class="">(</span><span class="">QTableWidget</span><span class="">.</span><span class="">NoEditTriggers</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">verticalHeader</span><span class="">().</span><span class="">setDefaultSectionSize</span><span class="">(</span><span class="">20</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setAlternatingRowColors </span><span class="">(</span><span class="">True</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">verticalHeader</span><span class="">().</span><span class="">setVisible</span><span class="">(</span><span class="">False</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setColumnCount</span><span class="">(</span><span class="">3</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setHorizontalHeaderLabels</span><span class="">([</span><span class="">'Data0'</span><span class="">,</span><span class="">'Data1'</span><span class="">,</span><span class="">'Data2'</span><span class="">])</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setRowCount</span><span class="">(</span><span class="">5</span><span class="">)</span><span class="">

    line </span><span class="">=</span><span class=""> </span><span class="">0</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setRowHeight</span><span class="">(</span><span class="">0</span><span class="">,</span><span class="">20</span><span class="">)</span><span class="">
    </span><span class="">for</span><span class=""> n </span><span class="">in</span><span class=""> </span><span class="">[</span><span class="">'data1'</span><span class="">,</span><span class="">'data2'</span><span class="">,</span><span class="">'data3'</span><span class="">,</span><span class="">'data4'</span><span class="">,</span><span class="">'data5'</span><span class="">]:</span><span class="">

        self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setCellWidget</span><span class="">(</span><span class="">line</span><span class="">,</span><span class=""> </span><span class="">0</span><span class="">,</span><span class=""> self</span><span class="">.</span><span class="">checkBoxGrid</span><span class="">(</span><span class="">'mark'</span><span class="">))</span><span class="">
        self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setCellWidget</span><span class="">(</span><span class="">line</span><span class="">,</span><span class=""> </span><span class="">1</span><span class="">,</span><span class=""> </span><span class="">QCheckBox</span><span class="">())</span><span class="">

        item </span><span class="">=</span><span class=""> </span><span class="">QTableWidgetItem</span><span class="">()</span><span class="">
        self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">setItem</span><span class="">(</span><span class="">line</span><span class="">,</span><span class=""> </span><span class="">2</span><span class="">,</span><span class="">item </span><span class="">)</span><span class="">
        item</span><span class="">.</span><span class="">setText</span><span class="">(</span><span class="">n</span><span class="">)</span><span class="">

        line </span><span class="">+=</span><span class="">1</span><span class="">
    </span><span class=""># this should work</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">horizontalHeader</span><span class="">().</span><span class="">setResizeMode</span><span class="">(</span><span class="">QHeaderView</span><span class="">.</span><span class="">Fixed</span><span class="">)</span><span class="">
    self</span><span class="">.</span><span class="">grid</span><span class="">.</span><span class="">horizontalHeader</span><span class="">().</span><span class="">resizeSection</span><span class="">(</span><span class=""> </span><span class="">1</span><span class="">,</span><span class=""> </span><span class="">20</span><span class=""> </span><span class="">);</span><span class="">


</span><span class="">def</span><span class=""> checkBoxGrid</span><span class="">(</span><span class="">self</span><span class="">,</span><span class=""> label </span><span class="">=</span><span class=""> </span><span class="">''</span><span class="">):</span><span class="">
    w </span><span class="">=</span><span class=""> </span><span class="">QWidget</span><span class="">()</span><span class="">
    l </span><span class="">=</span><span class=""> </span><span class="">QHBoxLayout</span><span class="">(</span><span class="">w</span><span class="">)</span><span class="">
    l</span><span class="">.</span><span class="">setContentsMargins</span><span class="">(</span><span class="">0</span><span class="">,</span><span class="">0</span><span class="">,</span><span class="">0</span><span class="">,</span><span class="">0</span><span class="">)</span><span class="">
    l</span><span class="">.</span><span class="">addStretch</span><span class="">()</span><span class="">
    c </span><span class="">=</span><span class=""> </span><span class="">QCheckBox</span><span class="">(</span><span class="">label</span><span class="">)</span><span class="">
    l</span><span class="">.</span><span class="">addWidget</span><span class="">(</span><span class="">c</span><span class="">)</span><span class="">
    l</span><span class="">.</span><span class="">addStretch</span><span class="">()</span><span class="">
    </span><span class="">return</span><span class=""> w
</span><span class="">def</span><span class=""> main</span><span class="">():</span><span class="">
    app </span><span class="">=</span><span class=""> </span><span class="">QApplication</span><span class="">(</span><span class="">sys</span><span class="">.</span><span class="">argv</span><span class="">)</span><span class="">
    form </span><span class="">=</span><span class=""> </span><span class="">TagsInputDialog</span><span class="">()</span><span class="">
    form</span><span class="">.</span><span class="">show</span><span class="">()</span><span class="">
    app</span><span class="">.</span><span class="">exec_</span><span class="">()</span><span class="">

</span><span class="">if</span><span class=""> __name__ </span><span class="">==</span><span class=""> </span><span class="">'__main__'</span><span class="">:</span><span class="">
    main</span><span class="">()</span></code></pre>

    </div></div>