<?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; drag</title>
	<atom:link href="http://tech.xster.net/tag/drag/feed/" rel="self" type="application/rss+xml" />
	<link>http://tech.xster.net</link>
	<description>Never relearn twice</description>
	<lastBuildDate>Fri, 20 Jan 2012 21:11:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<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[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><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-publisher-61"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><div class='shareaholic-like-buttonset' style='float:none;height:30px;'><a class='shareaholic-googleplusone' data-shr_size='medium' data-shr_count='true' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpyqt-drag-images-into-list-widget-for-thumbnail-list%2F' data-shr_title='PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List'></a><a class='shareaholic-tweetbutton' data-shr_count='none' data-shr_href='http%3A%2F%2Ftech.xster.net%2Ftips%2Fpyqt-drag-images-into-list-widget-for-thumbnail-list%2F' data-shr_title='PyQt+Drag+Images+into+List+Widget+for+Thumbnail+List'></a></div><div style="clear: both; min-height: 1px; height: 3px; width: 100%;"></div><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></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>
	</channel>
</rss>

