Shared by QT and VTK

Source: Internet
Author: User

 

Combined Development of QT and VTK

Recently, due to project requirements, I plan to use VTK for development.ProgramThe full name of VTK is visualizationtoolkit, which is a cross-platform C ++ library. It fully encapsulates OpenGL and stores, computes, and displays various 3D models, interaction and other content are all encapsulated in the form of classes and provide much more powerful functions than OpenGL. Unfortunately, the biggest drawback of VTK is that there is no free tutorial. It only provides free API manuals, which only lists the functions of each class, for reference books, it takes tens of dollars to purchase, so learning VTK can only rely on a large number of example.

Because of the need for a cross-platform development in the future (currently only in Windows), a cross-platform development library must be used. In the VTK \ examples \ GUI example, in Windows, there are only sdks, MFC, and C ++ builder. In addition, there are many options for motif, Python, and TCL, but motif is difficult to program, I have never touched on the performance and security of TCL and python as script languages. That is to say, none of them are usable... Speaking of cross-platform, due to the relationship between projects in the organization, I had a lot of contact with QT, so before VTK was selected, in fact, I plan to use the combination of QT + OpenGL to develop this software. In fact, if I do not consider cross-platform, I will still choose QT, because the event processing method is more convenient for me who are familiar with Delphi, can I combine VTK and QT with VTK now ..

As an experiment, I created the following small program, which is similar to VTK \ examples \ GUI \ Win32 \ samplemfc. The result proves my conjecture, QT and VTK can be easily combined. Let's create this program step by step with me:
1) load the latest version of VTK from http://www.vtk.org/. I use 4.2. This package only contains program files. You also need to compile it yourself. Therefore, you also need cmake to generate VC. DSW and. DSP files. Use VC to open and compile the generated project file. Finally, add the compiled directory VTK \ bin \ debug to the path in the Windows system environment.
2) download QT. I am currently using qt334. Open VC tools-> options-> directories and add the bin, include, and Lib paths of QT.
3) in VC, create a new Win32 console application project and create a main. cpp as the entry to the main function,CodeAs follows:
# Include <qapplication. h>

# Include "testwindow. H"

Int main (INT argc, char ** argv)
{
Qapplication app (argc, argv );

Testwindow testwin;
Testwin. Show ();
App. Connect (& App, signal (lastwindowclosed (), & App, slot (quit ()));
Return app.exe C ();
}

Testwindow. H is the header file of the Main Window testwindow of the program. This file will be created later.
This program is used to compile the qapplication, display testwindow, and enter the main program's message loop app.exe C ().

The testwindow class is implemented below, testwindow. h and testwindow. cpp are created respectively.
Testwindow. h:
# Include <qmainwindow. h>

Class testwindow: Public qmainwindow
{
Q_object

Public:
Testwindow ();
~ Testwindow ();
};

Testwindow. cpp:
# Include "testwindow. H"
# Include "moc_test1_1_h"

Testwindow: testwindow ()
{
}

Testwindow ::~ Testwindow ()
{
}

Have you noticed testwindow. # include "moc_test1_1_h" in CPP. This moc_test1_1_h will create the rtti information of testwindow and bind the message. It does not need to be implemented by ourselves, instead, it is created by a program named MOC in QT. In the FileView of VC, right-click testwindow. h, and select Settings. Then, in the displayed Project Settings dialog box, select the custom build page and enter the following in commands:
MOC-I testwindow. H-o moc_test1_1_h
The MOC program is called. Based on the Content of testwindow. H, a moc file named moc_test1_1_h is automatically generated.
In outputs, enter:
Moc_test1_1_h

Then, select projct in Project Settings and add the: qt-mt334.lib to the object/library modules on the Link Page.

Compile the program. If it succeeds, a simple QT program will be written, and a blank window will be displayed after it is started.

3) Add the VTK code. You can refer to the examples \ GUI \ Win32 \ samplemfc program to convert testwindow. H to the following:
# Include <qmainwindow. h>
# Include "vtkrenderer. H"
# Include "vtkwin32openglrenderwindow. H"
# Include "vtkwin32renderwindowindowinteractor. H"

Class testwindow: Public qmainwindow
{
Q_object

Public:
Testwindow ();
~ Testwindow ();

Protected:
Virtual void paintevent (qpaintevent *);
Virtual bool winevent (MSG *);

PRIVATE:
Vtkrenderer * Renderer;
Vtkwin32openglrenderwindow * renderwindow;
Vtkwin32renderwindowindowinteractor * interactor;
};

