<html><head><meta http-equiv="Content-Type" content="text/html; charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><br class=""><div><br class=""><blockquote type="cite" class=""><div class="">On 6 Feb 2021, at 09:46, Rodrigo de Salvo Braz <<a href="mailto:rodrigobraz@gmail.com" class="">rodrigobraz@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">Hi,</div><div class=""><br class=""></div><div class=""><a href="https://doc.qt.io/qtforpython-5.12/overviews/sql-presenting.html" class="">Qt's documentation says</a> one can use setHeaderData to set column headers.</div><div class=""><br class=""></div><div class="">However, I used it in the simple example below and it has no effect. Why not?</div><div class=""><br class=""></div><div class="">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 class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">Rodrigo<br class=""></div><div class="">PS: please let me know if this list should be used only for questions more specific about PyQt as opposed to Qt.<br class=""></div><div class=""><pre style="background-color: rgb(255, 255, 255); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;" class=""><span style="color:rgb(0,0,128);font-weight:bold" class="">import </span>sys<br class=""><br class=""><span style="color:rgb(0,0,128);font-weight:bold" class="">from </span>PyQt5 <span style="color:rgb(0,0,128);font-weight:bold" class="">import </span>QtCore<br class=""><span style="color:rgb(0,0,128);font-weight:bold" class="">from </span>PyQt5.QtCore <span style="color:rgb(0,0,128);font-weight:bold" class="">import </span>Qt<br class=""><span style="color:rgb(0,0,128);font-weight:bold" class="">from </span>PyQt5.QtWidgets <span style="color:rgb(0,0,128);font-weight:bold" class="">import </span>QVBoxLayout, QWidget, QApplication, QMainWindow, QTableView<br class=""><br class=""><br class=""><span style="color:rgb(0,0,128);font-weight:bold" class="">class </span>TableModel(QtCore.QAbstractTableModel):<br class="">    <span style="color:rgb(0,0,128);font-weight:bold" class="">def </span><span style="color:rgb(178,0,178)" class="">__init__</span>(<span style="color:rgb(148,85,141)" class="">self</span>):<br class="">        <span style="color:rgb(0,0,128)" class="">super</span>().<span style="color:rgb(178,0,178)" class="">__init__</span>()<br class="">        <span style="color:rgb(0,0,128);font-weight:bold" class="">for </span>i <span style="color:rgb(0,0,128);font-weight:bold" class="">in </span><span style="color:rgb(0,0,128)" class="">range</span>(<span style="color:rgb(0,0,255)" class="">4</span>):<br class="">            <span style="color:rgb(148,85,141)" class="">self</span>.setHeaderData(i, Qt.Horizontal, <span style="color:rgb(0,128,128);font-weight:bold" class="">"Column " </span>+ <span style="color:rgb(0,0,128)" class="">str</span>(i))<br class=""><br class="">    <span style="color:rgb(0,0,128);font-weight:bold" class="">def </span>data(<span style="color:rgb(148,85,141)" class="">self</span>, index, role=<span style="color:rgb(0,0,128);font-weight:bold" class="">None</span>):<br class="">        <span style="color:rgb(0,0,128);font-weight:bold" class="">if </span>role == Qt.DisplayRole:<br class="">            <span style="color:rgb(0,0,128);font-weight:bold" class="">return </span><span style="color:rgb(0,0,255)" class="">42<br class=""></span><span style="color:rgb(0,0,255)" class=""><br class=""></span><span style="color:rgb(0,0,255)" class="">    </span><span style="color:rgb(0,0,128);font-weight:bold" class="">def </span>rowCount(<span style="color:rgb(148,85,141)" class="">self</span>, index):<br class="">        <span style="color:rgb(0,0,128);font-weight:bold" class="">return </span><span style="color:rgb(0,0,255)" class="">3<br class=""></span><span style="color:rgb(0,0,255)" class=""><br class=""></span><span style="color:rgb(0,0,255)" class="">    </span><span style="color:rgb(0,0,128);font-weight:bold" class="">def </span>columnCount(<span style="color:rgb(148,85,141)" class="">self</span>, index):<br class="">        <span style="color:rgb(0,0,128);font-weight:bold" class="">return </span><span style="color:rgb(0,0,255)" class="">4<br class=""></span><span style="color:rgb(0,0,255)" class=""><br class=""></span><span style="color:rgb(0,0,255)" class=""><br class=""></span><span style="color:rgb(0,0,128);font-weight:bold" class="">class </span>MainWindow(QMainWindow):<br class="">    <span style="color:rgb(0,0,128);font-weight:bold" class="">def </span><span style="color:rgb(178,0,178)" class="">__init__</span>(<span style="color:rgb(148,85,141)" class="">self</span>):<br class="">        <span style="color:rgb(0,0,128)" class="">super</span>().<span style="color:rgb(178,0,178)" class="">__init__</span>()<br class=""><br class="">        l = QVBoxLayout()<br class=""><br class="">        <span style="color:rgb(148,85,141)" class="">self</span>.table = QTableView()<br class=""><br class="">        <span style="color:rgb(148,85,141)" class="">self</span>.model = TableModel()<br class="">        <span style="color:rgb(148,85,141)" class="">self</span>.table.setModel(<span style="color:rgb(148,85,141)" class="">self</span>.model)<br class=""><br class="">        l.addWidget(<span style="color:rgb(148,85,141)" class="">self</span>.table)<br class=""><br class="">        w = QWidget()<br class="">        w.setLayout(l)<br class="">        <span style="color:rgb(148,85,141)" class="">self</span>.setCentralWidget(w)<br class=""><br class=""><br class="">app = QApplication(sys.argv)<br class="">w = MainWindow()<br class="">w.show()<br class="">app.exec_()<br class=""></pre></div><div class=""><pre style="background-color: rgb(255, 255, 255); font-family: "JetBrains Mono", monospace; font-size: 9.8pt;" class=""><span style="color:rgb(0,0,128)" class=""><br class=""></span></pre></div></div>
</div></blockquote>Hi,</div><div><br class=""></div><div>I believe that in class TableModel instead of calling the inherited setHeaderData() method you must provide your own implementation of it. Perhaps something like</div><div><br class=""></div><div><div><font face="Courier New" class=""><span style="font-style: normal;" class="">    def headerData(self, section, orientation, role):</span></font></div><div><font face="Courier New" class=""><span style="font-style: normal;" class="">        if orientation == Qt.Vertical:</span></font></div><div><font face="Courier New" class=""><span style="font-style: normal;" class="">            return None</span></font></div><div><font face="Courier New" class=""><span style="font-style: normal;" class="">        elif role == Qt.DisplayRole:</span></font></div><div><font face="Courier New" class=""><span style="font-style: normal;" class="">            return "Column " + str(section)</span></font></div><div><font face="Courier New" class=""><span style="font-style: normal;" class="">        else:</span></font></div><div><font face="Courier New" class=""><span style="font-style: normal;" class="">            return None</span></font></div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Colin</div></div><br class=""></body></html>