Monthly Archives: January 2011

PyQt ProgressBar Example

This is a simple PyQt performing a simple progress bar, using thread.

import time
from PyQt4 import QtCore, QtGui

class SleepProgress(QtCore.QThread):
 procDone = QtCore.pyqtSignal(bool)
 partDone = QtCore.pyqtSignal(int)
 def run(self):
 print 'proc started'
 for a in range(1, 1+35):

 print 'sleep', a

 print 'proc ended'

class AddProgresWin(QtGui.QWidget):
 def __init__(self, parent=None):
 super(AddProgresWin, self).__init__(parent)

 self.thread = SleepProgress()

 self.nameLabel = QtGui.QLabel("0.0%")
 self.nameLine = QtGui.QLineEdit()

 self.progressbar = QtGui.QProgressBar()

 mainLayout = QtGui.QGridLayout()
 mainLayout.addWidget(self.progressbar, 0, 0)
 mainLayout.addWidget(self.nameLabel, 0, 1)




 def updatePBar(self, val):
 perct = "{0}%".format(val)

 def fin(self):

if __name__ == '__main__':

 import sys
 app = QtGui.QApplication(sys.path)

 pbarwin = AddProgresWin()



Filed under Python

Choose Python Inside Maya

If no PYTHONHOME is set, run below inside Maya:

import sys
print sys.prefix

returns /Python dir inside Maya install location.

Now let PYTHONHOME=”C:\\python26″, where stand-along python is installed. Run the same lines in Maya, we get:


So we explicitly choose the version of Python to work inside Maya. But there is something confusing about this Python. If you run


It will appear to be different from the version running outside Maya. Run


to check module search path. It will start will Maya /bin dir. While Python outside starts searching modules in C:\\windows\\system32

Another issue is Maya still need Python to load Maya modules. There is a /maya dir inside Maya\\Python\\Lib\\site-packages, copy it into C:\\python26\\Lib\\site-packages, so Python can find the modules.

Leave a comment

Filed under Python

Boost Python Embedded Example

The reason to call Python inside C++ program is I want to use PyQt for UI stuff. This document is intended to show how to build a minimal example to embed Python in C++ using Boost Python, on Max OS X.

To make this working, one should install a few programs in the system, including: Python, Qt, PyQt, and Boost.

For Leopard, the built-in Python is 2.5, which is too old to work with PyQt. Need update to 2.6. I would prefer to  download the tar ball of Python source and build myself:

$ ./configure --enable-framework

$ make

$ make install

When framework is enabled, Python will be built into


Now you have multiple versions of Python in the system, probably you are still using the old one. To make sure it is up-to-date, start Python, type:

import sys


Should return


This is what Python you are using. Type:


Should return a string array about where the Python is searching for modules, should include:


If you are getting ‘/usr/local/’ stuff, means you are still calling the old Python. To work round this, add environment value


to explicitly choose the version just installed. Now one can build PyQt and Boost.

Create the source file Main.cpp:

#include <boost/python.hpp>
#include <iostream>
using namespace std;
using namespace boost::python;

int main()
 cout<<"hello boost python embedded "<<endl;

cout<<"version "<<Py_GetVersion()<<endl;

PyRun_SimpleString("from time import time,ctime\n"
 "print 'Today is',ctime(time())\n");
FILE *file_1 = fopen("./","r+");


return 0;

Compile it by Makefile:


 g++ -o test main.cpp -I$(PYTHON_INC) -I$(BOOST_INC) -L$(BOOST_LIB) -lboost_python -framework Python

Still need the file to call from the test program, I just use the form PyQt tutorial examples. But for some reason

app = QtGui.QApplication(sys.argv)

doesn’t work. I have to change it to

app = QtGui.QApplication(sys.path)

If everything works out, the program is compiled, run it, a little window will pop up:

Well done! It is the first time you call Python inside a C++ program.

Leave a comment

Filed under Uncategorized

QImage Basic

QImage is the Qt class for image I/O. It conveniently supports a few usual image formats. The following is about some basic usage of QImage.

First include the header:

#include <QtGui/QImage.h>

To compile it on Mac, better use -framework flag like:

g++ -o test main.cpp -framework QtCore -framework QtGui

QtCore.framework and QtGui.framework should be found in /Library/Frameworks/

To load an image:

QImage in_img( “foo.tif” );

If it failed to open the image, result will be null:
cout<<“failed to load image, do nothing”<<endl;
return 0;
A few properties about the image:

cout<<”  size: “<<in_img.width()<<” x “<<in_img.height()<<endl;
cout<<”  depth: “<<in_img.depth()<<” bits-per-pixel”<<endl;

Any 8-bit Tiff image with alpha channel should return format as ARGB32:
if(in_img.format() == QImage::Format_ARGB32)
cout<<”  format: ARGB32″<<endl;

cout<<”  alpha channel: yes”<<endl;
cout<<”  alpha channel: no”<<endl;

Get the pixels from the image:

const uchar *imgbits = in_img.bits();

Because it is unsigned char, I think QImage only supports 8-bit images.

Create a RGB image from pixels:

uchar* image_data = new uchar[img_w*img_h*3];

// set the pixel colors

QImage out_img(image_data, img_w, img_h, QImage::Format_RGB888);

And save it in Jpeg:“foo.jpg”);

There should be enough bits in correct order. For RGB888 format, 3 char for each pixel as RED|GREEN|BLUE. For ARGB32, 4 char as BLUE|GREEN|RED|ALPHA.

Leave a comment

Filed under Qt