Truthiness of a QHeaderView
Maurizio Berti
maurizio.berti at gmail.com
Tue Sep 3 19:05:44 BST 2024
It behaves similarly to other classes that have a mono dimensional "length"
(eg. having a count(), like QLayout or QListWidget).
When QTableWidget is created with 0 rows or columns, the respective headers
will have a length based on those counts.
The following will print, respectively, "no" and "yes":
tw = QTableWidget(1, 0)
print('yes' if tw.horizontalHeader() else 'no')
print('yes' if tw.verticalHeader() else 'no')
That's because there is only one row (so the truthiness of the vertical
header is true) but no column (and the truthiness of the horizontal header
is false).
QTableView *always* has both a horizontal and vertical header, so it will
never return None for their getter functions.
Regards,
MaurizioB
Il giorno lun 2 set 2024 alle ore 20:14 Colin Macdonald <cbm at math.ubc.ca>
ha scritto:
> Sometime "recently" (maybe 6.6 or 6.7) I think the truthiness of
> QHeaderView has changed, so that `if header:` is no longer True.
>
> Here is the __init__ method of my subclass of QTableWidget:
>
> header = self.horizontalHeader()
> print(header)
> if header:
> print(f"truthiness of {header} confirmed")
> else:
> print(f"{header} is not truthy!")
> if header is not None:
> print(f"{header} is not None")
>
> The output is unexpected (to me):
>
> <PyQt6.QtWidgets.QHeaderView object at 0x7fcc20d3ee90>
> <PyQt6.QtWidgets.QHeaderView object at 0x7fcc20d3ee90> is not truthy!
> <PyQt6.QtWidgets.QHeaderView object at 0x7fcc20d3ee90> is not None
>
> I could probably try to narrow this down a bit, make a MWE etc. But
> before I do, does this look familiar? Are my expectations valid? Is it
> normal to explicitly check for None rather than to assume truthiness of
> PyQt objects?
>
> thanks,
> Colin
>
> --
> Colin Macdonald
> Associate Professor
> Dept of Math, UBC
>
--
È difficile avere una convinzione precisa quando si parla delle ragioni del
cuore. - "Sostiene Pereira", Antonio Tabucchi
http://www.jidesk.net
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20240903/b3cdf1d1/attachment.htm>
More information about the PyQt
mailing list