Ozon Yağı | Medikal Ozon Yağı | HGF Link Değişimi

PyQt and drag’n'drop

Today I was trying to get drag and drop working in PyQt and I had the worst time getting it to work. I was trying to have a QListWidget accept file drops which would then add the path to the file to the list. To enable dropping on a widget you must do several things. First of all you must call:

setAcceptDrops(True)

Then you have to implement the following methods:

dragEnterEvent(self, event)
dropEvent(self, event)

What I forgot to do (when I read the docs carefully) is that you also need to implement

dragMoveEvent(self, event)

Which of course makes sense when you think about it. First the cursor enters the widget, then moves within it, then you drop. Simple, right?

And since I couldn’t find an example of this behavior when I was googling (drop file on list, list adds file path) I threw together this example. This example can also reorder the list after you have dragged files on it.

import sys
from PyQt4.QtGui import *
from PyQt4.QtCore import *
 
class MyListWidget(QListWidget):
  def __init__(self, parent):
    super(MyListWidget, self).__init__(parent)
    self.setAcceptDrops(True)
    self.setDragDropMode(QAbstractItemView.InternalMove)
 
  def dragEnterEvent(self, event):
    if event.mimeData().hasUrls():
      event.acceptProposedAction()
    else:
      super(MyListWidget, self).dragEnterEvent(event)
 
  def dragMoveEvent(self, event):
    super(MyListWidget, self).dragMoveEvent(event)
 
  def dropEvent(self, event):
    if event.mimeData().hasUrls():
      for url in event.mimeData().urls():
        self.addItem(url.path())
      event.acceptProposedAction()
    else:
      super(MyListWidget,self).dropEvent(event)
 
class MyWindow(QWidget):
  def __init__(self):
    super(MyWindow,self).__init__()
    self.setGeometry(100,100,300,400)
    self.setWindowTitle("Filenames")
 
    self.list = MyListWidget(self)
    layout = QVBoxLayout(self)
    layout.addWidget(self.list)
 
    self.setLayout(layout)
 
if __name__ == '__main__':
 
  app = QApplication(sys.argv)
  app.setStyle("plastique")
 
  window = MyWindow()
  window.show()
 
  sys.exit(app.exec_())

Batch adventures (or: expanding variables in batch files)

I was looking into writing a fairly simple batch file to install some stuff and update environment variables. At some point I wanted to do something like this (constructing a string of ‘;’ separated paths):

set env_vars = \path\to\stuff
if x%user%==xsveinbjorn (
set env_vars = %env_vars%;\another\path
)
if x%hostname%==xgefjun (
set env_vars = %env_vars%;\whoah\this\is\radical
)
echo env_vars

When I would run this I would always get

\path\to\stuff;\whoah\this\is\radical

(Notice that the second path \another\path is missing from the result)
Being very new to writing batch scripts I found this extremely confusing since this sort of stuff would work like a charm in python. So I started the google machine.

The reason this happens is that variables enclosed in ‘%’ are expanded when the script is read, which is quite different from the way python handles this resolving stuff at the last possible time. So when the script is read it will actually look something like this:

set env_vars = \path\to\stuff
if xsveinbjorn==xsveinbjorn (
set env_vars = \path\to\stuff;\another\path
)
if xgefjun==xgefjun (
set env_vars = \path\to\stuff;\whoah\this\is\radical
)
echo env_vars

(I resolved the variables %user% to ‘sveinbjorn’ and %hostname% to ‘gefjun’)
And we can clearly see why we got the result that we got.

So what we have to do is tell the batch file to expand the variables as we go along (something close to how python does things) instead of this expand-it-when-you-read-it nonsense.

To do this we simply have to do three things:

  1. Add this line to the top of the code
    setlocal EnableDelayedExpansion
  2. Add this to the end of it
    endlocal
  3. Replace the enclosing ‘%’ to ‘!’ for the variable you want to delay the expansion for.
    if x%user%==xsveinbjorn (
    set env_vars = !env_vars!;\another\path
    )
    

So my original example would look like this:

setlocal EnableDelayedExpansion
set env_vars = \path\to\stuff
if xsveinbjorn==xsveinbjorn (
set env_vars = \path\to\stuff;\another\path
)
if xgefjun==xgefjun (
set env_vars = \path\to\stuff;\whoah\this\is\radical
)
echo env_vars
endlocal

Now we get the result we were looking for:

\path\to\stuff;\another\path;\whoah\this\is\radical

This also applies to loops and other places where the variable might change between the time you start the batch script and when you actually want to expand it.

subprocess.Popen and the env argument

Today I was looking at launching programs with some custom environment variables set and if you don’t want to redefine a whole lot of them for this new environment I’d recommend is copying the os.environ dictionary and do your changes on the copy, like so:

new_env = os.environ.copy()
new_env['MEGAVARIABLE'] = 'MEGAVALUE'
subprocess.Popen('path', env=new_env)

Then you should have your program launched with all them fancy environment variables set.

What I learned today #5: Quicktime COM object’s volume range.

When setting the volume of a quicktime player through COM please keep in mind that the range is from 0-1 NOT 0-100 as I thought (since there doesn’t seem to be any good documentation about the QT COM interface). QT allows you to set the volume to 100 and you end up with painfully loud sound. So set your volume to 1 to allow people to enjoy their hearing for a new day.

What I learned today #4:Center Current Frame

When using Maya’s Dope Sheet and Graph Editor you sometimes want to quickly center the editors on the current active frame. You can do this by going ‘View’>'Center Current Frame’ – or – you can add these two commands to your hotkey editor and map that functionality to some spiffy hotkeys:

For the Graph Editor:
animCurveEditor -edit -lookAt currentTime graphEditor1GraphEd;

For the Dope Sheet:
dopeSheetEditor -edit -lookAt currentTime dopeSheetPanel1DopeSheetEd;

For those who like this sort of stuff there is Cameron Fielding’s ‘Tap your timing’ – tip

What I learned today #3: Wrapping your paths in quotes!

Always always always remember wrapping your paths in quotes. I keep running into stuff I wrote break because someone saved a file with a space in it and I forgot to wrap the path in quotes.
This works:
blackwave.exe -d 50 superwave.wav
This doesn’t:
blackwave.exe -d 50 super 2.wav (two file arguments, ‘super’ and ’2.wav’)
To fix this:
blackwave.exe -d 50 "super 2.wav"

So remember: whenever you are using path rembemer the
"\""+ variable + "\""
(or something else that suits your language)

What I learned today #2: Various definitions of TEMP

In Softimage on Windows, if you fetch the TEMP environment variable e.g. from witihn python (with the os.environ dictionary), you will get a different directory when you get the TEMP variable from outside Softimage.
Outside Softimage you get:
C:\Users\<username>\some\path\I\cannot\rembemer
within it you get:
C:\Users\<username>\some\path\I\cannot\rembemer\XSI_temp_###

Just in case you wanted to write files to TEMP and read from them later.

What I learned today #1: profiling helps you make things run faster

After seeing an excellent post (as usual) on Hamish’s site about a profiling utility he wrote I decided to give it a try today, and man is this thing useful or what. It helped me find bottlenecks in two of my scripts today and I can only imagine how much of the user’s time it will save.

©2007, Sveinbjörn J. Tryggvason | RSS | Comments-RSS

Seo | Böcek İlaçlama | Böcek İlaçlama | İlaçlama | Böcek İlaçlama | Böcek İlaçlama | Böcek İlaçlama | prefabrik ofis | prefabrik