Showing posts with label pyqt. Show all posts
Showing posts with label pyqt. Show all posts

Oct 6, 2009

How to package the PyQt with VTK application

1. Download py2exe from http://sourceforge.net/projects/py2exe/files/
(Download the version that corresponds with your python version.)
2. Follow instructions from http://www.py2exe.org/index.cgi/Tutorial

Basically, you need a file called setup.py, like this:
from distutils.core import setup
import py2exe
setup(console=['runui12.py'])

Then run this line in command line:
vtkpython setup.py py2exe

Extra Tags: How to make executable run standalone pyqt qt vtk app application

Sep 3, 2009

QStrings and Strings

In Qt, QFileDialog can be used to open up a filedialog window. QFileDialog's static methods allows the GUI to capture the directory/file name that the user chooses. From that, a QString is returned.

My problem was that I wanted to find a certain string in the filename, but it was a QString, and not a string. I found that...

to convert a qstring to string, use the str function.

stringB = str(stringA)

In this situation, stringB will be the string version of stringA, which is of type QString. By the way, str works on integers as well.

PyQt Documentation on Qfiledialog

Sep 1, 2009

Embedding vtk into a pyqt GUI

Now that vtk is installed with python bindings, all you need to do is to let your GUI find these bindings, then import them into your GUI. Let's jump right into the code. I have skipped some bits that are general for vtk, and have mainly included bits that are directly relevant to the QVTKRenderWindowInteractor.

import sys
// Now append the path of the python wrapping, which comes in the source code.
// Alter the path according to your own path.
sys.path.append("C:\Qt\vtk-5.4.2\Wrapping\Python")
//Import the vtk module
import vtk

class StartMain(QtGui.QMainWindow):
# no parent, i.e. is top level widget
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_main()
self.ui.setupUi(self)
self.ui.meshDisplayWidget = QVTKRenderWindowInteractor(self.ui.meshDisplay) self.ui.meshDisplayWidget.Initialize()

//Set actors and mappers, then instead of creating a renderwindowinteractor,
// use the self.ui.meshDisplayWidget to display the mesh. Also define picker, and
// set data source (for code about displaying a mesh from coordinates, as
// an unstructured grid.
ren = vtk.vtkRenderer()
ren.AddActor(aMeshActor)
ren.AddActor2D(textActor)
ren.AddActor(dotActor)

ren.ResetCamera()
cam1 = ren.GetActiveCamera()
self.ui.meshDisplayWidget.GetRenderWindow().AddRenderer(ren)
self.ui.meshDisplayWidget.SetPicker(picker_point)
self.ui.meshDisplayWidget.show()


Jul 26, 2009

How to Install VTK for use with Python

I am installing VTK, which I will need to incorporate 3D visualisation in my GUI. I am designing my GUI in Qt, and programming with Python. (Hence, PyQt). To make VTK work with Python, you need to install from binaries, instead of downloading with the windows installer. Feel free to make comments, or ask questions. (I'm not an expert. I am actually a beginner, but I want to document my problems and solutions so that others can be benefited. So feel free to talk here, as it might help others too.)

Download and install:
Cmake(
http://www.cmake.org/cmake/resources/software.html)
Python(
http://www.python.org/download/releases/)

Download and unzip into a file (we will call this the source file):
VTK(http://www.vtk.org/VTK/resources/software.html): Source: vtk-5.4.2.zip (Don't use the windows installer, because then you can't use Python.)

Then follow these steps:

1. Open the Cmake 2.6 GUI, and type in the path for the source file, and also where you would like to store the binary files.
Source: C:/Qt/vtk-5.4.2 (I have unzipped the source file into this directory called vtk-5.4.2, which is in my 'Qt' folder. Type in the path for your source file.)
Binary: C:/Qt/VTKbin7 (This is an empty folder, into which Cmake will put binaries.)
>Configure
>Choose Visual Studio 8 2005 as generator
>Turn on these (in the red lines): BUILD_SHARED_LIBS and VTK_WRAP_PYTHON
>Configure
>Generate

2. Open Visual Studio;
Open File;
Find the folder where Cmake has put your binaries. E.g. C:/Qt/VTKbin7
Open VTK.sln

3. >Build >Configuration Manager >Active solution configuration
>Choose ‘Release’
>Ok

4. >Build
>Build Solution
This will take more than an hour.

5. Now you should be able to find ‘vtkpython.exe’ in your bin/release file. E.g. C:/Qt/VTKbin7/bin/release


Now go to Embedding VTK into a PyQt GUI

Jun 3, 2009

Python (PyQt)

Am working with PyQt for my 4th year project, developing a UI for a software at the Dept. Engineering Science.

Situation: I wanted to open a file for reading, then write at a certain place in the file.

Problem: Couldn't read and write even with
f = open('myFilename.txt', 'r+')

Solution: After reading, can't simply do f.write.
Instead,
f.seek()
then,
f.write('My writing')