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):

 self.partDone.emit(float(a)/35.0*100)
 print 'sleep', a
 time.sleep(0.13)

 self.procDone.emit(True)   
 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()
 self.progressbar.setMinimum(1)
 self.progressbar.setMaximum(100)

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

 self.setLayout(mainLayout)
 self.setWindowTitle("Processing")

 self.thread.partDone.connect(self.updatePBar)
 self.thread.procDone.connect(self.fin)

 self.thread.start()

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

 def fin(self):
 sys.exit()
 ##self.hide()

if __name__ == '__main__':

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

 pbarwin = AddProgresWin()
 pbarwin.show()

 sys.exit(app.exec_()

2 Comments

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:

c:\python26

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

sys.version

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

sys.path

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

/Library/Frameworks/Python.framework/Versions/2.6

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

sys.executable

Should return

'/Library/Frameworks/Python.framework/Versions/2.6/Resources/Python.app/Contents/MacOS/Python'

This is what Python you are using. Type:

sys.path

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

'/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages'

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

PYTHONHOME="/Library/Frameworks/Python.framework/Versions/2.6/"
export PYTHONHOME

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;
Py_Initialize();

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

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

Py_Finalize();

return 0;
 };

Compile it by Makefile:

BOOST_INC=/Library/boost_1_44_0/
BOOST_LIB=/Library/boost_1_44_0/stage/lib
PYTHON_INC=/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6

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

Still need the part1.py file to call from the test program, I just use the part1.py 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:
if(in_img.isNull())
{
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;

if(in_img.hasAlphaChannel())
cout<<”  alpha channel: yes”<<endl;
else
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:

out_img.save(“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