Qt.py and .ui files

691 views
Skip to first unread message

Marcus Ottosson

unread,
Sep 7, 2016, 5:00:42 PM9/7/16
to python_in...@googlegroups.com

Hi all,

Many of you have been asking about working with ui files in a way maintains the cross-binding benefit of Qt.py.

The problem has been that:

  • a) PySide and PyQt implement the in-memory loading mechanism loadUi() differently and
  • b) Files compiled via the command-line leave artifacts behind that make it difficult to run the result on another binding.

One suggestion was to:

  1. Compile to Python via PySide2
  2. Convert to cross-compatible Qt.py code afterwards.

So that’s what is happening here.

$ pyside2-uic my_ui.ui -o my_ui.py
$ python -m Qt --convert my_ui.py
$ cat my_ui_.py

What we need now is:

  1. Loads of .ui files to test with (if you have a stash, now’s the time to share!)
  2. Your feedback on whether this approach is one worth pursuing

Let me know what you think!

Best,
Marcus

--
Marcus Ottosson
konstr...@gmail.com

Justin Israel

unread,
Sep 7, 2016, 5:38:35 PM9/7/16
to python_in...@googlegroups.com
Hey Marcus,

It's a cool goal that you are trying to make everyone's workflows more compatible and consistent. Keep it up.

Quick question about the approach to providing the convert functionality... Do you think splitting the file on a '\n' and doing line-by-line string replacements is a viable approach? Could it be possible to break the code if someone has a '\n' in some text of their widget or the word 'PySide2"? I know its pretty specific right now with the 2 replacements its doing, but you might end up adding more changes once you get feedback. 
Have you looked at a safe approach that parses and rewrites the AST? Something like the rope project   or even the ast stdlib module if you are really against dependencies? At least you could be sure that you are only refactoring, say, the renaming of an import. Or renaming a method of a class.

Just throwing that out there to see if it helps.

Justin

--
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_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAFRtmOAjdKbcXmfG1LNSyA514-HC9XC3UkxtXq0fNUb6Nj%2BAJg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Marcus Ottosson

unread,
Sep 7, 2016, 6:23:11 PM9/7/16
to python_in...@googlegroups.com, python_in...@googlegroups.com
That's a good idea!

I'm expecting this initial proposal to mostly get the conversation started and iterate on it until all is well. Considering the source files are all automatically generated by another program, I'm expecting a finite set of edge cases. This does sound like one of them.

But, don't you feel working at the abstract tree-level is perhaps a tad premature? Spontaneously I'd first try splitting lines at file-open, via e.g. file.readlines().

Another reason for going with .replace() as opposed to jumping straight into regular expressions is readability and maintainability. I want to enable others to understand the conversion and make changes to it where necessary. Both re and AST carry the potential of making these options fairly limited.

