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.

Saturday, December 10, 2011

cython odeint example

In response to the request by Ryan I now post a full example of using cython and odeint together. This is a specially written example because the original work contains too much code from my work that I do not want to post on the web. The equations used in the example have been chosen rather randomly.

In this example I have created a sample Model class which is a callable class (you can use an instance of the class as the function parameter for odeint). The Model class is defined in model_cython.pyx and contains all the Cython code. In ode_script.py the model function is integrated with odeint and thus shows how to call the Cython class from a normal python script. Setup.py is needed to compile model_cython.pyx (see this post).

model_cython.pyx:
import sys

#define some constants a c floats but still callable from python scripts
#not really used here but left in here as an example
cpdef float R_GAS,K_B
R_GAS=8.31      #Gas constant J/molK
K_B=1.38066e-23 #Boltzmann constant J/K

#import some often used math functions from the c math library
cdef extern from "math.h":
    double sin(double)

cdef extern from "math.h":
    double fabs(double)

cdef extern from "math.h":
    double exp(double)

#define a function callable from c and python
cpdef float Velocity(float t,float amplitude=1.0,float period=4.):
    cdef f1
    f1=fabs(sin(t*period))*amplitude
    return(f1)

class Model:
    """
    This class can be used to integrate a differential equation
    """
    def __init__(self,float a,float p):
        """
        -a is the amplitude of the sine function that describes the 
            speed oscillation as a function time
        -p controls the period of the sine function
        """
        self.a=a
        self.p=p
        
    
    def __call__(self,float y,float t):
        """
        y is the current state of the initial value problem:
        y: position
        t: time
        """
        cdef float pos
        pos=y
        #Workhardening
        v=Velocity(t,amplitude=self.a,period=self.p)*exp(-pos)
        return(v)

ode_script.py:
#!/bin/env/python

from numpy import r_
import pylab,scipy.integrate,sys
sys.path.append(".")
from model_cython import *


#ampltiude and period
amp=1.0
period=4.0
#set initial position
pos0=0.0
#create instance of Model object with speed amplitude of 1
myModel=Model(amp,period)
#create time vector (4 seconds in 500 steps)
tvec=r_[0.0:4.0:500j]
#integrate ov
y=(scipy.integrate.odeint(myModel,pos0,tvec))
    

#plot results
pylab.figure(1)
pylab.clf()
pylab.subplot(211)
pylab.plot(tvec,y)
pylab.xlabel("Time (s)")
pylab.ylabel("Position (m)")
pylab.grid(b=True)
pylab.subplot(212)
v_list=[]
for t in tvec:
    v_list.append(Velocity(t,amplitude=amp,period=period))
pylab.plot(tvec,v_list)
pylab.xlabel("Time (s)")
pylab.ylabel("Undamped velocity (m/s)")

pylab.grid(b=True)
pylab.show()

setup.py:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext

ext_modules = [Extension("model_cython",["model_cython.pyx"],\
    libraries=["m"])]

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

Hope this helps.

Sunday, November 13, 2011

Ubuntu 11 samba mounting problems during booting

After upgrading to Ubuntu 11 I started to receive errors during booting mentioning CIFS-VFS. A quick search on the internet did not reveal a simple solution. Now I had a more thorough look. The CIFS-VFS error apparently has something to do with the automatic mounting of a samba share on a server in my network. I now know why the error pops up twice. From posts on the web I found that the most likely reason that I get this error is because the network is not up before the mounting is tried. Which makes sense when you get the error

mount error(101): Network is unreachable

(look in /var/log/boot.log)


Ubuntu runs the boot scripts in /etc/init. Here I found mountall.conf and mountall-net.conf. Here I found that the order of the scripts is determined by a line in the scripts in the form

start on runlevel [2345] stop on runlevel [!2345]

When I looked in /etc/init/mountall-net I found the line:

start on net-device-up

which suggests that the script waits for the network to be up before the script is run. The other script mountall.conf has the line "start on startup" in it which I think means that this script is called more or less at the beginning when the network indeed may not yet be up. When the network is up (or supposed to be up) mountall-net is called. This explains why I get the mount error twice during booting. While searching the web for "start on net-device-up" I somewhere found the variant


start on (net-device-up IFACE=eth0):

And this appears to make the problem go away (so now I only have one mnt error left during booting (from mountall.conf) but my samba share does get mounted automatically during booting).

Friday, October 14, 2011

working with openmp and gcc vectorization output

When working with openmp for parallelisation you can set the number of threads in a bash shell (cygwin) by

export OMP_NUM_THREADS=4

this can be good to do if you do not want to use all "cores" detected automatically on a cpu with hyperthreading.

To see which loops have been automatically vectorized by gcc you have to add the compiler switch

-ftree-vectorizer-verbose=5

Other verbose levels can also be used but for me level 5 seems to give the most relevant information.