Some Qt Tips
I’ve been jumping back into using Qt recently, this time in Maya and although there are many resources out there to help get started (I will be posting some below) there are a few things I ran into that might help others on the way.
-
Scroll Areas
QtGui.QScrollArea this is interesting, even if you are in Qt Designer you can’t seem to get a preview of it working. To get this to work you need to first make a widget and a layout, set the layout of that widget to the newly created layout. Now create the QScrollArea and there is a method called “setWidget”, you want to pass the widget you first created into this method. Next you should add the scrollArea to whatever layout it’s going to be sitting in. When you want to add something to the scroll area you need to add it to the layout of the widget you first created. A little strange but makes sense once you do it once or twice, see code below.
# create a scroll area for the modules to load myLayout = QtGui.QVBoxLayout() scrollingWidget = QtGui.QWidget() scrollingWidget.setLayout(myLayout) myAwesomeScrollArea = QtGui.QScrollArea() myAwesomeScrollArea.setWidgetResizable(True) myAwesomeScrollArea.setEnabled(True) myAwesomeScrollArea.setMaximumSize(375, 300) # optional myAwesomeScrollArea.setWidget(scrollingWidget) scrollParentLayout.addWidget(myAwesomeScrollArea) # add item to the scroll area myLayout.addWidget(widgetIWantToAdd)
-
Button Click Methods
Let’s say you wanted to have a method run when you click a QPushButton, you would write something like this:
myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(someMethod)
def someMethod(self, *args):
"""
some comments
"""
print 'something'
That works just fine and I’m happy with that. Now what if I needed to pass some values to a method when that button is clicked? If you try to add them like this:
myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(someMethod(arg_1, arg_2))
def someMethod(self, knight, phrase, *args):
"""
some comments
"""
print 'ni!'
You will get an error! You do not want this guy –>
hanging around your code, believe me. So how to remedy this troublesome citizen? We have 2 options, “partial” and “lambda“. Partial is a module that needs to be imported from functools and it allows you to specify a method call followed by the arguments you wish to pass it all in a single line. It runs as a single function thereby letting the button do it’s thing and you can continue on your merry way. Lambda? what is this lambda you speak of? In a nutshell its a syntax in python that lets you create a function on thy fly, so we can can create a quick single function that just runs our method call with arguments passed inside. Here are your jolly examples.
using partial
from functools import partial
myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(partial(someMethod, arg_1, arg_2))
def someMethod(self, knight, phrase, *args):
"""
some comments
"""
print 'ni!'
using lambda
myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(lambda : someMethod('brave sir robin', "I don't know that"))
def someMethod(self, knight, phrase, *args):
"""
some comments
"""
print 'aaarrrggg!'
Here are some other links to get started using PyQt in Maya:
Kristine Middlemiss
There are more links in older posts as well.