Thanks, and if you have any ui files (I know you generally don't work with them, neither do I) do let me know!

Justin Israel

unread,
Sep 7, 2016, 6:56:04 PM9/7/16
to python_in...@googlegroups.com
On Thu, Sep 8, 2016 at 10:23 AM Marcus Ottosson <konstr...@gmail.com> wrote:
That's a good idea!

I'm expecting this initial proposal to mostly get the conversation started and iterate on it until all is well. Considering the source files are all automatically generated by another program, I'm expecting a finite set of edge cases. This does sound like one of them.

True. This problem can occur in an edge case. And you are write about it being a smaller surface space for problems since its auto generated. But part of the output is not auto-generated, such as any text a user may have set into widgets. That is a wide open variable.
 

But, don't you feel working at the abstract tree-level is perhaps a tad premature? Spontaneously I'd first try splitting lines at file-open, via e.g. file.readlines().

When it comes to modifying source code in the form of a code generator that you are providing to others, maybe its not so premature. Since you don't have control over what ends up being in the UI file, you may end up string replacing things you didn't expect. Again my example being someone using a newline character in some text of their widget. Or using words that you have set up to be replaced, where you intended for them to be modules or function names, and someone used them elsewhere.
 

Another reason for going with .replace() as opposed to jumping straight into regular expressions is readability and maintainability. I want to enable others to understand the conversion and make changes to it where necessary. Both re and AST carry the potential of making these options fairly limited.

Definitely agree that adding another replace() line is trivial. And you have structured it in a way where it's at the top and easy to see. But again, doing text replacement could have unwanted effects since you have no control over doing syntax-safe replacements here. You just see a line and replace matching text. It could be possible to use the AST approach, but expose it as some kind of high level thing like making a canned set of refactor options, which get added to a list?

* replace module name x with y
* replace class name x with y
* replace function name x with y
* replace method name MyClass.x with MyClass.y

I 100% agree that this is a more complicated solution than string replacements. It just depends if your refactors will get more complicated or end up being more fragile in the way they are currently done. 
 

Thanks, and if you have any ui files (I know you generally don't work with them, neither do I) do let me know!

If I used UI files anymore, I would definitely share them! :-)
 

Marcus Ottosson

unread,
Sep 8, 2016, 3:09:48 AM9/8/16
to python_in...@googlegroups.com
Good thoughts.

I've always wanted a reason to get into AST, perhaps now is the time.

Thanks Justin.

Justin Israel

unread,
Sep 8, 2016, 4:38:34 AM9/8/16
to python_in...@googlegroups.com
Sweet. Let us know if you end up going down that path. Would love to see it in action. 

--
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_m...@googlegroups.com.

Marcus Ottosson

unread,
Sep 8, 2016, 5:09:04 AM9/8/16
to python_in...@googlegroups.com
Will do.

The current plan is:

1. Test the plain `.replace()` method on ~10.000 or more random .ui files from GitHub
2. I expect to encounter plenty of edge cases, and if I do, look into AST.
3. If that fails, look into whatever the https://github.com/rferrazz/pyqt4topyqt5 project is doing and try that.

I'm sure each of these approaches will have warts of its own, so if there are any other suggestions, I'm all ears.

On 8 September 2016 at 09:38, Justin Israel <justin...@gmail.com> wrote:


On Thu, 8 Sep 2016, 7:09 PM Marcus Ottosson <konstr...@gmail.com> wrote:
Good thoughts.

I've always wanted a reason to get into AST, perhaps now is the time.

Thanks Justin.

Sweet. Let us know if you end up going down that path. Would love to see it in action. 

--
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+unsub...@googlegroups.com.

--
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+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAPGFgA3aQFgm0BbA0La_ibx%3DFV-ExARHoOecAWE68XUXUwZbQg%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

Jason Brummett - gmail

unread,
Sep 8, 2016, 5:31:17 PM9/8/16
to python_in...@googlegroups.com
W c

Sent from my Virgin Mobile Android-Powered Device


----- Reply message -----
From: "Justin Israel" <justin...@gmail.com>
To: <python_in...@googlegroups.com>
Subject: [Maya-Python] Qt.py and .ui files
Date: Thu, Sep 8, 2016 2:38 am




On Thu, 8 Sep 2016, 7:09 PM Marcus Ottosson <konstr...@gmail.com> wrote:
Good thoughts.

I've always wanted a reason to get into AST, perhaps now is the time.

Thanks Justin.

Sweet. Let us know if you end up going down that path. Would love to see it in action. 

--
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_m...@googlegroups.com.

--
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_m...@googlegroups.com.

Marcus Ottosson

unread,
Sep 21, 2016, 1:39:44 AM9/21/16
to python_in...@googlegroups.com

Ok, we’ve made an alpha version of this to test with.

If you’re interested in compiling Qt Designer files for use across bindings, your help would be most appreciated. The rule is, .ui files are first compiled with the PySide2 compiler, and then converted to Qt.py.

$ pip install Qt.py
$ pyside2-uic my_ui.ui -o my_ui.py
$ python -m Qt --convert my_ui.py
# Creating "my_ui_backup.py"..
# Successfully converted "my_ui.py"

The resulting Python module should be compatible with every binding. If it’s not, submit a bug report or pull-request and we’ll see it fixed!

In Maya 2017, the compiler executable will be located in your Maya installation directory, alongside mayapy and maya.

Let me know how that works for you!

Reply all
Reply to author
Forward
0 new messages