Accessing PyQT .ui file buttons in script

Accessing PyQT .ui file buttons in script

adnanmg
Advocate Advocate
3,671 Views
9 Replies
Message 1 of 10

Accessing PyQT .ui file buttons in script

adnanmg
Advocate
Advocate

Hi,

 

I am new to using pyqt and could use some help figuring out how to get this coded just right.

<code>

import maya.cmds as cmds

from pyside2uic import compileUi

 

class className():

     def __init__(self):
         self.VarA = 1

 

    def doSomething(self):

        print selfVarA=1

 

 


from functools import partial

if cmds.window(dialog, exists = True):
    cmds.deleteUI(dialog)

dialog = cmds.loadUI(uiFile='pyqtuifile.ui')
cmds.showWindow(dialog)

 

</code>

 

I'm not sure where to put this code to compile the ui:

<code>

import sys, pprint
from pysideuic import compileUi
pyfile = open("[path to output python file]\output.py", 'w')
compileUi("[path to input ui file]\input.ui", pyfile, False, 4,
False)
pyfile.close()

</code>

 

and where to put this button code and exactly what it should look like?

 

<code>

myPushButton.clicked.connect(self.doSomething)

</code>

 

Thank you.

 

Adnan

Accepted solutions (1)
3,672 Views
9 Replies
Replies (9)
Message 2 of 10

stuzzz
Collaborator
Collaborator

Adnan,

 

It's a poor pratice to use the maya.cmds.loadUI() function.

I highly recommend using executable like pyuic4 which is for (PyQt4). You would be able to find the equivalent for PySide2. I believe it's pyside2-uic but not sure.

 

 

Message 3 of 10

adnanmg
Advocate
Advocate

Thanks Stuzzz. I can change it to that, and I have the basic compiling code for the ui and a button press, but I don't know where to put them. In the class? As a definition for the ui? etc

 

from pyside2uic import compileUi
pyfile = open("[path to output python file]\output.py", 'w')
compileUi("[path to input ui file]\input.ui", pyfile, False, 4,
False)
pyfile.close()

 

myPushButton.clicked.connect(self.doSomething)

0 Likes
Message 4 of 10

stuzzz
Collaborator
Collaborator

"[path to output python file]\output.py"

Is where you have your compiled python file ui as you already know.

You will find a class inside that file.

 

You then have to subclass this class and call in the constructor __init__() the function self.setupUI(self)

 

This will send the parent class object to your ui class. By doing so, you will be able to call you buttons and other widget.

 

here's a proto code:

 

import adnanCompiledUi 

class ParentClass(adnanCompiledUi, QObject):
    def __init__(self):
        super(ParentClass, self).__init__()
            self.setupUI(self)
            mybutton.clicked.connect(self.doSom)
   def doSom(self):
       print "it does something"

 

Message 5 of 10

adnanmg
Advocate
Advocate

Thanks again Stuzzz. I managed to get most of this code together. Now I think the issue is just the window call at the end. It says there is no window, so I am not sure if I put the right thing in there. class Ui_MainWindow(object) comes from the compiled UI file.

 

excerpt from compiled ui file:

class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(273, 245)

 

script that uses ui file:

 

import compiledUI
from compiledUI import Ui_MainWindow
from PySide2.QtCore import QObject


class myClass(Ui_MainWindow,QObject):    
    def __init__():
        super(myClass, self).__init__()
        self.setupUI(self)
        myPushButton.clicked.connect(partial(self.myDef))
    
    def myDef(self):
        print 'It works!'
        
if cmds.window(Ui_MainWindow, exists = True):
    cmds.deleteUI(Ui_MainWindow)
cmds.showWindow(Ui_MainWindow)
# Error: No window found.  # 

 

 

Thanks,

 

Adnan

0 Likes
Message 6 of 10

stuzzz
Collaborator
Collaborator

Salut Adnan,

 

At this point, you have to forget about about maya cmds. Qt is taking over Maya widget display.

 

 

class myClass(QMainWindow, Ui_MainWindow, QObject):    
# you have to put QMainWindow or whatever is your main widget

    def __init__(self): 
       # do not forget self which the current object, I dont' remember if I put that last time but that was obvious to me.
        super(myClass, self).__init__()
        self.setupUi(self)
        #partial won't help at all, i'm not sure to understand what you need to do with that module, lambda would do the trick for advanced slots which is not the case in here
        self.myPushButton.clicked.connect(self.myDef)
        #again don't forget self, myPushButton belong to the Ui_mainWindow which is now contained in the current object. Otherwise it won't find anything. myPushButton and self.mypushButton are 2 differnt things
    
    def myDef(self):
        print 'It works!'

#no cmds ... instantiate the class so you have an object 
obj = myClass()
#then with that object call the Qt function to display the QMainWindow. If you need to know if you window is already shown, just create a global variable 
obj.show()

 

 

Si tu as besoin d'aide, tu as mon skype

 

Message 7 of 10

adnanmg
Advocate
Advocate

Thanks Stuzzz. Each of your replies has gotten me closer. If I was more knowledgeable, I would likely have reached the solution sooner with your earlier posts.

 

import maya.cmds as cmds
import sys
from PySide2 import QtGui, QtCore, QtWidgets
from PySide2.QtWidgets import QDialog
from pyside2uic import compileUi
import compiled_ui
from compiled_ui import Ui_MainWindow

class myClass(Ui_MainWindow,QtWidgets.QMainWindow, QtWidgets.QDialog):    
    def __init__(self):
        super(myClass, self).__init__()
        ui = Ui_MainWindow()
        ui.setupUi(self)        
        ui.myButtonUpdatePushButton.clicked.connect(self.myDef)
    def myDef(self):
        print 'It worked!'
mgt=None            
mgt = myClass()
mgt.show()

 

Next I will work on connecting all the interface items and look into how to keep the window on top of Maya. Thank you.

 

Adnan

0 Likes
Message 8 of 10

stuzzz
Collaborator
Collaborator
Accepted solution

Hi Adnan,

I put some comments bellow

class myClass(Ui_MainWindow,QtWidgets.QMainWindow, QtWidgets.QDialog):    
    def __init__(self):
        super(myClass, self).__init__()
        ui = Ui_MainWindow() # this line is not useful, Ui_MainWindow is already part of this class. 
        ui.setupUi(self) # it has to be self.setupUi(self)        
        ui.myButtonUpdatePushButton.clicked.connect(self.myDef) #again, self.myButtonUpdatePushButton...

    def myDef(self):
        print 'It worked!'

mgt=None    # you can remove that line since you initialise that variable bellow
mgt = myClass()
mgt.show()

 

Message 9 of 10

adnanmg
Advocate
Advocate

Thanks so much for your time and effort in explaining and commenting on the code. I really appreciate it. Now I can get into some nice PyQT ui's for my tools.

 

Adnan

Message 10 of 10

stuzzz
Collaborator
Collaborator

you are welcome.

If you need to have a better comprehension of what we talked about, you may have a look at the subclass and ObjectOrientedProgrammation (OOP) concepts.