Ah, we might be referring to separate issues.
Have a look at this:
class Demo(QtWidgets.QWidget):
"""
Object hierarchy looks like this:
Win (Demo)
|-- Body (QWidget)
|-- Button (QPushButton)
Which translates into:
/Win.Demo/Body.QWidget/Button.QPushButton
"""
def __init__(self, parent=None):
super(Demo, self).__init__(parent)
body = QtWidgets.QWidget()
demo_button = QtWidgets.QPushButton('Hello Demo')
body_button = QtWidgets.QPushButton('Hello Body')
layout = QtWidgets.QHBoxLayout(body)
layout.addWidget(body_button)
layout = QtWidgets.QHBoxLayout(self)
layout.addWidget(demo_button)
layout.addWidget(body)
for widget_, name_ in {
self: 'Win',
body: 'Body',
demo_button: 'Button',
body_button: 'Button',
}.iteritems():
widget_.setObjectName(name_)
if __name__ == '__main__':
import sys
# Set-up PyQt application
app = QtWidgets.QApplication(sys.argv)
win = Demo()
# How we would normally find `Button` within `Demo`
find_button = win.findChild(QtWidgets.QPushButton, 'Button')
# Get an instance from `Demo` by serialised path
button = qtpath.instance(win, '/Body.QWidget/Button.QPushButton')
button = qtpath.instance(win, '/Button.QPushButton')
assert find_button == button
# Get back the serialised path from instance.
assert qtpath.abspath(win, button) == '/Win.Demo/Button.QPushButton'
# A path may return nothing
missing = qtpath.instance(win, '/Body.QWidget/Checkbox.QWidget')
assert missing is None
# findPath searches a hierarchy, regardless of proximity to root
# The following will return `Button` closest to `Win`, there is no
# way to directly reference the nested `Button`.
button = win.findChild(QtWidgets.QPushButton, 'Button')
assert button.parent() == win
button = qtpath.instance(win, '/Body.QWidget/Button.QPushButton')
assert button is not find_button
On 3 June 2014 17:19, Fredrik Averpil <[email protected]> wrote:
Sure. But I was referring to what I usually do, and which could sometimes
> be problematic and where it looks like your qtpaths solves it (I think):
>
> I set the parent from within a subclass with a parent like
>
> self.mainName = parent
>
> And then if this subclass is inherited I may set
>
> self.mainName.subclassName = parent
>
> And so on… and then I would make sure I can access these object names
> throughout my app. So from any class, I can access
> self.mainName.subclassName.widgetName
>
> But in the case of e.g. an image viewer which loads the same widget *n*
> times (e.g. one per image), you can’t hard code names like that. That’s
> where I thought your solution seemed nice, cause then you could serialize
> on window or maybe parent widget.
>
> On a slightly different note… if you load ui files into PySide vs PyQt the
> recommended ways of doing it – PySide/QUiLoader vs PyQt/uic.loadUi – this
> results in different ways of fetching the ui widgets. I solved this with by
> using PySide/pysideuic vs PyQT/uic, as shown in this boilerplate:
> https://github.com/fredrikaverpil/pyVFX-boilerplate
> I wrote more about that in detail here:
> https://github.com/fredrikaverpil/pyVFX-boilerplate/wiki#why-pysideuic
> Not sure if that is at all related to what you're doing but maybe worth
> taking into account.
>
> // Fredrik
>
>
>
> Thanks Fredrik, I’ve also felt the need for something like this.
>> Especially for debugging and CSS.
>>
>> But this could of course sometimes become problematic if a class can have
>> different parents.
>>
>> Could you elaborate on this?
>>
>>
>> --
> You received this message because you are subscribed to the Google Groups
> "Python Programming for Autodesk Maya" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWOdn%2BAXuffvp3MX49YfCEwfuaoDVCNm_pNRH3y4LPCn9A%40mail.gmail.com
> <https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWOdn%2BAXuffvp3MX49YfCEwfuaoDVCNm_pNRH3y4LPCn9A%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>
--
*Marcus Ottosson*
[email protected]
--
You received this message because you are subscribed to the Google Groups
"Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOCp_Sam8kik2ZJ4a1kE0T10KNrtjZn6RD0WC9m6OPXjxQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.