VTK和QT联合使用.docx
- 文档编号:28153940
- 上传时间:2023-07-08
- 格式:DOCX
- 页数:22
- 大小:157.75KB
VTK和QT联合使用.docx
《VTK和QT联合使用.docx》由会员分享,可在线阅读,更多相关《VTK和QT联合使用.docx(22页珍藏版)》请在冰豆网上搜索。
VTK和QT联合使用
qt和vtk共用
作者:
我是小李,欢迎交流 来源:
博客园 发布时间:
2009-02-1011:
00 阅读:
908次 原文链接 [收藏]
QT与VTK的结合开发
2008/07/1615:
26
最近,由于项目的需要,我打算使用VTK来开发程序,VTK全名是VisualizationToolKit,它是一套跨平台的C++库,对OpenGL作了较全面的封装,将各种三维模型的存储,运算,显示,交互等内容全都以类的方式封装起来了,并且提供比OpenGL强大得多的功能。
可惜VTK最大的缺点是,没有免费的教程,它只提供免费的API手册,其中只是对各个类的功能罗列而已,而参考书籍则需要花几十美元去购买,所以学习VTK只能依靠它的大量Example了。
由于我的项目需要兼故未来跨平台的可能(目前只在Windows下),所以必须使用一套跨平台的开发库,在VTK\Examples\GUI的例子里,Windows平台下只有SDK,MFC,C++Builder这几个例子,另外还有Motif,Python,Tcl,选择是不少,但是Motif听说编程比较麻烦,我也从来没有接触过,Tcl和Python作为脚本语言,其性能和安全性则有些令人担忧,也就是说,这里面没有一个是我能使用的.。
.说起跨平台,由于单位里项目的关系,我接触得比较多的就是QT了,所以,在未选定VTK之前,其实我是打算使用QT+OpenGL的组合方式来开发这个软件的,其实,如果不考虑跨平台,我还是会选择QT,因为它的事件处理方式对于用惯Delphi的我而言,更为顺手,那么现在使用了VTK,是否还能将VTK和QT组合起来使用呢。
。
作为试验,我仿照VTK\Examples\GUI\Win32\SampleMFC,制作了以下这个小程序,很顺利,结果证明了我的猜想,QT和VTK是可以很方便的结合起来的,下面就跟我来一步步制作这个程序吧:
1)从http:
//www。
vtk.org/下载VTK的最新版本,我用的是4。
2。
该包中只有程序文件,你还需要自己编译,为此你还需要CMake,用来生成VC的.dsw,。
dsp文件。
然后用VC打开生成的工程文件,并进行编译。
最后将编译后生成的目录:
VTK\bin\Debug加入到Windows系统环境的Path中。
2)下载QT,我目前使用的是QT334,打开VC的Tools-〉Options—〉Directories,将QT的bin,include,lib路径分别加入其中。
3)在VC中,建立一个新的Win32ConsoleApplication工程,建立一个main。
cpp,作为main函数的入口,代码如下:
#include h〉 #include”testwindow。 h" intmain(intargc,char**argv) { QApplicationapp(argc,argv); TestWindowtestwin; testwin.show(); app.connect(&app,SIGNAL(lastWindowClosed()),&app,SLOT(quit())); returnapp.exec(); } 其中的testwindow.h,是程序的主窗口TestWindow的头文件,一会将会建立该文件。 这段程序所做的是,初始化QApplication,并将TestWindow显示出来,并进入主程序的消息循环app.exec()。 下面实现TestWindow类,分别建立testwindow.h和testwindow.cpp testwindow。 h: #include〈qmainwindow。 h〉 classTestWindow: publicQMainWindow { Q_OBJECT public: TestWindow(); ~TestWindow(); }; testwindow.cpp: #include"testwindow。 h" #include"moc_testwindow.h” TestWindow: : TestWindow() { } TestWindow: : ~TestWindow() { } 你是否注意到了testwindow。 cpp中的#include”moc_testwindow.h”一行,这个moc_testwindow.h将会建立TestWindow的RTTI信息,并且绑定消息等,它并不需要我们自己实现,而是由qt的一个名为moc的程序建立,在VC的FileView中,右键点击testwindow.h,并选择Settings,然后在打开的ProjectSettings对话框中选择CustomBuild页,在Commands中填入: moc-itestwindow.h-omoc_testwindow。 h 意思是调用moc程序,根据testwindow.h的内容,自动生成一个名为moc_testwindow.h的moc文件。 在Outputs中填入: moc_testwindow。 h 然后,在ProjectSettings中选中projct,在Link页的Object/librarymodules中加入: qt—mt334.lib。 编译程序,如果顺利,一个简单的QT程序就写好了,它会在启动后显示一个空白的窗口。 3)加入VTK的代码,这些代码都可以参考Examples\GUI\Win32\SampleMFC程序,将testwindow.h改造成如下: #include〈qmainwindow。 h〉 #include”vtkRenderer.h" #include"vtkWin32OpenGLRenderWindow。 h" #include”vtkWin32RenderWindowInteractor。 h" classTestWindow: publicQMainWindow { Q_OBJECT public: TestWindow(); ~TestWindow(); protected: virtualvoidpaintEvent(QPaintEvent*); virtualboolwinEvent(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.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=vtkWin32RenderWindowInteractor: : New(); vtkActor2D*actor2d=vtkActor2D: : New(); vtkTextMapper*txt=vtkTextMapper: : New(); actor2d—>SetMapper(txt); txt—〉SetInput("HelloWorld”); 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(); } } voidTestWindow: : 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(); } boolTestWindow: : winEvent(MSG*msg) { switch(msg—〉message){ caseWM_LBUTTONDOWN: caseWM_LBUTTONUP: caseWM_MBUTTONDOWN: caseWM_MBUTTONUP: caseWM_RBUTTONDOWN: caseWM_RBUTTONUP: caseWM_MOUSEMOVE: caseWM_CHAR: caseWM_TIMER: if(this—>Interactor->GetInitialized()){ vtkHandleMessage2(msg-〉hwnd,msg—〉message,msg—>lParam,msg->wParam,this->Interactor); } } returnfalse; } 其中用到了一个模型文件weldedSpheres。 vtk,可以在VTK中找到.可能你需要修改它的路径。 然后,再次打开ProectSettings对话框,在C/C++页中,选择Category: Preprocessor,在Additionalincludedirectories: 中加入: 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 选择Link页,选择Category: Input,在Object/librarymodules: 中加入: vtkRendering.libvtkGraphics.libvtkImaging。 libvtkIO。 libvtkFiltering。 libvtkCommon。 libvtkftgl.libglu32.libopengl32。 libglu32。 libopengl32.libvtkfreetype.libvtkpng.libvtktiff.libvtkzlib。 libvtkjpeg。 libvtkexpat。 lib 在Addtionallibrarypath中加入: D: \VTK\bin\debug 以上都假设VTK安装在D盘下,如果你安装在其它目录,请修改以上的路径。 好了,重新编译程序,运行,你将看到如下所示的VTK界面. Tuesday,October17th2006,6: 13pm TutorialforusingQtwithVTK Hi, IamanewbieinVTKandQt.Forsometimenow,IhavebeentryingtouseVTKwithQtbutfacedalotofproblemsgettingstartedandconfiguringthem.Intheend,itworked(thankstoAnjaEnde,ClintonStimpsonandeveryonewhohelpedmegetstarted)。 IamwritingdowntheproceduretogetVTKtoworkwithQtinvisualstudio2005environment。 Hopefullythisdocumentwillhelpothernewbiessavealotoftime。 IhavetestedthefollowingprocedurewithMSVisualStudio2005ProfessionalonWindowsXPProX64。 1).InstallandgetQtworkingaspertheinstructionshere: http: //qtnode。 net/wiki/Qt4_with_Visual_Studio ThisworksfineandyoucanstartusingQtwithvisualstudio(forthosewhojustwanttouseQtwithVisualStudio,thisishowwedoit! ) 2).InstallandconfigureVTKusingCmakewith(a)VTK_USE_GUISUPPORToptionand(b)VTK_USE_QVTKoption,setto'ON' IfQtisinstalledafterVTK,youwillneedtoreconfigureVTKtofindQt。 3)。 Nowit'stimetotesttheconfigurationwithacodewhichusesbothQtandVTK。 YoucantryitwiththeexampleinVTKsource(。 ./Examples/GUI/Qt/Imageviewer)。 Copythiscode(onlythecxxfile)toanewdirectory。 4)Iuse’qmake'togeneratebuildfiles。 Themethodtobuildthisexampleusingqmakeis: (a)openvisualstudiocommandprompt,gotothedirectorycontainingthecxxfileandtype: qmake—project thismakesa’。 pro’file.Openthisfileinaneditorandmodifyitasbelow- -—-- ###################################################################### #Automaticallygeneratedbyqmake(2.00a)WedOct1117: 14: 012006 ###################################################################### TEMPLATE=vcapp#thiswasoriginally’app'modifyitto'vcapp’ TARGET+= DEPENDPATH+=. INCLUDEPATH+=。 #hereincludethepathtoallthedirectoriescontainingtheheaderfiles LIBS+=#addthislineandincludeallthelibraries(QtandVTK)neededfortheapplication #Input SOURCES+=main.cxx ——--— (b)Savethe.profileandrunqmakefromthecommandpromptagain。 Thiswillgeneratea'。 vcproj'file.Openthisfileinvisualstudioandbuilditthere.Ifyouincludedallthelibrariesandincludepaths,thisshouldbuildandrunperfectly. Iusedqmakeforbuilding,becauseIdidn’tknowhowtodoitwithCmake.Butiftheexpertsherecanthrowlightonthatitwouldhelpmany.Alsoifsomeoneknowsofanyotherway(probablyeasierandsmarter)touseQtwithVTK,pleaseaddtothisdocument。 Thanks, Ashish ∙ Gotothetopofthepage ∙ Quote Skipuserinformation alexiuk Beginner Posts: 2 ∙ ∙ 2 Thursday,May10th2007,6: 04pm RE: TutorialforusingQtwithVTK HiAshish, Thanksforpostingthosedetails。 IamtryingtofollowthoseinstructionsandamrunningintoproblemsbuildingQVTK.Errormessagesfollow。 DidyouhaveanytroublebuildingVTK? Thanks, Mark 1>———-——Buildstarted: Project: QVTK,Configuration: DebugWin32——-——— 1>Generatingmoc_vtkEventQtSlotConnect.cxx 1〉Generatingmoc_QVTKWidget.cxx 1〉Compiling。 。 . 1〉moc_vtkEventQtSlotConnect。 cxx 1>c1xx: fatalerrorC1083: Cannotopensourcefile: ’.\moc_vtkEventQtSlotConnect.cxx': Nosuchfileordirectory 1〉moc_QVTKWidget.cxx 1>c1xx: fatalerrorC1083: Cannotopensourcefile: ’.\moc_QVTKWidget。 cxx’: Nosuchfileordirectory 1〉GeneratingCode。 .. 1>Buildlogwassavedat”file: //c: \VTK\vcc_build\GUISupport\Qt\QVTK.dir\Debug\BuildLog。 htm" 1>QVTK-2error(s),0warning(s) VTKBuildsummary: ==========Build: 1succeeded,3failed,47up—to-date,6skipped ∙ Gotothetopofthepage ∙ Quote Skipuserinformation Ashish Beginner ∙ Posts: 8 ∙ ∙ 3 Thursday,May10th2007,8: 30pm RE: TutorialforusingQtwithVTK HiMark, Ididn’tfaceanyproblemsbuildingQVTK.AlsoIhavestoppedusing'qmake’anduse'cmake'thesedays.Canyougivememoredetailsastowhatyouhavedonesofarandatwhatstepareyougettingtheerrors? AreyouabletotestrunasimpleQtcode? Thanks, Ashish ∙ Gotothetopofthepa
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- VTK QT 联合 使用
![提示](https://static.bdocx.com/images/bang_tan.gif)