<div dir="ltr"><div>Hi,</div><div><br></div><div><a href="https://doc.qt.io/qtforpython-5.12/overviews/sql-presenting.html">Qt's documentation says</a> one can use setHeaderData to set column headers.</div><div><br></div><div>However, I used it in the simple example below and it has no effect. Why not?</div><div><br></div><div>Note: I've used headerData to return column names directly and it works. However, I am curious why setHeaderData doesn't work in this example.</div><div><br></div><div>Thanks,</div><div><br></div><div>Rodrigo<br></div><div>PS: please let me know if this list should be used only for questions more specific about PyQt as opposed to Qt.<br></div><div><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,0,128);font-weight:bold">import </span>sys<br><br><span style="color:rgb(0,0,128);font-weight:bold">from </span>PyQt5 <span style="color:rgb(0,0,128);font-weight:bold">import </span>QtCore<br><span style="color:rgb(0,0,128);font-weight:bold">from </span>PyQt5.QtCore <span style="color:rgb(0,0,128);font-weight:bold">import </span>Qt<br><span style="color:rgb(0,0,128);font-weight:bold">from </span>PyQt5.QtWidgets <span style="color:rgb(0,0,128);font-weight:bold">import </span>QVBoxLayout, QWidget, QApplication, QMainWindow, QTableView<br><br><br><span style="color:rgb(0,0,128);font-weight:bold">class </span>TableModel(QtCore.QAbstractTableModel):<br>    <span style="color:rgb(0,0,128);font-weight:bold">def </span><span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(148,85,141)">self</span>):<br>        <span style="color:rgb(0,0,128)">super</span>().<span style="color:rgb(178,0,178)">__init__</span>()<br>        <span style="color:rgb(0,0,128);font-weight:bold">for </span>i <span style="color:rgb(0,0,128);font-weight:bold">in </span><span style="color:rgb(0,0,128)">range</span>(<span style="color:rgb(0,0,255)">4</span>):<br>            <span style="color:rgb(148,85,141)">self</span>.setHeaderData(i, Qt.Horizontal, <span style="color:rgb(0,128,128);font-weight:bold">"Column " </span>+ <span style="color:rgb(0,0,128)">str</span>(i))<br><br>    <span style="color:rgb(0,0,128);font-weight:bold">def </span>data(<span style="color:rgb(148,85,141)">self</span>, index, role=<span style="color:rgb(0,0,128);font-weight:bold">None</span>):<br>        <span style="color:rgb(0,0,128);font-weight:bold">if </span>role == Qt.DisplayRole:<br>            <span style="color:rgb(0,0,128);font-weight:bold">return </span><span style="color:rgb(0,0,255)">42<br></span><span style="color:rgb(0,0,255)"><br></span><span style="color:rgb(0,0,255)">    </span><span style="color:rgb(0,0,128);font-weight:bold">def </span>rowCount(<span style="color:rgb(148,85,141)">self</span>, index):<br>        <span style="color:rgb(0,0,128);font-weight:bold">return </span><span style="color:rgb(0,0,255)">3<br></span><span style="color:rgb(0,0,255)"><br></span><span style="color:rgb(0,0,255)">    </span><span style="color:rgb(0,0,128);font-weight:bold">def </span>columnCount(<span style="color:rgb(148,85,141)">self</span>, index):<br>        <span style="color:rgb(0,0,128);font-weight:bold">return </span><span style="color:rgb(0,0,255)">4<br></span><span style="color:rgb(0,0,255)"><br></span><span style="color:rgb(0,0,255)"><br></span><span style="color:rgb(0,0,128);font-weight:bold">class </span>MainWindow(QMainWindow):<br>    <span style="color:rgb(0,0,128);font-weight:bold">def </span><span style="color:rgb(178,0,178)">__init__</span>(<span style="color:rgb(148,85,141)">self</span>):<br>        <span style="color:rgb(0,0,128)">super</span>().<span style="color:rgb(178,0,178)">__init__</span>()<br><br>        l = QVBoxLayout()<br><br>        <span style="color:rgb(148,85,141)">self</span>.table = QTableView()<br><br>        <span style="color:rgb(148,85,141)">self</span>.model = TableModel()<br>        <span style="color:rgb(148,85,141)">self</span>.table.setModel(<span style="color:rgb(148,85,141)">self</span>.model)<br><br>        l.addWidget(<span style="color:rgb(148,85,141)">self</span>.table)<br><br>        w = QWidget()<br>        w.setLayout(l)<br>        <span style="color:rgb(148,85,141)">self</span>.setCentralWidget(w)<br><br><br>app = QApplication(sys.argv)<br>w = MainWindow()<br>w.show()<br>app.exec_()<br></pre></div><div><pre style="background-color:rgb(255,255,255);color:rgb(0,0,0);font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,0,128)"><br></span></pre></div></div>