Discussion:
[Maya-Python] Can you reorder a .ui file in a function?
kiteh
2018-11-29 02:29:06 UTC
Permalink
Hi all, I have a .ui file in which I have created some widgets using
qt-designer in a specific order.

Eg.
QFrame
|- horLayout_01
|-|- buttonA
|- horLayout_02
|-|- LabelB
|-|- buttonB
|- horLayout_03
|-|- buttonC
|-|- buttonC

And later on, this particular UI is being called and use together with a
QObject (which is also another UI, but created in pythonic terms), such
that the code looks something like this:
# `mainInfoPanel` is the file that uses .ui file for setting all the
signals within..
# `BasePanel` is the GUI created pythonically

import mainInfoPanel as mainInfoWidget

class infoPanel(BasePanel):
def _build_ui(self, parent):
self._widget = mainInfoWidget(parent)
super(infoPanel, self)._build_ui(parent)

And so, my question here is - will it be possible to reorder the layout of
the .ui file it is reading from under the `_build_ui`?

Eg. to this:
QFrame
|- horLayout_01
|-|- buttonA
|- horLayout_03
|-|- buttonC
|-|- buttonC
|- horLayout_02
|-|- LabelB
|-|- buttonB
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ee8c06f0-9627-4060-993b-80b9959926ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-11-29 03:47:42 UTC
Permalink
You can programatically reorder a layout after it has been read in from the
ui file into QObjects:

class Widget(QtGui.QWidget):

def __init__(self, parent=None):
super(Widget, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)

self.b1 = QtGui.QPushButton("one", self)
self.b2 = QtGui.QPushButton("two", self)

self.l1 = QtGui.QHBoxLayout()
self.l1.addWidget(self.b1)

self.l2 = QtGui.QHBoxLayout()
self.l2.addWidget(self.b2)

layout.addLayout(self.l1)
layout.addLayout(self.l2)

self.b1.clicked.connect(self.reorder)
self.b2.clicked.connect(self.reorder)

def reorder(self):
item = self.layout().takeAt(1)
self.layout().insertItem(0, item)


Justin
Post by kiteh
Hi all, I have a .ui file in which I have created some widgets using
qt-designer in a specific order.
Eg.
QFrame
|- horLayout_01
|-|- buttonA
|- horLayout_02
|-|- LabelB
|-|- buttonB
|- horLayout_03
|-|- buttonC
|-|- buttonC
And later on, this particular UI is being called and use together with a
QObject (which is also another UI, but created in pythonic terms), such
# `mainInfoPanel` is the file that uses .ui file for setting all the
signals within..
# `BasePanel` is the GUI created pythonically
import mainInfoPanel as mainInfoWidget
self._widget = mainInfoWidget(parent)
super(infoPanel, self)._build_ui(parent)
And so, my question here is - will it be possible to reorder the layout of
the .ui file it is reading from under the `_build_ui`?
QFrame
|- horLayout_01
|-|- buttonA
|- horLayout_03
|-|- buttonC
|-|- buttonC
|- horLayout_02
|-|- LabelB
|-|- buttonB
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/ee8c06f0-9627-4060-993b-80b9959926ac%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/ee8c06f0-9627-4060-993b-80b9959926ac%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA38Gfzy_ZpTxA3axL5FXbubMnHPs1xbnvWKaOV7JEojWQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
kiteh
2018-11-29 17:05:42 UTC
Permalink
Hi Justin, thank you for the example.

Perhaps I have not phrase my question properly (was being bad at putting in
words)
Using the example you have given, the reordering only happens if you click
on either buttons.. But I am trying to achieve the ui setup as the
code/tool is being called, and is it possible to set which layout to be at
which particular order?
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/1106ccf4-3a45-4816-9ac4-a4ceb3627152%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-11-29 18:10:20 UTC
Permalink
Post by kiteh
Hi Justin, thank you for the example.
Perhaps I have not phrase my question properly (was being bad at putting
in words)
Using the example you have given, the reordering only happens if you click
on either buttons.. But I am trying to achieve the ui setup as the
code/tool is being called, and is it possible to set which layout to be at
which particular order?
I don't think we have misunderstood each other, actually. My example only
used button clicks to illustrate the before and after effect. Is it not
clear how you could apply this example to your build method to swap the
ordering of layouts once before the widget is shown? UI created with Qt
Designer will end up saving a reference to every widget and layout that it
creates, as fields on the top level widget. So it should be pretty easy to
move them around.
Post by kiteh
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/1106ccf4-3a45-4816-9ac4-a4ceb3627152%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/1106ccf4-3a45-4816-9ac4-a4ceb3627152%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3SU62os_AV%3DJNtHgFQ1W5fRMb6VO8M0n_0QbbSiaKZ7w%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
kiteh
2018-11-29 18:52:42 UTC
Permalink
After tinkering some more with my code, using your example as a basis, I
sort of get it to work... My bad on this :)

