<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title> &#187; lambda</title>
	<atom:link href="http://www.mattmurrayanimation.com/archives/tag/lambda/feed" rel="self" type="application/rss+xml" />
	<link>http://www.mattmurrayanimation.com</link>
	<description></description>
	<lastBuildDate>Fri, 22 Mar 2013 06:10:39 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Some Qt Tips</title>
		<link>http://www.mattmurrayanimation.com/archives/269</link>
		<comments>http://www.mattmurrayanimation.com/archives/269#comments</comments>
		<pubDate>Thu, 08 Mar 2012 05:57:24 +0000</pubDate>
		<dc:creator>mattanimation</dc:creator>
				<category><![CDATA[Maya]]></category>
		<category><![CDATA[pyQT]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[how do I use a QScrollArea in PyQt?]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[partial]]></category>
		<category><![CDATA[QPushButton]]></category>
		<category><![CDATA[QScrollArea]]></category>
		<category><![CDATA[Tips]]></category>

		<guid isPermaLink="false">http://www.mattmurrayanimation.com/?p=269</guid>
		<description><![CDATA[I&#8217;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 [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;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.</p>
<ul>
<li>
<h3>Scroll Areas</h3>
</li>
</ul>
<p>QtGui.QScrollArea  this is interesting, even if you are in Qt Designer you can&#8217;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 &#8220;setWidget&#8221;, you want to pass the widget you first created into this method. Next you should add the scrollArea to whatever layout it&#8217;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.</p>
<pre class="brush: python; title: ; notranslate">

# 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)

</pre>
<ul>
<li>
<h3>Button Click Methods</h3>
</li>
</ul>
<p>Let&#8217;s say you wanted to have a method run when you click a QPushButton, you would write something like this:</p>
<pre class="brush: python; title: ; notranslate">

myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(someMethod)

def someMethod(self, *args):
    &quot;&quot;&quot;
    some comments
    &quot;&quot;&quot;
    print 'something'

</pre>
<p>That works just fine and I&#8217;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:</p>
<pre class="brush: python; title: ; notranslate">

myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(someMethod(arg_1, arg_2))

def someMethod(self, knight, phrase, *args):
    &quot;&quot;&quot;
    some comments
    &quot;&quot;&quot;
    print 'ni!'

</pre>
<p>You will get an error! You do not want this guy &#8211;&gt;  <img src="http://i3.kym-cdn.com/entries/icons/original/000/001/368/i-am-error.jpeg" alt="" width="75" height="100" />  hanging around your code, believe me. So how to remedy this troublesome citizen? We have 2 options, &#8220;<a title="Partial" href="docs.python.org/library/functools.html" target="_blank">partial</a>&#8221; and &#8220;<a title="Lambda" href="http://docs.python.org/tutorial/controlflow.html" target="_blank">lambda</a>&#8220;. 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&#8217;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.</p>
<h4>using partial</h4>
<pre class="brush: python; title: ; notranslate">

from functools import partial

myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(partial(someMethod, arg_1, arg_2))

def someMethod(self, knight, phrase, *args):
    &quot;&quot;&quot;
    some comments
    &quot;&quot;&quot;
    print 'ni!'

</pre>
<h4>using lambda</h4>
<pre class="brush: python; title: ; notranslate">

myBtn = QtGui.QPushButton('Click Me')
myBtn.clicked.connect(lambda : someMethod('brave sir robin', &quot;I don't know that&quot;))

def someMethod(self, knight, phrase, *args):
    &quot;&quot;&quot;
    some comments
    &quot;&quot;&quot;
    print 'aaarrrggg!'

</pre>
<p>Here are some other links to get started using PyQt in Maya:<br />
<a title="Kristine Middlemiss video" href="http://area.autodesk.com/masterclasses/masterclass/class2_q4_2011" target="_blank">Kristine Middlemiss</a></p>
<p><a title="David Coleman video" href="http://area.autodesk.com/gdc2011/class3" target="_blank">David Coleman</a></p>
<p>There are more links in older posts as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.mattmurrayanimation.com/archives/269/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
