PDA

View Full Version : python with QTdesigner, how to call class function?


tistatos
02-03-2011, 11:26 AM
I'm working on making a tool for maya 2011 and i use QTDesigner for creating the UI, but it seems like i've hit a problem. I want to use a class to easier work with my script, and in my ui I have buttons that i'd would like to be able to call class functions within my instance of my class.

Is this possible or do i have to go the long way and create my whole UI in Maya Python?

Thanks

pritishd
02-03-2011, 07:21 PM
I'm working on making a tool for maya 2011 and i use QTDesigner for creating the UI, but it seems like i've hit a problem. I want to use a class to easier work with my script, and in my ui I have buttons that i'd would like to be able to call class functions within my instance of my class.

Is this possible or do i have to go the long way and create my whole UI in Maya Python?

Thanks

Haven't used QT, but when a function is called by the QT interface, does it execute in mel or python?

scottiedoo
02-03-2011, 11:33 PM
I'm a little confused with what you are asking, but I'll try to answer anyways.

Do you mean you would like your ui button to call a method on some some specific object. Such as:

button click -> calls myObject.someMethod()

If that is the case, then you will need to store a pointer to your object that the ui can locate meaning:

class MyWidow(QTCore.QWindow):
def __init__(self, myObject, parent=None):
self.myObject = myObject
launch window...

def buttonClickedEvent(self):
self.myObject.someFunction()

Or you would some how declare the myObject some other way to fit your purpose.

Sorry, I can't be exact on my coding since I don't have time to write and check, so it's just to get the idea across.

You can get the object you want to call other ways such as

def buttonClickedEvent(self):
myObject = funcToGetObject()
myObject.someFunction()

etc...

pritishd: If using PyQt, then you are using Python that listens for the signals from the window which calls a Python function, from there you can call MEL or another Python function as you see fit.

pritishd
02-03-2011, 11:46 PM
pritishd: If using PyQt, then you are using Python that listens for the signals from the window which calls a Python function, from there you can call MEL or another Python function as you see fit.

Thanks for that info. I really have to start learning PyQt next, cause Maya 2011 messed up my pretty UI for my auto rigging script.

scottiedoo
02-04-2011, 12:09 AM
yeah it's a bit of a headache to adjust all the tricks to get it looking how liked it to begin with and now things end up in the wrong places.

But once you get you understand Qt, it's so much faster to build windows with a lot less code if building through the designer. Looking forward to some new widgets to come out of the community in the near future.

pritishd
02-04-2011, 01:02 AM
yeah it's a bit of a headache to adjust all the tricks to get it looking how liked it to begin with and now things end up in the wrong places.

But once you get you understand Qt, it's so much faster to build windows with a lot less code if building through the designer. Looking forward to some new widgets to come out of the community in the near future.

Would you be able to share links to the resources you used to learn about QT interface and their integration inside of maya?

scottiedoo
02-04-2011, 04:02 AM
Sure,

There is a book (it may be the only) on Python and Qt. Very good and covers a lot of things:
Rapid GUI Programming with Python and Qt by Mark Summerfield

On the Maya side, look at this page in the documentation:
file:///C:/Program%20Files/Autodesk/Maya2011/docs/Maya2011/en_US/files/WS73099cc142f48755527cc80126d167ffa2-7170.htm

or do a search for Qt and there is some pages that talk about using it. It's pretty simple, but not exactly easy for absolute beginners to understand the concepts.

Also just the Qt documentation will cover all of the methods for each class and widget online:
http://doc.qt.nokia.com/


You don't have to learn Qt in Maya, you can just install and use it in the normal python environment.

It's a pretty large system and has a lot of great features, so don't let the complexity scare you. I think it's easier to learn and use than Tk/Tcl and Wx in my opinion which is probably why it is getting more popular.

Good luck,
Scott

pritishd
02-04-2011, 04:27 AM
Sure,

There is a book (it may be the only) on Python and Qt. Very good and covers a lot of things:
Rapid GUI Programming with Python and Qt by Mark Summerfield

On the Maya side, look at this page in the documentation:
file:///C:/Program%20Files/Autodesk/Maya2011/docs/Maya2011/en_US/files/WS73099cc142f48755527cc80126d167ffa2-7170.htm

or do a search for Qt and there is some pages that talk about using it. It's pretty simple, but not exactly easy for absolute beginners to understand the concepts.

Also just the Qt documentation will cover all of the methods for each class and widget online:
http://doc.qt.nokia.com/


You don't have to learn Qt in Maya, you can just install and use it in the normal python environment.

It's a pretty large system and has a lot of great features, so don't let the complexity scare you. I think it's easier to learn and use than Tk/Tcl and Wx in my opinion which is probably why it is getting more popular.

Good luck,
Scott

Thanks for that scott. Appreciate your help

tistatos
02-04-2011, 08:28 AM
scottiedoo, I'm not sure if we're talking about the same thing...

I'm using maya's built-in loadUI() command to load a UI file created in QTdesigner, so I'm not actually writing any QT code inside of maya.

Inside QT Designer, to add a function to a button, you add a new dynamic property and use "-command"/"+command" as property name, and then the actual value of this property is just a string to a function.
So here I can't write self.buttonClick() because the UI doesnt know what "self" is, and i can't use myClass.buttonClick() because then I won't be working with the instance of the class..

I guess I have to do something like this:

MyClass():
"""
class containing most of the script functions and variables
"""
def __init__(self):
#example vars
self.var = none
self.x = 10
self.dialog = cmds.loadUI(uifile="C:\\uifile.ui")
def Button(self):
"""
button stuff
"""
print (self.x)

#create instance of class
tool = MyClass()

def buttonEvent():
#this is what the button in the ui file calls, which then calls the function inside the
#instance of the class
tool.Button()

And it feels like stuff could go wrong doing it like that :)

scottiedoo
02-04-2011, 05:08 PM
I never liked using a dynamic property as trigging method for functions like the way Maya tries to do. It will work for simple commands, but I never use this way. I prefer to directly use the PyQt module for using the signals/slots.

Here is what I would do. You can do this if you built your UI in Python or the designer UI file. This assumes you have installed the correct version of PyQt so you can import in the modules.


from PyQt4 import QtCore, QtGui, Qt, uic

class MyWindow(QtGui.QWidget):

def __init__(self, parent=None):
'''Initialize a QWidget and load our UI, then show it'''
super(MyWindow, self).__init__(parent)
uic.loadUi("PATH TO UI FILE", self)
self.show()

@QtCore.pyqtSignature('')
def on_myButton_released(self):
'''this is an automatic way to register a signal/slot with Qt so when myButton is released, it will call this method'''
myObject = functionToGetObject()
myObject.methodToCall()



This puts your UI in a class so you can create much cleaner code to handle and store data within your window. Maya's way of loading a UI is more MEL oriented which I don't like personally. The above is closer to what you would do outside of Maya but works inside as long as you installed the same version of PyQt on your computer as Maya uses (otherwise you will get an error when you import)

Let me know if this helps or if I need to explain anything.

CGTalk Moderation
02-04-2011, 05:08 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.