I do have a question though.. My company is using qt designer version 4.8.5
which is pretty old, I guess?
Even so, when I was trying to get the list of widgets and its position
within using the following code:
for i in range(self.layout().count()):
item_name = self.layout().itemAt(i).objectName()
print "Name - {}, Position - {}".format(item_name, i)

It seems to works for all other widgets with the exception of
QHorizontalLayout and QVerticalLayout in which in the designer version I am
using, it does not have a "objectName" field and hence it errors out for me
as there are 2 instances where I am using the above mentioned layout each.

Luckily, while these 2 layouts are the top 2 widgets, I can use `self.layout().takeAt(0)`
or `self.layout().takeAt(1)` and re-insert them to be the last second and
third in position.

Even so, is there a better way that I can query the index?
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/ae2d37a0-feeb-4e50-813d-716068552e01%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-11-29 19:31:49 UTC
Permalink
Post by kiteh
After tinkering some more with my code, using your example as a basis, I
sort of get it to work... My bad on this :)
I do have a question though.. My company is using qt designer version
4.8.5 which is pretty old, I guess?
Even so, when I was trying to get the list of widgets and its position
item_name = self.layout().itemAt(i).objectName()
print "Name - {}, Position - {}".format(item_name, i)
It seems to works for all other widgets with the exception of
QHorizontalLayout and QVerticalLayout in which in the designer version I am
using, it does not have a "objectName" field and hence it errors out for me
as there are 2 instances where I am using the above mentioned layout each.
You would need to account for the fact that itemAt() returns a QLayoutItem,
which wraps the actual layout object (in order to get its object name)
http://doc.qt.io/archives/qt-4.8/qlayoutitem.html
Post by kiteh
Luckily, while these 2 layouts are the top 2 widgets, I can use `self.layout().takeAt(0)`
or `self.layout().takeAt(1)` and re-insert them to be the last second and
third in position.
Even so, is there a better way that I can query the index?
Since indexOf() only tells you the index of widgets in the layout, you
would have to loop over each item the way you are doing and compare the
item to a particular layout to get its index. This could be made into a
method like self._layoutIndexOf(layout, item)
If item is an instance of QWidget then you can call layout.indexOf(item)
Otherwise you have to loop over all the items and if the loop item is a
QLayoutItem then you have to do currentItem.layout() == item
Post by kiteh
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/ae2d37a0-feeb-4e50-813d-716068552e01%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/ae2d37a0-feeb-4e50-813d-716068552e01%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA1fJRbXLpr6uUaTPqyAsWXdatjkKM-FDvaAJKd_hxzszw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
kiteh
2018-11-29 23:55:27 UTC
Permalink
It seems that these layouts and the spacerItem are returning me as None
when I tried using `.widget()`
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/3fa01ecd-1520-4b4e-bedb-da70bab7340b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Justin Israel
2018-11-30 01:01:05 UTC
Permalink
Post by kiteh
It seems that these layouts and the spacerItem are returning me as None
when I tried using `.widget()`
That is expected right?
widget() returns widget
layout() returns layout
spacerItem() returns a spacer item
Post by kiteh
--
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
To view this discussion on the web visit
https://groups.google.com/d/msgid/python_inside_maya/3fa01ecd-1520-4b4e-bedb-da70bab7340b%40googlegroups.com
<https://groups.google.com/d/msgid/python_inside_maya/3fa01ecd-1520-4b4e-bedb-da70bab7340b%40googlegroups.com?utm_medium=email&utm_source=footer>
.
For more options, visit https://groups.google.com/d/optout.
--
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 python_inside_maya+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3nAHHX1ZKmC3HmdCbcsMjw3h-qF2GZ4OusSQe8O1tTBA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Search results for '[Maya-Python] Can you reorder a .ui file in a function?' (Questions and Answers)
14
replies
Creating a "Why we should switch to Mac" Speech. Help Please?
started 2007-10-26 15:44:14 UTC
desktops
Loading...