<?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>Xster.net &#187; Qt</title>
	<atom:link href="http://tech.xster.net/tag/qt/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.xster.net</link>
	<description>Never relearn twice</description>
	<lastBuildDate>Mon, 30 Aug 2010 00:30:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Deploy PyQt Applications on Mac OS X with PyInstaller!</title>
		<link>http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/</link>
		<comments>http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/#comments</comments>
		<pubDate>Wed, 28 Apr 2010 07:20:39 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[Deployment]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[py2app]]></category>
		<category><![CDATA[PyInstaller]]></category>
		<category><![CDATA[PyQt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Snow Leopard]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=218</guid>
		<description><![CDATA[The interweb seem to incline on py2app when it come to deploying applications on mac. I&#8217;ve tried to make a single deployable .app file for my application for a long time trying to follow these instructions from ars technica. I&#8217;m not a hacker and just want to produce a deployable usable application for others to [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.pyinstaller.org/"><img class="alignright size-medium wp-image-221" title="PyInstaller" src="http://tech.xster.net/wp-content/uploads/2010/04/logo-300x60.png" alt="" width="240" height="48" /></a>The interweb seem to incline on <a href="http://svn.pythonmac.org/py2app/py2app/trunk/doc/index.html">py2app</a> when it come to deploying applications on mac. I&#8217;ve tried to make a single deployable .app file for my application for a long time trying to follow <a href="http://arstechnica.com/open-source/guides/2009/03/how-to-deploying-pyqt-applications-on-windows-and-mac-os-x.ars/2">these instructions from ars technica</a>. I&#8217;m not a hacker and just want to produce a deployable usable application for others to use. And it seems py2app from MacPorts wasn&#8217;t able to surmount the Snow Leopard&#8217;s 64-bit compatibility issue.</p>
<p>And then, I was slacking off while studying for my final and out of nowhere I found <a href="http://www.pyinstaller.org/">PyInstaller</a>&#8216;s explicit support for PyQt and its recent support for the mac. And after trying, almost everything works out without much of a kink. Credit goes to <a href="http://diotavelli.net/PyQtWiki/PyInstallerOnMacOSX">ChrisWayg</a> who produced an amazingly complete and up-to-date set of instructions to follow. I&#8217;m merely telling how my application did using his instructions (April 2010) and hopefully doing my part to draw more attention to the excellent <a href="http://www.pyinstaller.org/">PyInstaller</a>.<span id="more-218"></span></p>
<h3>My Application</h3>
<p>A PyQt application to help photographers apply custom watermarks in a manual but in an assisted way.</p>
<p>Uses:</p>
<ul>
<li>PIL 1.1.7</li>
<li>Qt4 4.6.2</li>
<li>PyQt4 4.7.2</li>
<li>SIP 4.10.1</li>
</ul>
<p>All from MacPorts on Python 2.6.5, OSX 10.6.3.</p>
<h3>Prepare PyInstaller</h3>
<p>PyInstaller is kinda special in that it&#8217;s not installed anywhere. It&#8217;s just a bunch of Python scripts that work on stuff in place. While you&#8217;re at it, read the <a href="http://www.pyinstaller.org/export/latest/tags/1.4/doc/Manual.html">manual</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.pyinstaller.org<span style="color: #000000; font-weight: bold;">/</span>trunk ~<span style="color: #000000; font-weight: bold;">/</span>PyInstaller</pre></div></div>

<p>Or put it somewhere you want.</p>
<p>Then you need to build the bootloaders. If you&#8217;re making PyQt applications, you probably have XCode already. Anyway, you need it.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">cd</span> source<span style="color: #000000; font-weight: bold;">/</span>linux
python Make.py
<span style="color: #c20cb9; font-weight: bold;">make</span>
<span style="color: #7a0874; font-weight: bold;">cd</span> ..
python Configure.py</pre></div></div>

<h3>Making your Application</h3>
<p><a href="http://www.pyinstaller.org/ticket/158">So far</a>, PyInstaller has a problem of not showing your application at the front when you run it. You can fix it in your own code by adding</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">form.raise_<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>right after</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">form.show<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span></pre></div></div>

<p>with &#8220;form&#8221; being whatever you called your instance of MainWindow().</p>
<p>Like with py2app, you gotta first make a &#8220;spec file&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python Makespec.py <span style="color: #660033;">--onefile</span> <span style="color: #000000; font-weight: bold;">/</span>path<span style="color: #000000; font-weight: bold;">/</span>to<span style="color: #000000; font-weight: bold;">/</span>YourApplication.py</pre></div></div>

<p>This will make a YourApplication/YourApplication.spec file in your PyInstaller folder.<br />
To make it work on mac, add</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">sys</span>.<span style="color: #dc143c;">platform</span>.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;darwin&quot;</span><span style="color: black;">&#41;</span>:
    app = BUNDLE<span style="color: black;">&#40;</span>exe,
                 appname=<span style="color: #483d8b;">'WhateverYouWantToCallIt'</span>,
                 version=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span></pre></div></div>

<p>at the bottom.</p>
<p>For my application, it made 70 files inside the .app file on default. &#8211;onefile makes it more neat and 3 times smaller in size! but it does make it a bit slower. If you care to make it as fast as possible, leave out that option but you have to <a href="http://www.pyinstaller.org/ticket/155">fix a bug</a> yourself.</p>
<p>Finally, build the application!</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python Build.py YourApplication<span style="color: #000000; font-weight: bold;">/</span>YourApplication.spec</pre></div></div>

<p>This will make your application and leave a YourApplication.app in your PyInstaller folder.</p>
<h3>Fix the Application</h3>
<p>Put your own nice icon on the application. Copy your graphic, right-click your application and choose Get Info, click the icon and paste the graphic.</p>
<p>Open the content of your application, open Contents/Info.plist, uncheck &#8220;Application is background only&#8221; and save. If you&#8217;re using a plain text editor, change LSBackgroundOnly to false.</p>
<p>There is only one last problem to deal with on mac. You have to copy the qt_menu.nib file.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">locate</span> qt_menu.nib</pre></div></div>

<p>to find it. If you use MacPorts, it&#8217;s probably somewhere like /opt/local/libexec/qt4-mac/lib/QtGui.framework/Versions/4/Resources/qt_menu.nib</p>
<p>Copy it to your YourApplication.app/Contents/Resources</p>
<p>Boom, you can double click your application and it will run!</p>
<p>Ok, one last problem, I swear. When you run, it shows 2 icons on the dock&#8230; pretty ugly. <a href="http://www.pyinstaller.org/ticket/156">This hack</a> can fix it, but it&#8217;s not perfect as it leaves junk on your computer. Hopefully PyInstaller can address it soon.</p>
<h3>Conclusion</h3>
<p>The program is beautiful! You don&#8217;t have to worry about your Python plug-ins, your 64-bitness, anything (mostly). It just works. I have yet to figure out how to make 32-bit binaries on 64-bit systems and how to make upx work properly to reduce executable size. I&#8217;m currently running at 32mb for an application that really should be 1mb. But then again, it&#8217;s packaging a whole new GUI framework that doesn&#8217;t exist on the target computer with the application. Consider Skype (yes, it&#8217;s infinitely more complex than my thing), it is still 43mb.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-spaced">
<ul class="socials">
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;title=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;t=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21+-+http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-blogger">
			<a href="http://www.blogger.com/blog_this.pyra?t&amp;u=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;n=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21&amp;pli=1" rel="nofollow" class="external" title="Blog this on Blogger">Blog this on Blogger</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;title=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;title=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-hackernews">
			<a href="http://news.ycombinator.com/submitlink?u=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;t=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;title=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://slashdot.org/bookmark.pl?url=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;title=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/&amp;title=Deploy+PyQt+Applications+on+Mac+OS+X+with+PyInstaller%21&amp;summary=The%20interweb%20seem%20to%20incline%20on%20py2app%20when%20it%20come%20to%20deploying%20applications%20on%20mac.%20I%27ve%20tried%20to%20make%20a%20single%20deployable%20.app%20file%20for%20my%20application%20for%20a%20long%20time%20trying%20to%20follow%20these%20instructions%20from%20ars%20technica.%20I%27m%20not%20a%20hacker%20and%20just%20want%20to%20produce%20a%20deployable%20usable%20application%20f&amp;source=Xster.net" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyQt and Snow Leopard</title>
		<link>http://tech.xster.net/tips/pyqt-and-snow-leopard/</link>
		<comments>http://tech.xster.net/tips/pyqt-and-snow-leopard/#comments</comments>
		<pubDate>Sat, 14 Nov 2009 23:11:12 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[32]]></category>
		<category><![CDATA[64]]></category>
		<category><![CDATA[i386]]></category>
		<category><![CDATA[Mac]]></category>
		<category><![CDATA[PyQt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[SIP]]></category>
		<category><![CDATA[Snow Leopard]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=148</guid>
		<description><![CDATA[There are some 64-bit related issues when using PyQt and Snow Leopard. There&#8217;s the way to resolve it by reverting to 32-bits: Get the latest versions of PyQt and SIP. You need Qt installed of course. Configure SIP using python configure.py --arch i386 Configure PyQt using python configure.py --use-arch=i386 Finally, make sure your python is [...]]]></description>
			<content:encoded><![CDATA[<p>There are some 64-bit related issues when using PyQt and Snow Leopard. There&#8217;s the way to resolve it by reverting to 32-bits:</p>
<ol>
<li>Get the <a href="http://www.riverbankcomputing.co.uk/software/pyqt/download">latest versions</a> of PyQt and SIP. You need Qt installed of course.</li>
<li>Configure SIP using</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python configure.py <span style="color: #660033;">--arch</span> i386</pre></div></div>

<li>Configure PyQt using</li>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">python configure.py <span style="color: #660033;">--use-arch</span>=i386</pre></div></div>

<li>Finally, make sure your python is running in 32-bit mode because current Qt doesn&#8217;t support 64-bit mode. Add

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">VERSIONER_PYTHON_PREFER_32_BIT</span>=<span style="color: #c20cb9; font-weight: bold;">yes</span></pre></div></div>

<p> to your .bash_profile in your home directory</p>
</li>
<li>If your Python still refuses to run in 32 bit mode, try

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">arch</span> <span style="color: #660033;">-i386</span> python</pre></div></div>

</li>
</ol>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-spaced">
<ul class="socials">
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;title=PyQt+and+Snow+Leopard" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;t=PyQt+and+Snow+Leopard" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=PyQt+and+Snow+Leopard+-+http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-blogger">
			<a href="http://www.blogger.com/blog_this.pyra?t&amp;u=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;n=PyQt+and+Snow+Leopard&amp;pli=1" rel="nofollow" class="external" title="Blog this on Blogger">Blog this on Blogger</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;title=PyQt+and+Snow+Leopard" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;title=PyQt+and+Snow+Leopard" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-hackernews">
			<a href="http://news.ycombinator.com/submitlink?u=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;t=PyQt+and+Snow+Leopard" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;title=PyQt+and+Snow+Leopard" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://slashdot.org/bookmark.pl?url=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;title=PyQt+and+Snow+Leopard" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://tech.xster.net/tips/pyqt-and-snow-leopard/&amp;title=PyQt+and+Snow+Leopard&amp;summary=There%20are%20some%2064-bit%20related%20issues%20when%20using%20PyQt%20and%20Snow%20Leopard.%20There%27s%20the%20way%20to%20resolve%20it%20by%20reverting%20to%2032-bits%3A%0D%0A%0D%0AGet%20the%20latest%20versions%20of%20PyQt%20and%20SIP.%20You%20need%20Qt%20installed%20of%20course.%0D%0AConfigure%20SIP%20using%0D%0Apython%20configure.py%20--arch%20i386%0D%0AConfigure%20PyQt%20using%0D%0Apython%20configure.py%20&amp;source=Xster.net" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/pyqt-and-snow-leopard/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PyQt Drag Images into List Widget for Thumbnail List</title>
		<link>http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/</link>
		<comments>http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/#comments</comments>
		<pubDate>Sun, 03 May 2009 05:47:18 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[drag]]></category>
		<category><![CDATA[drag-and-drop]]></category>
		<category><![CDATA[drop]]></category>
		<category><![CDATA[explorer]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[image]]></category>
		<category><![CDATA[picture]]></category>
		<category><![CDATA[PIL]]></category>
		<category><![CDATA[PyQt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[QListWidget]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[thumbnail]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=61</guid>
		<description><![CDATA[This simple tutorial shows how you can create a program with Python and Qt to allow for image files from Explorer/Finder/Nautilus to be dropped in a list widget and create list items with thumbnails First we subclass a QListWidget to handle events class DragDropListWidget&#40;QListWidget&#41;: def __init__&#40;self, type, parent=None&#41;: super&#40;DragDropListWidget, self&#41;.__init__&#40;parent&#41; self.setAcceptDrops&#40;True&#41; self.setIconSize&#40;QSize&#40;72, 72&#41;&#41; &#160; def [...]]]></description>
			<content:encoded><![CDATA[<p>This simple tutorial shows how you can create a program with Python and Qt to allow for image files from Explorer/Finder/Nautilus to be dropped in a list widget and create list items with thumbnails</p>
<p style="text-align: center;"><img class="size-full wp-image-64  aligncenter" title="droppedthumbnails" src="http://tech.xster.net/wp-content/uploads/2009/05/picture-3.png" alt="droppedthumbnails" width="323" height="263" /></p>
<p>First we subclass a QListWidget to handle events<br />
<span id="more-61"></span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> DragDropListWidget<span style="color: black;">&#40;</span>QListWidget<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">type</span>, parent=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
		<span style="color: #008000;">super</span><span style="color: black;">&#40;</span>DragDropListWidget, <span style="color: #008000;">self</span><span style="color: black;">&#41;</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span>parent<span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">setAcceptDrops</span><span style="color: black;">&#40;</span><span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
		<span style="color: #008000;">self</span>.<span style="color: black;">setIconSize</span><span style="color: black;">&#40;</span>QSize<span style="color: black;">&#40;</span><span style="color: #ff4500;">72</span>, <span style="color: #ff4500;">72</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> dragEnterEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hasUrls</span>:
			event.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			event.<span style="color: black;">ignore</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> dragMoveEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hasUrls</span>:
			event.<span style="color: black;">setDropAction</span><span style="color: black;">&#40;</span>Qt.<span style="color: black;">CopyAction</span><span style="color: black;">&#41;</span>
			event.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			event.<span style="color: black;">ignore</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
	<span style="color: #ff7700;font-weight:bold;">def</span> dropEvent<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, event<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">hasUrls</span>:
			event.<span style="color: black;">setDropAction</span><span style="color: black;">&#40;</span>Qt.<span style="color: black;">CopyAction</span><span style="color: black;">&#41;</span>
			event.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
			l = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span>
			<span style="color: #ff7700;font-weight:bold;">for</span> url <span style="color: #ff7700;font-weight:bold;">in</span> event.<span style="color: black;">mimeData</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">urls</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
				l.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>url.<span style="color: black;">toLocalFile</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">emit</span><span style="color: black;">&#40;</span>SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;dropped&quot;</span><span style="color: black;">&#41;</span>, l<span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:
			event.<span style="color: black;">ignore</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The initialiser simply allows drops into the widget. It also makes sure thumbnails are well visible.</p>
<p>Then each event performs a check to make sure the object being dropped is indeed a file. Finally, at the drop, the class sends an event with a list of local files dropped. Of course all handling could be done within the class but I will eventually need to manage dropped files in a parent form so an event is used here.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">self</span>.<span style="color: black;">connect</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">pictureListWidget</span>, SIGNAL<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;dropped&quot;</span><span style="color: black;">&#41;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">pictureDropped</span><span style="color: black;">&#41;</span></pre></div></div>

<p>In the parent class, I first connect the signal I defined in the custom list widget.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> pictureDropped<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, l<span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">for</span> url <span style="color: #ff7700;font-weight:bold;">in</span> l:
		<span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">exists</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>:
			picture = Image.<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
			picture.<span style="color: black;">thumbnail</span><span style="color: black;">&#40;</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">72</span>, <span style="color: #ff4500;">72</span><span style="color: black;">&#41;</span>, Image.<span style="color: black;">ANTIALIAS</span><span style="color: black;">&#41;</span>
			icon = QIcon<span style="color: black;">&#40;</span>QPixmap.<span style="color: black;">fromImage</span><span style="color: black;">&#40;</span>ImageQt.<span style="color: black;">ImageQt</span><span style="color: black;">&#40;</span>picture<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
			item = QListWidgetItem<span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">basename</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">20</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">&quot;...&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">pictureListWidget</span><span style="color: black;">&#41;</span>
			item.<span style="color: black;">setStatusTip</span><span style="color: black;">&#40;</span>url<span style="color: black;">&#41;</span>
			item.<span style="color: black;">setIcon</span><span style="color: black;">&#40;</span>icon<span style="color: black;">&#41;</span></pre></div></div>

<p>Here the Image and ImageQt modules of PIL are used to process the images. The Image class can help to create a thumbnail of the opened image. ImageQt can then convert the Image class into an ImageQt class which is a subclass of QImage. QIcons can only be constructed from QPixmap so we build one from the ImageQt class and send it to make a QIcon.</p>
<p>There you go, you now have a working list widget which allow image files to be dropped and can list them with thumbnail.</p>
<p>Of course, this snippet doesn&#8217;t check for other file types being dropped. More code would take away the focus.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-spaced">
<ul class="socials">
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;title=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;t=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List+-+http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-blogger">
			<a href="http://www.blogger.com/blog_this.pyra?t&amp;u=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;n=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List&amp;pli=1" rel="nofollow" class="external" title="Blog this on Blogger">Blog this on Blogger</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;title=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;title=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-hackernews">
			<a href="http://news.ycombinator.com/submitlink?u=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;t=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;title=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://slashdot.org/bookmark.pl?url=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;title=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/&amp;title=PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List&amp;summary=This%20simple%20tutorial%20shows%20how%20you%20can%20create%20a%20program%20with%20Python%20and%20Qt%20to%20allow%20for%20image%20files%20from%20Explorer%2FFinder%2FNautilus%20to%20be%20dropped%20in%20a%20list%20widget%20and%20create%20list%20items%20with%20thumbnails%0D%0A%0D%0A%0D%0AFirst%20we%20subclass%20a%20QListWidget%20to%20handle%20events%0D%0A%0D%0Aclass%20DragDropListWidget%28QListWidget%29%3A%0D%0A%09def&amp;source=Xster.net" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Qt with C++ or Python Tutorial</title>
		<link>http://tech.xster.net/tips/qt-with-c-or-python-tutorial/</link>
		<comments>http://tech.xster.net/tips/qt-with-c-or-python-tutorial/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 23:18:31 +0000</pubDate>
		<dc:creator>xiao</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[Framework]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[PyQt]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Qt]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://tech.xster.net/?p=34</guid>
		<description><![CDATA[This is a collection of tutorials I found useful to get into the Qt framework. Being more used to the Microsoft standard, I have always wanted to branch into more cross-platform stuff so that I can at least write tools and programs for my Mac. I never really liked the idea of &#8220;reinventing the wheels&#8221; [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_35" class="wp-caption alignright" style="width: 310px"><img class="size-medium wp-image-35" title="Qt Creator" src="http://tech.xster.net/wp-content/uploads/2009/03/picture-1-300x183.png" alt="Qt Creator" width="300" height="183" /><p class="wp-caption-text">Qt Creator</p></div>
<p>This is a collection of tutorials I found useful to get into the Qt framework. Being more used to the Microsoft standard, I have always wanted to branch into more cross-platform stuff so that I can at least write tools and programs for my Mac. I never really liked the idea of &#8220;reinventing the wheels&#8221; feel of C++ unless it was on an embedded system so Python looked like a solid contender to the rather messy Perl.<span id="more-34"></span></p>
<p>In terms of interface, the choice is rather broad for Python but after trying a bit of Cocoa, TkInter, wxPython and GTK, nothing can quite give the clean and organized feel of Qt.</p>
<p>A solid start for Qt (which is written for C++) is always to go through the official 500+ pages book for Qt and C++. Luckily, the book&#8217;s available <a href="http://www.qtrac.eu/C++-GUI-Programming-with-Qt-4-1st-ed.zip" target="_blank">online</a> from the <a href="http://doc.trolltech.com/4.5/how-to-learn-qt.html">Qt website</a>. Too bad they&#8217;ve made it not very obvious to get to from the start page.</p>
<p>Then you can go through some more task-specific tutorials or get straight into PyQt:</p>
<ul>
<li><a href="http://doc.trolltech.com/4.5/designer-manual.html" target="_blank">http://doc.trolltech.com/4.5/designer-manual.html</a> a manual for the neat Qt Designer tool that is available from the Qt website. There&#8217;s the <a href="http://www.youtube.com/user/QtStudios" target="_blank">YouTube videos</a> that go with it as well.</li>
<li><a href="http://www.commandprompt.com/community/pyqt/book1" target="_blank">http://www.commandprompt.com/community/pyqt/book1</a> slightly dated but a very solid source with vast amounts of examples</li>
<li><a href="http://www.rkblog.rk.edu.pl/w/p/python/" target="_blank">http://www.rkblog.rk.edu.pl/w/p/python/</a> another nice set of tutorials that goes through scenarios with you</li>
<li><a href="http://vizzzion.org/?id=pyqt" target="_blank">http://vizzzion.org/?id=pyqt</a> a really simple 5 minute tutorial with Qt and Python</li>
<li><a href="http://www.zetcode.com/tutorials/pyqt4/" target="_blank">http://www.zetcode.com/tutorials/pyqt4/</a> short and sweet, although it could leave you wanting more</li>
<li>You can check out <a href="http://tech.xster.net/tips/pyqt-drag-images-into-list-widget-for-thumbnail-list/">my tutorial</a> on creating thumbnail list via drag-drop for a quick get shit done guide</li>
</ul>
<p>But nothing can compare with the wealth of information that&#8217;s available in the <a href="http://www.amazon.com/Programming-Python-Prentice-Software-Development/dp/0132354187/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1273714615&amp;sr=1-1">Rapid GUI Programming with Python and Qt book</a>. It is simply my best recommendation.</p>
<p>Here&#8217;s a <a href="http://tech.xster.net/tips/deploy-pyqt-applications-on-mac-os-x-with-pyinstaller/">tutorial</a> for deploying PyQt applications on Mac.</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-spaced">
<ul class="socials">
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;title=Qt+with+C%2B%2B+or+Python+Tutorial" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;t=Qt+with+C%2B%2B+or+Python+Tutorial" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Qt+with+C%2B%2B+or+Python+Tutorial+-+http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-blogger">
			<a href="http://www.blogger.com/blog_this.pyra?t&amp;u=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;n=Qt+with+C%2B%2B+or+Python+Tutorial&amp;pli=1" rel="nofollow" class="external" title="Blog this on Blogger">Blog this on Blogger</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;title=Qt+with+C%2B%2B+or+Python+Tutorial" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;title=Qt+with+C%2B%2B+or+Python+Tutorial" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-hackernews">
			<a href="http://news.ycombinator.com/submitlink?u=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;t=Qt+with+C%2B%2B+or+Python+Tutorial" rel="nofollow" class="external" title="Submit this to Hacker News">Submit this to Hacker News</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;title=Qt+with+C%2B%2B+or+Python+Tutorial" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-slashdot">
			<a href="http://slashdot.org/bookmark.pl?url=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;title=Qt+with+C%2B%2B+or+Python+Tutorial" rel="nofollow" class="external" title="Submit this to SlashDot">Submit this to SlashDot</a>
		</li>
		<li class="shr-googlebuzz">
			<a href="http://www.google.com/buzz/post?url=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;imageurl=" rel="nofollow" class="external" title="Post on Google Buzz">Post on Google Buzz</a>
		</li>
		<li class="shr-linkedin">
			<a href="http://www.linkedin.com/shareArticle?mini=true&amp;url=http://tech.xster.net/tips/qt-with-c-or-python-tutorial/&amp;title=Qt+with+C%2B%2B+or+Python+Tutorial&amp;summary=%0D%0A%0D%0AThis%20is%20a%20collection%20of%20tutorials%20I%20found%20useful%20to%20get%20into%20the%20Qt%20framework.%20Being%20more%20used%20to%20the%20Microsoft%20standard%2C%20I%20have%20always%20wanted%20to%20branch%20into%20more%20cross-platform%20stuff%20so%20that%20I%20can%20at%20least%20write%20tools%20and%20programs%20for%20my%20Mac.%20I%20never%20really%20liked%20the%20idea%20of%20%22reinventing%20the%20wh&amp;source=Xster.net" rel="nofollow" class="external" title="Share this on LinkedIn">Share this on LinkedIn</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://tech.xster.net/tips/qt-with-c-or-python-tutorial/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
