QtDesigner UI Files in H16

   3704   1   1
User Avatar
Member
16 posts
Joined: March 2016
Offline
Hi Folks,

I'm trying to launch a UI in H16 from a shelf tool using PySide2's QUiLoader. I'm basically following the code example from this page [sidefx.com], but with a .ui file rather than building it all myself (it's a pretty large complex UI). The problems I'm having are the following:
  • If I implement ui.show() outside of the class, the UI shows up as a blank window.
  • If I implement self.ui.show() inside of the class, the UI shows up properly, but I can't seem to be able to close the window using any of my buttons - self.ui.buttonName.clicked.connect(self.close) does nothing. Plus, this seems incorrect anyways.
  • Implementing a closeEvent method in the class as suggested here [sidefx.com] does not close the window, it just removes the inherited stylesheet formatting - leaving my ui ugly.

What am I doing wrong? As far as I can tell, I'm following all the provided examples for running PySide inside Houdini. My abbreviated code is:
import hou
from PySide2 import QtCore, QtGui, QtWidgets, QtUiTools
class HDAManager(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super(HDAManager, self).__init__(parent)
        ui_file = QtCore.QFile('/path/to/file.ui')
        ui_file.open(QtCore.QFile.ReadOnly)
        self.ui = QtUiTools.QUiLoader().load(ui_file, parentWidget=self)
        ui_file.close()
        self.makeConnections()
        # ... more code ... #
    def makeConnections(self):
        self.ui.cancelButton.clicked.connect(self.close)
        self.ui.otherCancelButton.clicked.connect(self.closeEvent)
    def closeEvent(self, event):
        self.setParent(None)
def run():
    window = HDAManager()
    window.setParent(hou.qt.mainWindow(), QtCore.Qt.Window)
    window.show()
User Avatar
Member
16 posts
Joined: March 2016
Offline
I had an idea and tracked it down and sure enough I found the problem! Always happens right after posting haha… Anyways the issue was that, when I first created my QtDesigner ui file, I had created it as a MainWindow, not as a Widget. So when adding the widget to a layout in the class (part of the “more code” section), nothing showed up because it couldn't add a MainWindow as a widget to a layout. But every MainWindow comes with a centralWidget, so the solution is this line instead:
layout.addWidget(self.ui.centralWidget)
So essentially instead of adding the window of my ui file, I'm adding the centralWidget inside that window. Now everything works including window.show() outside of the class, and button.clicked.connect(self.close)! Yay!
  • Quick Links