版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、qt和vtk共用作者:我是小李,歡迎交流 來(lái)源:博客園 發(fā)布時(shí)間:2009-02-10 11:00 閱讀:908 次 原文鏈接 收藏 QT與VTK的結(jié)合開(kāi)發(fā)2008/07/16 15:26最近,由于項(xiàng)目的需要,我打算使用VTK來(lái)開(kāi)發(fā)程序,VTK全名是VisualizationToolKit,它是一套跨平臺(tái)的C庫(kù),對(duì) OpenGL作了較全面的封裝,將各種三維模型的存儲(chǔ),運(yùn)算,顯示,交互等內(nèi)容全都以類(lèi)的方式封裝起來(lái)了,并且提供比OpenGL強(qiáng)大得多的功
2、能??上?VTK最大的缺點(diǎn)是,沒(méi)有免費(fèi)的教程,它只提供免費(fèi)的API手冊(cè),其中只是對(duì)各個(gè)類(lèi)的功能羅列而已,而參考書(shū)籍則需要花幾十美元去購(gòu)買(mǎi),所以學(xué)習(xí)VTK只 能依靠它的大量Example了。由于我的項(xiàng)目需要兼故未來(lái)跨平臺(tái)的可能(目前只在Windows下),所以必須使用一套跨平臺(tái)的開(kāi)發(fā)庫(kù), 在VTKExamplesGUI的例子里,Windows平臺(tái)下只有SDK,MFC,C+ Builder這幾個(gè)例子,另外還有Motif,Python,Tcl,選擇是不少,但是Motif聽(tīng)說(shuō)編程比較麻煩,我也從來(lái)沒(méi)有接觸過(guò),Tcl和 Python作為腳本語(yǔ)言,其性能和安全性則有些令人擔(dān)憂(yōu),也就是說(shuō),這里面沒(méi)有一個(gè)是我
3、能使用的。說(shuō)起跨平臺(tái),由于單位里項(xiàng)目的關(guān)系,我接觸得比較 多的就是QT了,所以,在未選定VTK之前,其實(shí)我是打算使用QTOpenGL的組合方式來(lái)開(kāi)發(fā)這個(gè)軟件的,其實(shí),如果不考慮跨平臺(tái),我還是會(huì)選擇 QT,因?yàn)樗氖录幚矸绞綄?duì)于用慣Delphi的我而言,更為順手,那么現(xiàn)在使用了VTK,是否還能將VTK和QT組合起來(lái)使用呢。作為試驗(yàn),我仿照VTKExamplesGUIWin32SampleMFC,制作了以下這個(gè)小程序,很順利,結(jié)果證明了我的猜想,QT和VTK是可以很方便的結(jié)合起來(lái)的,下面就跟我來(lái)一步步制作這個(gè)程序吧:1)從下 載VTK的最新版本,我用的是4.2。該包中只有程序文件,你還需要自己編
4、譯,為此你還需要CMake,用來(lái)生成VC的.dsw,.dsp文件。然后用 VC打開(kāi)生成的工程文件,并進(jìn)行編譯。最后將編譯后生成的目錄:VTKbinDebug加入到Windows系統(tǒng)環(huán)境的Path中。2)下載QT,我目前使用的是QT334,打開(kāi)VC的Tools->Options->Directories,將QT的bin,include,lib路徑分別加入其中。3)在VC中,建立一個(gè)新的Win32 Console Application工程,建立一個(gè)main.cpp,作為main函數(shù)的入口,代碼如下:#include <qapplication.h>#include &qu
5、ot;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();其中的testwindow.h,是程序的主窗口TestWindow的頭文件,一會(huì)將會(huì)建立該文件。這段程序所做的是,初始化QApplication,并將TestWindow顯示出來(lái),并進(jìn)入主程序的
6、消息循環(huán)app.exec()。下面實(shí)現(xiàn)TestWindow類(lèi),分別建立testwindow.h和testwindow.cpptestwindow.h:#include <qmainwindow.h>class TestWindow: public QMainWindowQ_OBJECTpublic:TestWindow();TestWindow();testwindow.cpp:#include "testwindow.h"#include "moc_testwindow.h"TestWindow:TestWindow()TestWindow
7、:TestWindow()你 是否注意到了testwindow.cpp中的#include "moc_testwindow.h"一行,這個(gè)moc_testwindow.h將會(huì)建立TestWindow的RTTI信息,并且綁定消息等,它并不 需要我們自己實(shí)現(xiàn),而是由qt的一個(gè)名為moc的程序建立,在VC的FileView中,右鍵點(diǎn)擊testwindow.h,并選擇Settings,然 后在打開(kāi)的Project Settings對(duì)話框中選擇Custom Build頁(yè),在Commands中填入:moc -i testwindow.h -o moc_testwindow.h意思是調(diào)用
8、moc程序,根據(jù)testwindow.h的內(nèi)容,自動(dòng)生成一個(gè)名為moc_testwindow.h的moc文件。在Outputs中填入:moc_testwindow.h然后,在Project Settings中選中projct,在Link頁(yè)的Object/library modules中加入:qt-mt334.lib。編譯程序,如果順利,一個(gè)簡(jiǎn)單的QT程序就寫(xiě)好了,它會(huì)在啟動(dòng)后顯示一個(gè)空白的窗口。3)加入VTK的代碼,這些代碼都可以參考ExamplesGUIWin32SampleMFC程序,將testwindow.h改造成如下:#include <qmainwindow.h>#inc
9、lude "vtkRenderer.h"#include "vtkWin32OpenGLRenderWindow.h"#include "vtkWin32RenderWindowInteractor.h"class TestWindow: public QMainWindowQ_OBJECTpublic:TestWindow();TestWindow();protected:virtual void paintEvent(QPaintEvent *);virtual bool winEvent(MSG *);private:vtkRe
10、nderer *Renderer;vtkWin32OpenGLRenderWindow *RenderWindow;vtkWin32RenderWindowInteractor *Interactor;testwindow.cpp改造成如下:#include "testwindow.h"#include "moc_testwindow.h"#include "vtkActor2D.h"#include "vtkTextMapper.h"#include "vtkTextProperty.h"#i
11、nclude "vtkDataSetReader.h"#include "vtkDataSetMapper.h"#include "vtkCommand.h"#include "vtkCamera.h"#include "vtkWin32RenderWindowInteractor.h"#include "vtkInteractorStyleTrackballCamera.h"TestWindow:TestWindow()this->Renderer = vtkRend
12、erer: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 = vtkTe
13、xtMapper: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->
14、;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->Int
15、eractor) this->Interactor->Delete();if (this->Renderer) this->Renderer->SetRenderWindow(NULL);if (this->RenderWindow) this->RenderWindow->Delete();if (this->Renderer) this->Renderer->Delete();void TestWindow:paintEv
16、ent(QPaintEvent *e)if (! this->Interactor->GetInitialized() this->RenderWindow->SetWindowId(this->winId(); this->RenderWindow->WindowInitialize(); this->Interactor->SetRenderWindow(this->RenderWindow); this->Interactor
17、->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_R
18、BUTTONUP: case WM_MOUSEMOVE: case WM_CHAR: case WM_TIMER: if (this->Interactor->GetInitialized() vtkHandleMessage2(msg->hwnd, msg->message, msg->lParam, msg->wParam, this->Interactor);
19、60; return false;其中用到了一個(gè)模型文件weldedSpheres.vtk,可以在VTK中找到??赡苣阈枰薷乃穆窂?。然后,再次打開(kāi)Proect Settings對(duì)話框,在C/C+頁(yè)中,選擇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頁(yè),選擇Catego
20、ry: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
21、以上都假設(shè)VTK安裝在D盤(pán)下,如果你安裝在其它目錄,請(qǐng)修改以上的路徑。好了,重新編譯程序,運(yùn)行,你將看到如下所示的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 work
22、ed(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 Studi
23、o 2005 Professional on Windows XP Pro X64.1). Install and get Qt working as per the instructions here: This 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_
24、GUISUPPORT option and (b) VTK_USE_QVTK option, set to 'ON'If Qt is installed after VTK, you will need to reconfigure VTK to find Qt.3).Now it's 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/Imagevie
25、wer). 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. Op
26、en 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 'vcapp'TARGET +=DEPENDPATH += .INCLUDEPATH += . # here include the path to all the directories containing the
27、 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 y
28、ou included all the libraries and include paths, this should build and run perfectly.I used qmake for building, because I didn't 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) t
29、o use Qt with VTK, please add to this document.Thanks,Ashish · Go to the top of the page · Quote Skip user informationalexiuk BeginnerPosts: 2··2 Thursday, May 10th 2007, 6:04pmRE: Tutorial for using Qt with VTKHi Ashish,Thanks for posting those details. I am trying to follow tho
30、se 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 -1>Generating moc_vtkEventQtSlotConnect.cxx1>Generating moc_QVTKWidget.cxx1>Compiling.1>moc_
31、vtkEventQtSlotConnect.cxx1>c1xx : fatal error C1083: Cannot open source file: '.moc_vtkEventQtSlotConnect.cxx': No such file or directory1>moc_QVTKWidget.cxx1>c1xx : fatal error C1083: Cannot open source file: '.moc_QVTKWidget.cxx': No such file or directory1>Generating C
32、ode.1>Build log was saved at "file:/c:VTKvcc_buildGUISupportQtQVTK.dirDebugBuildLog.htm"1>QVTK - 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 Beginner·
33、Posts: 8··3 Thursday, May 10th 2007, 8:30pmRE: Tutorial for using Qt with VTKHi Mark,I didn't 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
34、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: 1··4 Monday, February 16th 2009, 8:41amVTK with QTcreatorHi Ashish,I installed VTK and successfully built all the ex
35、amples ( 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. doesn't workI updated my .pro with the includepath and library . but doesn't work.do you have a simple .pro so l can check if l don&
36、#39;t miss anything ? also l'm 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:
37、Entering directory C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtk'C:/Qt/QtCreator/mingw/bin/mingw32-make -f Makefile.Debugmingw32-make1: Entering directory C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtk'g+ -c -g -frtti -fexceptions -mthreads -Wall -DUNICOD
38、E -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"c:QtQtCreatorqtincludeQtCore" -I"c:QtQtCreatorqtincludeQtCore" -I"c:QtQtCreatorqtincludeQtGui" -I"c:QtQtCreatorqtincludeQtGui" -I"c:QtQtCreatorqtinclude&q
39、uot; -I"c:VTKbin" -I"c:VTKincludevtk-5.2" -I"c:QtQtCreatorqtincludeActiveQt" -I"debug" -I"." -I"c:QtQtCreatorqtmkspecswin32-g+" -o debugmain.o main.cxxmingw32-make1: Leaving directory C:/Documents and Settings/Michael/Desktop/Project/Qt_cre
40、ator/vtk'mingw32-make: Leaving directory C:/Documents and Settings/Michael/Desktop/Project/Qt_creator/vtk'This application has requested the Runtime to terminate it in an unusual way.Please contact the application's support team for more information.mingw32-make1: * debug/main.o Error 3m
41、ingw32-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: 1··5 Wednesday, March 11th 2009, 1:46amhello ,everyonehello, everyone, i am a f
42、oreigner, 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
43、send me some of them , thank you very much! And my email is : · Go to the top of the page · Quote Skip user informationjustadreamer BeginnerPosts: 2··6 Friday, April 24th 2009, 10:13pmQtCreator + QT + VTK in LinuxHi, just wanted to share my experience on a jump start with using Q
44、t+VTK.As I don't 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
45、 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/b
46、in/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 <QVTKWidget.h> #include <vtkRenderer.h> #include <vtkRenderWindow.h>And these members:Source code12QVTKWidget* vtkWidget; vtkRend
47、erer* 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-&
48、gt;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 vertic
49、alLayout 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.
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 擠壓模擬課程設(shè)計(jì)
- 銀行支行的行政后勤工作綜述
- 寵物服務(wù)員工作總結(jié)
- 港口貨物裝卸合同三篇
- 三年級(jí)科學(xué)學(xué)科的教學(xué)工作總結(jié)
- 門(mén)診護(hù)士年終總結(jié)
- 【八年級(jí)下冊(cè)歷史】期中達(dá)標(biāo)測(cè)試卷
- 2024年統(tǒng)計(jì)員年終工作總結(jié)篇
- 2024-2025學(xué)年北京門(mén)頭溝區(qū) 初三(上)期末物物理試卷(含答案)
- 分包采購(gòu)委托合同(2篇)
- 《人員素質(zhì)測(cè)評(píng)理論與方法》電子版本
- 61850基礎(chǔ)技術(shù)介紹0001
- 陶瓷色料的技術(shù)PPT課件
- 幼兒園食品安全工作計(jì)劃四篇
- 課程設(shè)計(jì)YA32-350型四柱萬(wàn)能液壓機(jī)液壓系統(tǒng)設(shè)計(jì)
- (精心整理)系動(dòng)詞練習(xí)題
- 體彩排列五歷史數(shù)據(jù)
- 中國(guó)工業(yè)數(shù)據(jù)庫(kù)介紹
- 弱電智能化設(shè)計(jì)服務(wù)建議書(shū)(共35頁(yè))
- 中國(guó)銀監(jiān)會(huì)關(guān)于規(guī)范中長(zhǎng)期貸款還款方式的通知
- 通信工程外文文獻(xiàn)(共12頁(yè))
評(píng)論
0/150
提交評(píng)論