Testwindow. cpp is transformed into the following:
# Include "testwindow. H"
# Include "moc_test1_1_h"

# Include "vtkactor2d. H"
# Include "vtktextmapper. H"
# Include "vtktextproperty. H"
# Include "vtkdatasetreader. H"
# Include "vtkdatasetmapper. H"

# Include "vtkcommand. H"
# Include "vtkcamera. H"
# Include "vtkwin32renderwindowindowinteractor. H"
# Include "vtkinteractorstyletrackballcamera. H"

Testwindow: testwindow ()
{
This-> Renderer = vtkrenderer: New ();
This-> Renderer-> setbackground (0.3, 0.5, 0.1 );
This-> renderwindow = vtkwin32openglrenderwindow: New ();
This-> renderwindow-> addrenderer (this-> Renderer );
This-> interactor = vtkwin32renderwindowindowinteractor: New ();

Vtkactor2d * actor2d = vtkactor2d: New ();
Vtktextmapper * TXT = vtktextmapper: New ();
Actor2d-> setmapper (txt );
TXT-> setinput ("Hello World ");
TXT-> gettextproperty ()-> setfontsize (24 );
This-> Renderer-> addprop (actor2d );
TXT-> Delete ();
Actor2d-> Delete ();

Vtkactor * Actor = vtkactor: New ();
Vtkdatasetreader * reader = vtkdatasetreader: New ();
Reader-> setfilename ("weldedspheres. VTK ");
Vtkdatasetmapper * mapper = vtkdatasetmapper: New ();
Mapper-> setinput (Reader-> getoutput ());
Actor-> setmapper (Mapper );
This-> Renderer-> addprop (actor );
Mapper-> Delete ();
Reader-> Delete ();
Actor-> Delete ();
}

Testwindow ::~ Testwindow ()
{
If (this-> interactor ){
This-> interactor-> Delete ();
}
If (this-> Renderer ){
This-> Renderer-> setrenderwindow (null );
}
If (this-> renderwindow ){
This-> renderwindow-> Delete ();
}
If (this-> Renderer ){
This-> Renderer-> Delete ();
}
}

void testwindow: paintevent (qpaintevent * E)
{< br> If (! This-> interactor-> getinitialized () {
This-> renderwindow-> setwindowid (this-> winid ());
This-> renderwindow-> windowinitialize ();
This-> interactor-> setrenderwindow (this-> renderwindow );
This-> interactor-> initialize ();
}< br> This-> renderwindow-> render ();
}

Bool testwindow: winevent (MSG * MSG)
{
Switch (MSG-> message ){
Case wm_lbuttondown:
Case wm_lbuttonup:
Case wm_mbuttondown:
Case wm_mbuttonup:
Case wm_rbuttondown:
Case wm_rbuttonup:
Case wm_mousemove:
Case wm_char:
Case wm_timer:
If (this-> interactor-> getinitialized ()){
Vtkhandlemessage2 (MSG-> hwnd, MSG-> message, MSG-> lparam, MSG-> wparam, this-> interactor );
}
}
Return false;
}

A model file weldedspheres. VTK is used, which can be found in VTK. You may need to modify its path.

Then, open the proect Settings dialog box again. On the C/C ++ page, select category: Preprocessor and add:
D: \ VTK, D: \ VTK \ parallel, D: \ VTK \ hybrid, D: \ VTK \ patented, D: \ VTK \ rendering, D: \ VTK \ Io, d: \ VTK \ imaging, D: \ VTK \ graphics, D: \ VTK \ filtering, D: \ VTK \ common

Select the link page, select category: input, and add:
Vtkrendering. lib vtkgraphics. lib vtkimaging. lib vtkio. lib vtkfiltering. lib vtkcommon. lib vtkftgl. lib glu32.lib opengl32.lib glu32.lib opengl32.lib vtkfreetype. lib vtkpng. lib vtktiff. lib vtkzlib. lib vtkjpeg. lib vtkexpat. lib
Add the following to addtional library path:
D: \ VTK \ bin \ debug

All of the above assumes that VTK is installed on disk D. If you install it in another directory, modify the above path.

Now, re-compile the program and run it. You will see the VTK interface shown below.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.