Sunday, December 9, 2012

Windows application icon in Qt Creator

In an earlier post I described how to add icons to a Qt application using Qt Creator. With the procedure described there, the application shows the icon in the window title bar and on the taskbar. However, when you browse to the folder containing the application in windows, the executable will not be shown with the icon. The same is true when you create a shortcut to the executable (windows will also say that the executable does not contain an icon).

As described here, there is a simple windows specific method to add an application icon that will change the appearance of the executable when browsing under windows (and when creating a shortcut to the executable). You must first create a windows specific resource file which has the extension .rc e.g. myapp.rc. This simple text file then needs to contain the (single) line:

IDI_ICON1 ICON DISCARDABLE "images/myapp.ico"

The final step is then to add this resource file in the Qt project (.pro) file:

RC_FILE = myapp.rc

This works for me when I put the myapp.rc in the same folder as where the Qt project file is located (and that contains the images folder). I did not try any other file locations.

Sunday, October 28, 2012

pythonxy cython mingw problems

Again I was having issues using a cython compiled package with pythonxy. For some reason when running from a ipython terminal started from pythonxy, I would get a DLL load error. If I would run the python/cython program directly (i.e. from a dos box: python my_program.py) then it would run fine. By default if you compile a program with mingw you need a dll with the std c or c++ library (typically this dll has a difficult name as libgcc_s_dw2-1.dll). As I have mingw installed in more than one location (32bit, 64bit, QtSDK, Python(x,y)) I think it cannot find the right dll (although I could not detect which dll it was using). The problem can be most easily solved if you link the std library statically. This you can do by simply adding "stdc++" to your libraries field in setup.py.

So my new setup.py looks now something like

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy

ext_modules = [Extension("BvL_cython",["model_cython.pyx"],\
    libraries=["m","stdc++"],include_dirs=[numpy.get_include()])]

setup(
    name= 'Model class',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

(This also shows how to automatically detect the location of the numpy include directories)

Saturday, October 13, 2012

Color text output from python

At the moment I run a minimization routine build in Cython/Python that outputs a lot of text to the terminal. Not all text is equally interesting and I was wondering if it is difficult to output some of the more important messages in a different color. It turns out that this is very easy. On stackoverflow Dietrich Epp posted a nice example which I have changed a little to select the color with a single character string:

def hilite(string, status, bold):
    attr = []
    if(sys.stdout.isatty()):
        if status=='g':
            # green
            attr.append('32')
        elif status=='r':
            # red
            attr.append('31')
        elif status=='y':
            # yellow
            attr.append('33')    
        elif status=='b':
            # blue
            attr.append('34')
        elif status=='m':
            # magenta
            attr.append('35')
        if bold:
            attr.append('1')

        return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)
    else:
        return(string)

More info on ANSI escape codes is given on Wikipedia. You can also do things like underline. So far I have only tested this on Linux so I do not know if it also works on Windows.

Update:
This does not work on Windows (windows terminals do not work with ANSI codes). Luckily there is a python package you can use to make it work on windows after all: colorama. Simply call colorama.init()
(after import colorama) and you can use the above code again. However, I noticed that if you use ipython this garbles your prompt (and other things). To work around this I call colorama.init() in the functions that use the ANSI codes in the output. Then when the script is finished you have to call colorama.deinit() and ipython will behave normally.

Sunday, June 10, 2012

header file location for numpy arrays (cython)

Today was the first time I tried working with numpy arrays in Cython. I got a compilation error saying that the header file for numpy could not be found. Here I found an easy fix for this problem. The setup.py script can be changed to automatically detect and add the numpy include path:


from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy

ext_modules = [Extension("xx_cython",["xx_cython.pyx"],\
    libraries=["m"],include_dirs=[numpy.get_include()])]

setup(
    name= 'xx model class',
    cmdclass = {'build_ext': build_ext},
    ext_modules = ext_modules
)

Friday, March 2, 2012

adding icons to an application with Qt Creator

The book "C++ GUI Programming with Qt 4" gives a short description of how to use a resource file to include images into the executable which can then (for example) be used as icons. The example given is clear by itself but when using the latest QtCreator version (2.4.1) things turn out to work just a little bit different. Furthermore a resource file as given in the book does not load correctly in QtCreator.

To add image resources I now do this:
1. Create a new resource file by clicking File -> New File or Project and then Choose Qt resource file.
2. Add a prefix for example /icons
3. Add your images files for example: images/icon.png (with a images a subdirectory in your project folder
4. Refer to these resources in your code as ":/icons/images/icon.png"

The changed point compared to the resource file example in the book is the prefix. You now have to add a prefix which (probably) did not exist in the past. In the example application (called "application") that comes with QtCreator the prefix that is used is simply "/".

Adding the icon to a widget (e.g. your main window) is now simple:

setWindowIcon(QIcon(":/icons/images/icon.png"));

And as requested some screen shots.

Step 1, creating a new resource file:


Step 2, add a prefix:


Simply type over the default prefix label generator by QtCreator. Your prefix will appear in the main window:



Step 3, add image files (after selecting the prefix in the main window):


The final result then looks something like this:

Sunday, February 5, 2012

SPE versions

If you use SPE version 0.8.4 instead of version 0.8.3 none of the earlier mentioned problems occur. It is a bit strange that if you go to the sourceforge download site for SPE you get 0.8.3 as the recommended version. If you download from the archive link on the SPE blog you can download version 0.8.4.

Saturday, February 4, 2012

more SPE problems

After posting how to get SPE running on python(x,y) and openSUSE I wanted to continue working on the python script I was until now editing with SciTE. When I actually wanted to edit my python file in SPE it did not accept any keyboard input in the main editor window (it gives errors in _spe\sm\wxp\stc.py). Rather inconvenient for an editor. On the internet I found one posting of the same problem. Unfortunately this time without a solution. By chance I found this website which gave me the idea to simply replace the two calls to event.KeyCode() in the stc.py file with event.GetKeyCode(). Now everything seems to work (both on openSUSE and python(x,y)).

spe python(x,y) wxPython2.8 problems

On Ubuntu the nice python editor from Stani (SPE) has started to behave strange since a recent Ubuntu upgrade. When scrolling with the wheel on the mouse lines get stuck. Moves with the cursor keys correct this but it is really annoying. Therefore I now wanted to run SPE on windows with python(x,y). This proved to be a bit more troublesome than expected. After putting the _spe directory obtained from the SPE download on sourcefourge in the site-packages directory of python (\Python27\Lib\site-packages) simply running the SPE.py script did not work. After running as python SPE.py --debug from a command prompt I had the same problem as reported here (an error in Throbber.__init__). Luckily the solution reported on that same site also fixed the problem:

Moving initialization of _filename, _running, and _position to before the call to GIFAnimationCtrl.__init__ resolves the problem.

Interestingly on openSUSE I experienced the same problems. But there I also had to edit smdi.py (_spe\sm\wxp\smdi.py). In the function setTitle of the class Child(Framework) I commented the calls to self.SetTitle (lines 988-991). Calling self.setTitle instead of self.SetTitle (mark the capital) does not fix the problem (you get a maximum recursion limit error instead). The commenting of the code does not seem to affect the execution of SPE. The only remaining problem on openSUSE with SPE is now that I can only run it as root.