[prev in list] [next in list] [prev in thread] [next in thread] 

List:       pykde
Subject:    Re: Setting header data in QTableView
From:       Rodrigo de Salvo Braz <rodrigobraz () gmail ! com>
Date:       2021-02-08 3:15:33
Message-ID: CABypmH4=9FW7N5w5szjs7xOEO3UwHuAfk3PsFGmZ5oOKnXL5DA () mail ! gmail ! com
[Download RAW message or body]

Awesome, thanks, that's exactly what I needed to know.

Rodrigo

On Sun, Feb 7, 2021 at 8:31 PM Maurizio Berti <maurizio.berti@gmail.com>
wrote:

> Il giorno sab 6 feb 2021 alle ore 10:46 Rodrigo de Salvo Braz <
> rodrigobraz@gmail.com> ha scritto:
>
>> Hi,
>>
>> Qt's documentation says
>> <https://doc.qt.io/qtforpython-5.12/overviews/sql-presenting.html> one
>> can use setHeaderData to set column headers.
>>
>> However, I used it in the simple example below and it has no effect. Why
>> not?
>>
>
> If you simply try to print the result of your `setHeaderData()` calls,
> you'll see that the returned value is always False.
>
> By default abstract item models do not implement anything on their basic
> methods, especially when trying to set data and properties on the model
> itself. Those methods are implemented in models that are *not* abstract and
> allow data writing (such as QStandardModelItem or, in your case,
> QSqlQueryModel and all its inherited classes which are reported in your
> link). The same would happen, in fact, for setData(), which makes sense:
> being an abstract model, it's up to you to actually set the data and return
> whether if the setting actually occurred or not.
>
> Methods like setHeaderData() will always return False (meaning that the
> data has not been set) unless they're actually implemented in order to
> return True.
>
> class TableModel(QtCore.QAbstractTableModel):
>     def __init__(self):
>         super().__init__()
>         self.headers = []
>         for i in range(4):
>             self.setHeaderData(i, Qt.Horizontal, "Column " + str(i))
>
>     def setHeaderData(self, section, orientation, value,
> role=QtCore.Qt.EditRole):
>         if (orientation == QtCore.Qt.Horizontal and
>             0 <= section < self.columnCount() and role ==
> QtCore.Qt.EditRole):
>                 while len(self.headers) < section + 1:
>                     self.headers.append(None)
>                 self.headers[section] = value
>                 self.headerDataChanged.emit(orientation, section, section)
>                 return True
>         return False
>
> --
> È difficile avere una convinzione precisa quando si parla delle ragioni
> del cuore. - "Sostiene Pereira", Antonio Tabucchi
> http://www.jidesk.net
>

[Attachment #3 (text/html)]

<div dir="ltr"><div>Awesome, thanks, that&#39;s exactly what I needed to \
know.</div><div><br></div><div>Rodrigo<br></div></div><br><div \
class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Feb 7, 2021 at 8:31 PM \
Maurizio Berti &lt;<a \
href="mailto:maurizio.berti@gmail.com">maurizio.berti@gmail.com</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div \
dir="ltr"><div dir="ltr">Il giorno sab 6 feb 2021 alle ore 10:46 Rodrigo de Salvo \
Braz &lt;<a href="mailto:rodrigobraz@gmail.com" \
target="_blank">rodrigobraz@gmail.com</a>&gt; ha scritto:<br></div><div \
class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px \
0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div \
dir="ltr"><div>Hi,</div><div><br></div><div><a \
href="https://doc.qt.io/qtforpython-5.12/overviews/sql-presenting.html" \
target="_blank">Qt&#39;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></div></div></blockquote></div><div><br></div><div>If you simply try \
to print the result of your `setHeaderData()` calls, you&#39;ll see that the returned \
value is always False.</div><div><br></div><div>By default  abstract item models do \
not implement anything on their basic methods, especially when trying to set data and \
properties on the model itself. Those methods are implemented in models that are \
*not* abstract and allow data writing (such as QStandardModelItem or, in your case, \
QSqlQueryModel and all its inherited classes which are reported in your link). The \
same would happen, in fact, for setData(), which makes sense: being an abstract \
model, it&#39;s up to you to actually set the data and return whether if the setting \
actually  occurred or not.</div><div><br></div><div>Methods like setHeaderData() will \
always return False (meaning that the data has not been set) unless they&#39;re \
actually implemented in order to return True.</div><div><br></div><div><div><font \
face="monospace">class TableModel(QtCore.QAbstractTableModel):</font></div><div><font \
face="monospace">      def __init__(self):</font></div><div><font face="monospace">   \
super().__init__()</font></div><div><font face="monospace">            self.headers  \
= []</font></div><div><font face="monospace">            for i in \
range(4):</font></div><div><font face="monospace">                  \
self.setHeaderData(i, Qt.Horizontal, &quot;Column &quot; + \
str(i))</font></div><div><font \
face="monospace"><br></font></div><div></div></div><div><font face="monospace">      \
def setHeaderData(self, section, orientation, value, \
role=QtCore.Qt.EditRole):</font></div><div><font face="monospace">            if \
(orientation == QtCore.Qt.Horizontal and  </font></div><div><font face="monospace">   \
0 &lt;= section &lt; self.columnCount() and role == \
QtCore.Qt.EditRole):</font></div><div><font face="monospace">                        \
while len(self.headers) &lt; section + 1:</font></div><div><font face="monospace">    \
self.headers.append(None)</font></div><div><font face="monospace">                    \
self.headers[section] = value</font></div><div><font face="monospace">                \
self.headerDataChanged.emit(orientation, section, section)</font></div><div><font \
face="monospace">                        return True</font></div><div><font \
face="monospace">            return False</font></div><div><br></div>-- <br><div \
dir="ltr">È difficile avere una convinzione precisa quando si parla delle ragioni \
del cuore. - &quot;Sostiene Pereira&quot;, Antonio Tabucchi<br><a \
href="http://www.jidesk.net" \
target="_blank">http://www.jidesk.net</a></div></div></div> </blockquote></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic