The combination of QT and VTK

xiaoxiao2021-03-05  20

Recently, due to the needs of the project, I plan to use VTK to develop programs, VTK full name is VisualizationToolkit, it is a set of cross-platform C libraries, which makes OpenGL more comprehensive package, and stores various three-dimensional model storage, operation, Displaying, interaction, etc. is all encapsulated in a class, and it provides a much better function than OpenGL. Unfortunately, VTK's biggest disadvantage is that there is no free tutorial, which only provides free API manual, which is just the functions of various classes, and reference books need to spend hundreds of dollars to buy, so learn VTK can only rely on it. A lot of example.

Since my project needs to have a possible cross-platform possible (now only in Windows), a set of cross-platform development libraries must be used, in the VTK / Examples / GUI example, only SDK, MFC, C under the Windows platform These examples of Builder, there are many MotiF, Python, TCL, the choice is a lot, but Motif is talked about programming, I have never been exposed to, TCL and Python as scripting languages, their performance and security have some order People are worried, that is, there is no one I can use this. . . Speaking of cross-platform, due to the relationship between the project, I have been in contact with QT, so before I didn't choose VTK, I was intended to use Qt OpenGL combination to develop this software, in fact, if not Consider cross-platform, I will still choose QT because it is more touching for me, and now uses VTK, and if you can use VTK and QT to use it. .

As a test, I used the VTK / Examples / Gui / Win32 / Samplemfc to make the following small procedures, very smooth, and the results proved that my guess, QT and VTK can be more convenient, let's take a step below. Step this procedure: 1) Download the latest version of VTK from http://www.vtk.org, I use 4.2. Only program files in this package, you still need to compile yourself, for this, you also need CMAKE, used to generate a VC.dsw, .dsp file. Then open the generated engineering file with VC and compile. Finally, the catalog generated after compiled: VTK / BIN / DEBUG is added to the PATH of the Windows system environment. 2) Download QT, I am currently using QT334, open the VC's Tools-> Options-> Directories, add the qt bin, include, and lib paths, respectively. 3) In VC, establish a new Win32 console application engineering, build a main.cpp, as the entrance to the main function, the code is as follows: #include

#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.exec ();} The testWindow.h is the master of the program. The header file of the window TestWindow will be created. What this program is done is to initialize QApplication and display TestWindow and enter the message cycle app.exec () of the main program.

The TestWindow class is implemented below, and TestWindow.h and TestWindow.cppTestWindow.h: #include

Class testwindow: public qmainwindow {q_object

PUBLIC: TestWindow (); ~ TestWindow ();

TestWindow.cpp: #include "testwindow.h" #include "moc_testwindow.h"

TestWindow :: TestWindow () {}

TestWindow :: ~ TestWindow () {}

Have you noticed the #include "moc_testwindow.h" in TestWindow.cpp, this moc_testwindow.h will establish TestWindow's RTTI information, and bind messages, etc., it does not need our own implementation, but by Qt The program named MOC is established. In the VC's fileview, right-click TestWindow.h and select Settings, then select the custom build page in the Open Project Settings dialog box, fill in Commands: moc -i testwindow.h - o moc_testWindow.h means calling the MOC program, automatically generates a moc file called moc_testwindow.h according to the contents of TestWindow.h. Fill in Outputs: MOC_TESTWINDOW.H

Then, in the Project Settings, Projct is selected, add: Qt-mt334.lib in the Object / library modules of the Link page: QT-MT334.LIB.

Compiler, if it is smooth, a simple QT program is written, it will display a blank window after startup.

3) Add to the VTK code, these code can refer to the Examples / GUI / WIN32 / SampleMFC program, transform TestWindow.h into the following: #include #include "vtkrenderer.h" #include "vtkwin32openglrenderwindow.h" #include "vtkwin32RenderWindowinteractor.h"

Class testwindow: public qmainwindow {q_object

PUBLIC: TESTWINDOW (); ~ TestWindow ();

Protected: Virtual Void Paintevent (QPaintevent *); Virtual Bool Winevent;

private: vtkRenderer * Renderer; vtkWin32OpenGLRenderWindow * RenderWindow; vtkWin32RenderWindowInteractor * Interactor;}; testwindow.cpp transformed into the following: #include "testwindow.h" #include "moc_testwindow.h"

#include "vtkactor2d.h" #include "vtktextMapper.h" #include "vtktextproperty.h" #include "vtkdatasetreader.h" #include "vtkdataSetMapper.h"

#include "vtkcommand.h" #include "vtkcamera.h" #include "vtkwin32renderwindowinteractor.h" #include "vtkinteractorstyletrackball popballcamera.h"

TestWindow :: TestWindow () {this-> renderer = vtkrenderer :: new (); this-> renderer-> setBackground (0.3, 0.5, 0.1); this-> renderWindow = vtkwin32otenglrenderWindow :: new (); this-> renderwindow -> addrenderer (this-> renderer); this-> interactor = vtkwin32renderwindowinteractor :: 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; this-> renderer-> addprop (actor); mapper-> delete (); reader-> delete (); actor-> delete ();}

TestWindow :: ~ TestWindow () {i (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 * e) {if (! this-> interactor-> getinitialized ()) {this-> renderWindow-> setWindowid (this-> winid ()); this-> renderWindow-> Windowinitialize (); this-> interactor-> setRenderWindow (this-> renderwindow) This-> interactor-> initialize ();} 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-> mess, msg-> lparam, msg-> wparam, this-> interactor);}}

Among them, a model file WeldedSpheres.vtk can be found in VTK. Maybe you need to modify its path.

Then, open the ProECT Settings dialog again, in the C / C page, select Category: Preprocessor, add: D: / VTK, D: / VTK / Parallel, D: / VTK / Hybrid, D: / VTK / Patented, D: / VTK / IO, D: / VTK / IMAGING, D: / VTK / GRAPHICS, D: / VTK / FILTERING, D: / VTK / COMMON

Select Link page, select the Category: Input, in the Object / library modules: Join in: 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: D: / vtk / bin / debug in Addtional Library Path assumes that VTK is installed under D-Dish, if you are installed in other directories Please modify the above path.

Ok, recompilate the program, run, you will see the VTK interface as shown below.

The above programs are just a simple application of VTK and QT, you can package VTK's display window in a QTWidget and display it in one area in the entire program.

转载请注明原文地址:https://www.9cbs.com/read-37980.html

New Post(0)