




已阅读5页,还剩14页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
qt和vtk共用作者:我是小李,欢迎交流来源:博客园发布时间:2009-02-10 11:00阅读:908 次原文链接 收藏 QT与VTK的结合开发2008/07/16 15:26最近,由于项目的需要,我打算使用VTK来开发程序,VTK全名是VisualizationToolKit,它是一套跨平台的C库,对 OpenGL作了较全面的封装,将各种三维模型的存储,运算,显示,交互等内容全都以类的方式封装起来了,并且提供比OpenGL强大得多的功能。可惜 VTK最大的缺点是,没有免费的教程,它只提供免费的API手册,其中只是对各个类的功能罗列而已,而参考书籍则需要花几十美元去购买,所以学习VTK只 能依靠它的大量Example了。由于我的项目需要兼故未来跨平台的可能(目前只在Windows下),所以必须使用一套跨平台的开发库, 在VTKExamplesGUI的例子里,Windows平台下只有SDK,MFC,C+ Builder这几个例子,另外还有Motif,Python,Tcl,选择是不少,但是Motif听说编程比较麻烦,我也从来没有接触过,Tcl和 Python作为脚本语言,其性能和安全性则有些令人担忧,也就是说,这里面没有一个是我能使用的。说起跨平台,由于单位里项目的关系,我接触得比较 多的就是QT了,所以,在未选定VTK之前,其实我是打算使用QTOpenGL的组合方式来开发这个软件的,其实,如果不考虑跨平台,我还是会选择 QT,因为它的事件处理方式对于用惯Delphi的我而言,更为顺手,那么现在使用了VTK,是否还能将VTK和QT组合起来使用呢。作为试验,我仿照VTKExamplesGUIWin32SampleMFC,制作了以下这个小程序,很顺利,结果证明了我的猜想,QT和VTK是可以很方便的结合起来的,下面就跟我来一步步制作这个程序吧:1)从/下 载VTK的最新版本,我用的是4.2。该包中只有程序文件,你还需要自己编译,为此你还需要CMake,用来生成VC的.dsw,.dsp文件。然后用 VC打开生成的工程文件,并进行编译。最后将编译后生成的目录:VTKbinDebug加入到Windows系统环境的Path中。2)下载QT,我目前使用的是QT334,打开VC的Tools-Options-Directories,将QT的bin,include,lib路径分别加入其中。3)在VC中,建立一个新的Win32 Console Application工程,建立一个main.cpp,作为main函数的入口,代码如下:#include #include testwindow.hint main(int argc, char* argv)QApplication app(argc, argv);TestWindow testwin;testwin.show();app.connect(&app, SIGNAL(lastWindowClosed(), &app, SLOT(quit();return app.exec();其中的testwindow.h,是程序的主窗口TestWindow的头文件,一会将会建立该文件。这段程序所做的是,初始化QApplication,并将TestWindow显示出来,并进入主程序的消息循环app.exec()。下面实现TestWindow类,分别建立testwindow.h和testwindow.cpptestwindow.h:#include class TestWindow: public QMainWindowQ_OBJECTpublic:TestWindow();TestWindow();testwindow.cpp:#include testwindow.h#include moc_testwindow.hTestWindow:TestWindow()TestWindow:TestWindow()你 是否注意到了testwindow.cpp中的#include moc_testwindow.h一行,这个moc_testwindow.h将会建立TestWindow的RTTI信息,并且绑定消息等,它并不 需要我们自己实现,而是由qt的一个名为moc的程序建立,在VC的FileView中,右键点击testwindow.h,并选择Settings,然 后在打开的Project Settings对话框中选择Custom Build页,在Commands中填入:moc -i testwindow.h -o moc_testwindow.h意思是调用moc程序,根据testwindow.h的内容,自动生成一个名为moc_testwindow.h的moc文件。在Outputs中填入:moc_testwindow.h然后,在Project Settings中选中projct,在Link页的Object/library modules中加入:qt-mt334.lib。编译程序,如果顺利,一个简单的QT程序就写好了,它会在启动后显示一个空白的窗口。3)加入VTK的代码,这些代码都可以参考ExamplesGUIWin32SampleMFC程序,将testwindow.h改造成如下:#include #include vtkRenderer.h#include vtkWin32OpenGLRenderWindow.h#include vtkWin32RenderWindowInteractor.hclass TestWindow: public QMainWindowQ_OBJECTpublic:TestWindow();TestWindow();protected:virtual void paintEvent(QPaintEvent *);virtual bool winEvent(MSG *);private:vtkRenderer *Renderer;vtkWin32OpenGLRenderWindow *RenderWindow;vtkWin32RenderWindowInteractor *Interactor;testwindow.cpp改造成如下:#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 vtkInteractorStyleTrackballCamera.hTestWindow: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 = 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(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)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-message, msg-lParam, msg-wParam, this-Interactor); return false;其中用到了一个模型文件weldedSpheres.vtk,可以在VTK中找到。可能你需要修改它的路径。然后,再次打开Proect Settings对话框,在C/C+页中,选择Category:Preprocessor,在Additional include directories:中加入:D:VTK,D:VTKParallel,D:VTKHybrid,D:VTKPatented,D:VTKRendering,D:VTKIO,D:VTKImaging,D:VTKGraphics,D:VTKFiltering,D:VTKCommon选择Link页,选择Category:Input,在Object/library modules:中加入: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 在Addtional library path中加入:D:VTKbindebug以上都假设VTK安装在D盘下,如果你安装在其它目录,请修改以上的路径。好了,重新编译程序,运行,你将看到如下所示的VTK界面。Tuesday, October 17th 2006, 6:13pmTutorial for using Qt with VTKHi,I am a newbie in VTK and Qt. For some time now,I have been trying to use VTK with Qt but faced a lot of problems getting started and configuring them. In the end, it worked(thanks to Anja Ende,Clinton Stimpson and everyone who helped me get started). I am writing down the procedure to get VTK to work with Qt in visual studio 2005 environment. Hopefully this document will help other newbies save a lot of time.I have tested the following procedure with MS Visual Studio 2005 Professional on Windows XP Pro X64.1). Install and get Qt working as per the instructions here: /wiki/Qt4_with_Visual_StudioThis works fine and you can start using Qt with visual studio(for those who just want to use Qt with Visual Studio, this is how we do it!)2). Install and configure VTK using Cmake with (a) VTK_USE_GUISUPPORT option and (b) VTK_USE_QVTK option, set to ONIf Qt is installed after VTK, you will need to reconfigure VTK to find Qt.3).Now its time to test the configuration with a code which uses both Qt and VTK. You can try it with the example in VTK source( ./Examples/GUI/Qt/Imageviewer). Copy this code(only the cxx file) to a new directory.4) I use qmake to generate build files. The method to build this example using qmake is:(a) open visual studio command prompt, go to the directory containing the cxx file and type: qmake -projectthis makes a .pro file. Open this file in an editor and modify it as below-# Automatically generated by qmake (2.00a) Wed Oct 11 17:14:01 2006#TEMPLATE = vcapp # this was originally app modify it to vcappTARGET +=DEPENDPATH += .INCLUDEPATH += . # here include the path to all the directories containing the header filesLIBS += # add this line and include all the libraries(Qt and VTK) needed for the application# InputSOURCES += main.cxx-(b)Save the .pro file and run qmake from the command prompt again. This will generate a .vcproj file. Open this file in visual studio and build it there. If you included all the libraries and include paths, this should build and run perfectly.I used qmake for building, because I didnt know how to do it with Cmake. But if the experts here can throw light on that it would help many. Also if someone knows of any other way(probably easier and smarter) to use Qt with VTK, please add to this document.Thanks,Ashish Go to the top of the page Quote Skip user informationalexiuk BeginnerPosts: 22 Thursday, May 10th 2007, 6:04pmRE: Tutorial for using Qt with VTKHi Ashish,Thanks for posting those details. I am trying to follow those instructions and am running into problems building QVTK. Error messages follow. Did you have any trouble building VTK?Thanks,Mark1- Build started: Project: QVTK, Configuration: Debug Win32 -1Generating moc_vtkEventQtSlotConnect.cxx1Generating moc_QVTKWidget.cxx1Compiling.1moc_vtkEventQtSlotConnect.cxx1c1xx : fatal error C1083: Cannot open source file: .moc_vtkEventQtSlotConnect.cxx: No such file or directory1moc_QVTKWidget.cxx1c1xx : fatal error C1083: Cannot open source file: .moc_QVTKWidget.cxx: No such file or directory1Generating Code.1Build log was saved at file:/c:VTKvcc_buildGUISupportQtQVTK.dirDebugBuildLog.htm1QVTK - 2 error(s), 0 warning(s)VTK Build summary:= Build: 1 succeeded, 3 failed, 47 up-to-date, 6 skipped Go to the top of the page Quote Skip user informationAshish BeginnerPosts: 83 Thursday, May 10th 2007, 8:30pmRE: Tutorial for using Qt with VTKHi Mark,I didnt face any problems building QVTK. Also I have stopped using qmake and use cmake these days.Can you give me more details as to what you have done so far and at what step are you getting the errors? Are you able to test run a simple Qt code?Thanks,Ashish Go to the top of the page Quote Skip user informationfrenchmikey BeginnerPosts: 14 Monday, February 16th 2009, 8:41amVTK with QTcreatorHi Ashish,I installed VTK and successfully built all the examples ( specially the GUI/Qt). everything is working fine when compiled with CMAKEnow l try to compile the same examples with Qtcreator and his qmake funtion. doesnt workI updated my .pro with the includepath and library . but doesnt work.do you have a simple .pro so l can check if l dont miss anything ? also lm using mingwin .thanksMichaelmy .pro:CONFIG+= DEBUG TEMPLATE = appTARGET = VTK_testDEPENDPATH += .INCLUDEPATH +=C:VTKbin C:VTKincludevtk-5.2LIBS +=# InputSOURCES += main.cxxCompile messages:Starting: C:/Qt/QtCreator/mingw/bin/mingw32-make.exe debug -w mingw32-make: Entering directory C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtkC:/Qt/QtCreator/mingw/bin/mingw32-make -f Makefile.Debugmingw32-make1: Entering directory C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtkg+ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -Ic:QtQtCreatorqtincludeQtCore -Ic:QtQtCreatorqtincludeQtCore -Ic:QtQtCreatorqtincludeQtGui -Ic:QtQtCreatorqtincludeQtGui -Ic:QtQtCreatorqtinclude -Ic:VTKbin -Ic:VTKincludevtk-5.2 -Ic:QtQtCreatorqtincludeActiveQt -Idebug -I. -Ic:QtQtCreatorqtmkspecswin32-g+ -o debugmain.o main.cxxmingw32-make1: Leaving directory C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtkmingw32-make: Leaving directory C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtkThis application has requested the Runtime to terminate it in an unusual way.Please contact the applications support team for more information.mingw32-make1: * debug/main.o Error 3mingw32-make: * debug Error 2Exited with code 2.Error while building project vtkWhen executing build step Make Go to the top of the page Quote Skip user informationxzh_biti BeginnerPosts: 15 Wednesday, March 11th 2009, 1:46amhello ,everyonehello, everyone, i am a foreigner, and i am poor in English, so forgive me my English.So i want to ask Michael , have your solved your problem with qtcreator to compile VTK, if you have, could you tell me how to write .pro , and do you have some vtk electrical books ? In my country, so few information about vtk, and please send me some of them , thank you very much! And my email is : xzh_ Go to the top of the page Quote Skip user informationjustadreamer BeginnerPosts: 26 Friday, April 24th 2009, 10:13pmQtCreator + QT + VTK in LinuxHi, just wanted to share my experience on a jump start with using Qt+VTK.As I dont have experience with using Qt I decided to go the easiest possible way - downloaded Qt4 and the QtCreator. The steps I took next:1. Built Qt4 from source and installed it.2. Added /usr/local/Trolltech to my PATH environment variable3. Downloaded the source for VTK 5.44. Launched ccmake in the root of the source tree. 5. Switched ON VTK_USE_GUISUPPORT VTK_USE_QVTK.6. Pressed c to configure in ccmake, and specified the version of Qt 4.0. 7. After configuring in ccmake, launched cmake, and then make and make install.8. The needed QVTKWidget.h will be in the /usr/bin/local/include/vtk-5.4.9. Now we launch QtCreator and create the simple application.10. In the mainwindow.h I add these headers:Source code123#include #include #include And these members:Source code12QVTKWidget* vtkWidget; vtkRenderer* ren;11. In the mainwindow.cpp I added this code in the constructor:Source code12345678910MainWindow:MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui:MainWindowClass) ui-setupUi(this); vtkWidget = new QVTKWidget(this,QFlag(0); ui-verticalLayout-addWidget(vtkWidget); ui-verticalLayout-update(); ren = vtkRenderer:New(); vtkWidget-GetRenderWindow()-AddRenderer(ren); ren-SetBackground(1.0,0,0); ren-Render(); And destructor:Source code1234MainWindow:MainWindow() ren-Delete(); delete vtkWidget; delete ui; As you see above I have added a verticalLayout to my form - it has some more controls, and the widget I am adding goes to the bottom. 12. In the .pro file I added these:Source code123 LIBS += -L/usr/local/lib/vtk-5.4 -lvtkCommon -lvtksys -lQVTK -lvtkQtChart -lvtkViews -lvtkWidgets -lvtkInfovis -lvtkRendering -lvtkGraphics -lvtkImaging -lvtkIO -lvtkFiltering -lvtklibxml2 -lvtkDICOMParser -lvtkpng -lvtkpng -lvtktiff -lvtkzlib -lvtkjpeg -lvtkalglib -lvtkexpat -lvtkverdict -lvtkmetaio -lvtkNetCDF -lvtksqlite -lvtkexoIIc -lvtkftgl -lvtkfreetype -lvtkHybrid INCLUDEPATH += /usr/local/include/vtk-5.413. In the Project-Build settings-Build environment I added a variable LD_LIBRARY_PATH
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 电瓶车保险相关知识培训课件
- 北京五年级考试数学题及答案
- 电焊工职业健康知识培训课件
- 高温防护安全知识培训课件
- 2-Ethyl-ss-ketobutiryl-SCoA-2-Ethyl-ss-ketobutiryl-coenzyme-A-生命科学试剂-MCE
- 新解读《GB-T 25122.1-2018轨道交通 机车车辆用电力变流器 第1部分:特性和试验方法》
- 会考物理考试题及答案
- 电焊学徒基础知识培训总结
- 保定动力技校考试题目及答案
- 蚌埠四中近期考试试卷及答案
- 中国土地荒漠化课件
- 2025晋中祁县司法协理员招聘笔试备考试题及答案解析
- Unit 3 Same or DifferentSection A Grammar Focus (3a-3c) 课件-2025-2026学年人教版八年级英语上册
- 2025数据中心机房建设方案
- 管线及设备开启作业安全管理制度与操作流程
- 2025年浙江社区《网格员》模拟训练题(含答案)
- 揭西招投标管理办法
- 社区与小课堂的合同协议
- DG-TJ08-2467-2025 超低能耗建筑设计标准(居住建筑)
- GB/T 45844-2025智慧城市基础设施开发和运营通用框架
- 养老机构风险防范课件
评论
0/150
提交评论