Jump to content
Sign in to follow this  
kiryha

PySide to PySyde2 UI

Recommended Posts

Hello!

I have my UI window done in QT Designer (with PySide) and I launch this window with a code in PyCharm successfully:

from PySide.QtGui import *
from UI import createScene_Main

class CreateScene(QWidget, createScene_Main.Ui_CreateScene):
    def __init__(self):
        super(CreateScene, self).__init__()

        # SETUP UI
        self.setupUi(self)

app = QApplication([])
CS = CreateScene()
CS.show()
app.exec_()

When I try to import this code in Houdini (changing PySide to PySide2 in this code and in UI file as well) I get an error: NameError: name 'QWidget' is not defined. How can I fix it?

Share this post


Link to post
Share on other sites

PySide2 / Qt5 has migrated many of their classes away from the QtGui module to QtWidgets. QWidget is one of them. 

I don't like the "from module import *" way of doing things in general because it makes things like this harder to track later on, but in your example you could add the line:

from PySide2.QtWidgets import *

 

  • Like 1

Share this post


Link to post
Share on other sites

Thanks, Henry, that works, but then I get a new error in my UI file:
 

self.verticalLayout = QtGui.QVBoxLayout(CreateScene)
AttributeError: 'module' object has no attribute 'QVBoxLayout'

The PySyde2 import in UI file done like: from PySide2 import QtCore, QtGui

I used QT designer shipped with PySide to build UI. It will be a problem if I will run those UIs with PySide2, right? I just can't pip install PySide2 to my PC, getting an error (Could not find a version that satisfies the requirement PySide2 (from versions: ) No matching distribution found for PySide2)

51 minutes ago, toadstorm said:

I don't like the "from module import *" way

How should I import PySide modules in a better way?

Share this post


Link to post
Share on other sites

QVBoxLayout is also part of QtWidgets now, not QtGui, so you'd have to modify your import accordingly.

Everyone has different opinions on the import style, but I typically use something like:

from PySide2 import QtGui, QtWidgets, QtCore

That way when you're calling these functions later on, it's very clear which module you're using:

self.verticalLayout = QtWidgets.QVBoxLayout(parent)

 

For cross-compatibility between PySide and PySide2, I'd recommend checking out Qt.py. It allows you to write all of your code as if it were PySide2, but will translate to PyQt4, PySide, PyQt5, and PySide2 interchangeably. There's a few gotchas when dealing with models and .data() overloads, but it's mostly seamless.

I'd recommend avoiding Qt Designer in general as it has some limitations that may cause more harm than good later on. If you need it, though, you can use Qt.py's QtCompat module for the loadUi() function.

  • Like 1

Share this post


Link to post
Share on other sites

I don`t need a cross-compatibility between PySide and PySide2. Just building tools which will work in Houdini 16.5 and later, trying to keep things simple as possible for reliability. 
If I understand correctly, PySide2 requires Python 3.5, but it is Python27 in 16.5, so how it works then?

I guess it worth to explain why I had an initial question at least now. I have some experience building UI with PySide for Maya and Nuke and I was thinking it would be the same process to build a UI for Houdini but it is not.

Edited by kiryha

Share this post


Link to post
Share on other sites

I found a working solution (using a designer, which is fine for me right now):

import hou
import os
from PySide2 import QtCore, QtUiTools, QtWidgets

class CreateScene(QtWidgets.QWidget):
    def __init__(self):
        super(CreateScene,self).__init__()
        ui_file = "pathTo/createScene.ui"
        self.ui = QtUiTools.QUiLoader().load(ui_file, parentWidget=self)
        self.setParent(hou.ui.mainQtWindow(), QtCore.Qt.Window)
        
def run():
    CS = CreateScene()
    CS.show()

 

Share this post


Link to post
Share on other sites
On 9/27/2018 at 6:28 AM, kiryha said:

PySide2 requires Python 3.5, but it is Python27 in 16.5, so how it works then?

This is not true. PySide2 (currently officially maintained my Qt company) is built for Py2 and Py3.

 
Quote

 I have some experience building UI with PySide for Maya and Nuke and I was thinking it would be the same process to build a UI for Houdini but it is not.

The difference is in Qt4/Qt5, not in Maya/Houdini.

 

Share this post


Link to post
Share on other sites

Somebody on tech artist forum told that PySide2 is supported by Python 3 only when I was not able to install it on my computer (but I notice that according to a docs it's not the case). I am still not able to do it. 
 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×