Tags

Showing posts with label PyQt. Show all posts
Showing posts with label PyQt. Show all posts

Saturday, July 7, 2012

Qt Designer

Before I begin writing a new UI in PyQt, I like to use Designer to help design my UI and get some ideas down on screen. Designer is a tool that is part of the Qt download package, you can read more about it here.

It's great for putting some widgets and layouts together to visualise how you'd like your UI to look before writing it in Python. Or... you could just use the loadUI() method in Maya to bring it directly into Maya. I don't recommend this though, it's not very optimised and a bit annoying to connect up to custom methods.

Converting a .ui file to .py:
Save your .ui file you've just made in Designer, then load up command window (Windows) or terminal for OSX/Linux. Navigate to the folder where the .ui file is and type:

pyuic4 example.ui > example.py

This converts your .ui to a .py file, which you can now open and pick out what code you'd like to re-write and optimise. Pretty handy!

Check out this page for a slightly dated but still relevant tutorial: http://www.cs.usfca.edu/~afedosov/qttut/

Friday, July 6, 2012

First PyQt window in Maya

I've been using a bit of PyQt in Maya lately and felt like writing up some of my thoughts and solutions to problems I've come across in learning this module. I'm going to assume who ever is reading this has Maya 2011+ installed and intermediate knowledge of Python. If you're reading this as a beginner, I suggest checking out the following sites:

Python in Maya:
http://www.chadvernon.com/blog/resources/python-scripting-for-maya-artists/

PyQt examples:
http://diotavelli.net/PyQtWiki/Tutorials - A collection of PyQt tutorial websites.
http://zetcode.com/tutorials/pyqt4/ - One of my favourites. Note: These are written as standalone applications so won't work in Maya if you copy/paste. Just run these examples in IDLE or interpreter of your choice. This is what causes Maya to stall:
def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
Maya already has it's own QApplication running when it opens, so creating a new one isn't necessary.

Maya PyQt window:
First up, let's start with a simple PyQt window in Maya that has a button which prints out a message!







Paste this into Maya:
from PyQt4 import QtGui
# inherit QtGui.QMainWindow class
class Main(QtGui.QMainWindow):
    def __init__(self, parent=None):
        # runs __init__ in QMainWindow first, then overrides with Main
        super(Main, self).__init__(parent)

        # make a central widget
        self.centralWidget = QtGui.QWidget(self)
        # attach widget to the PyQt window
        self.setCentralWidget(self.centralWidget)

        # set the window title     
        self.setWindowTitle("PyQt window")
       
        # setup UI method
        self.setup_UI()

    # this method add things to the QMainWindow
    def setup_UI(self):
        # make a vertical box layout and set it's parent to the central widget           
        self.mainLayout=QtGui.QVBoxLayout(self.centralWidget)
      
        # make a button
        helloButton=QtGui.QPushButton("Hello")
        # connect button's click slot to say_hello method
        helloButton.clicked.connect(self.say_hello)

        # add the button to the main layout
        self.mainLayout.addWidget(helloButton)

        # set the QMainWindow's height and width to be fixed with given integers
        self.setFixedWidth(200)
        self.setFixedHeight(40)
        
        # bind this layout to QMainWindow
        self.setLayout(self.mainLayout)

    # make a print method
    def say_hello(self, *args):
     print "Hello, Maya!"

# show the window!
myWindow=Main()
myWindow.show()

There you have it! An example of displaying a simple PyQt window in Maya. Over the next couple of weeks I'd like write about some of the following topics:

  • How to work with a QTreeWidget
  • Mixing PyQt and Maya UI (reparenting windows and scripted panels)
  • Custom right click menus on QWidgets
  • Adding popup windows
  • Parenting to Maya's window explanation

Saturday, October 29, 2011

Installing PyQT on OSX

Here are two websites I found very helpful with installing PyQT on my Mac.


This tutorial would seem like the installation is pretty straight forward... however I ran into some trouble where I couldn't install PyQT4 in the terminal due to an annoying error. I eventually came across this site:


...and it worked! Here is a link to a forum post I made which has a lot more information on what was happening and what platform I was using.

EDIT:
Or... you could head over here and read this post:
http://www.justinfx.com/2011/11/09/installing-pyqt4-for-maya-2012-osx/

Inside is a fantastic little package that does everything for you from one install! Awesome.