QStyleOptionViewItem.rect
-
Hello from a Python beginner,
I have to subclass
QStyledItemDelegate. In methodupdateEditorGeometrythe following code is markeddef updateEditorGeometry(self, editor: QWidget, option: QStyleOptionViewItem, index: QModelIndex): rect = option.rect() # <- IDE is claiming rect() is no method of QStyleOptionViewItem rect.setWidth(option.rect().width + 100) editor.setGeometry(rect)According to my IDE (PyCharm) the class
QStyleOptionViewItemhas (in Python) no methodrect(). That's "true" sincerect()is a method of its super classQStyleOption.How to access
rect()(or is itrec?) fromQStyleOptionViewItem?J.
BTW:
the C++ pendant is working:
void CategoryDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const { auto rect = option.rect; rect.setWidth(option.rect.width() + 100); editor->setGeometry(rect); -
no, there is really no rect, see the documentation of PySide I provided. The IDE is right!
@MasterQ said in QStyleOptionViewItem.rect:
no, there is really no rect, see the documentation of PySide I provided. The IDE is right!
No, there is indeed a
rectmember!-
I agree under PySide6, however it is implemented, IDEs do not see
QStyleOptionViewItem.rectnor theQStyleOption.rectwhich is what it is (inherited). Something to do with shiboken I think. Tested under PyCharm, the only/best IDE I trust. -
However whatever the IDE says the PySide6 code with
option.rectdoes work at runtime, and accesses therect. -
Under PyQt6 (in PyCharm) the IDE does find
QStyleOptionViewItem.rect. And it's in__PyQt6_sip.simplewrapper, which is PyQt only.
So the IDE problem is PySide only, and will have something to do with its implementation there (e.g. not being constructed till runtime, which I found with something else recently where IDE could not find a symbol). [Oh yes, that is the implementation of PySide still supporting all the old enums,
Qt.AlightLeft, where you should now use the new ones,Qt.AllignFlag.AlignLeft. PySide6 creates the former "aliases" dynamically at runtime, so they work but the IDE does not know that and says they don't exist.]@SGaist
While runtime behaviour is fine this is a code-time "limitation" of PySide implementation. Whether that is a "bug" or (as I suspect) a "feature" of the PySide implementation, and hence not worth reporting, I do not know. There are other places in Python/PySide/PyQt where things get created at runtime and so are not visible at IDE edit time as part of the implementation. -
-
Hi,
The hint is correct.
rectis not a method it's a public variable. Just use it directly. -
neither
rectnorrect()is recognized

see documentation:
and
this mechanism of PySide seems significantly different to that of C++. Since the PySide documentation seems to be a robot translation from C++, it is not really helpfull.
I don't get it, how PySide is working here.
-
neither
rectnorrect()is recognized

see documentation:
and
this mechanism of PySide seems significantly different to that of C++. Since the PySide documentation seems to be a robot translation from C++, it is not really helpfull.
I don't get it, how PySide is working here.
@MasterQ said in QStyleOptionViewItem.rect:
neither rect nor rect() is recognized
But does it actually work when you run the application?
-
Can you provide a minimal runable script that shows this ?
Creating a QStyleOptionViewItem manually works.>>> from PySide6.QtWidgets import QStyleOptionViewItem >>> option = QStyleOptionViewItem() >>> option.rect PySide6.QtCore.QRect(0, 0, 0, 0) >>> -
no, there is really no rect, see the documentation of PySide I provided. The IDE is right!
@MasterQ said in QStyleOptionViewItem.rect:
no, there is really no rect, see the documentation of PySide I provided. The IDE is right!
No, there is indeed a
rectmember!-
I agree under PySide6, however it is implemented, IDEs do not see
QStyleOptionViewItem.rectnor theQStyleOption.rectwhich is what it is (inherited). Something to do with shiboken I think. Tested under PyCharm, the only/best IDE I trust. -
However whatever the IDE says the PySide6 code with
option.rectdoes work at runtime, and accesses therect. -
Under PyQt6 (in PyCharm) the IDE does find
QStyleOptionViewItem.rect. And it's in__PyQt6_sip.simplewrapper, which is PyQt only.
So the IDE problem is PySide only, and will have something to do with its implementation there (e.g. not being constructed till runtime, which I found with something else recently where IDE could not find a symbol). [Oh yes, that is the implementation of PySide still supporting all the old enums,
Qt.AlightLeft, where you should now use the new ones,Qt.AllignFlag.AlignLeft. PySide6 creates the former "aliases" dynamically at runtime, so they work but the IDE does not know that and says they don't exist.]@SGaist
While runtime behaviour is fine this is a code-time "limitation" of PySide implementation. Whether that is a "bug" or (as I suspect) a "feature" of the PySide implementation, and hence not worth reporting, I do not know. There are other places in Python/PySide/PyQt where things get created at runtime and so are not visible at IDE edit time as part of the implementation. -
-
@JonB said in QStyleOptionViewItem.rect:
@MasterQ said in QStyleOptionViewItem.rect:
no, there is really no rect, see the documentation of PySide I provided. The IDE is right!
No, there is indeed a
rectmember!Thank you for that information.
My last tests confirm your answer, I was wrong about missing rect. Sry for the confusion.
-
M MasterQ has marked this topic as solved on