QT+VTK 对接使用
由于MFC和pcl的不兼容問題,只能用QT和VTK進(jìn)行程序開發(fā),確實(shí)是一件蛋疼的事!
出自于QT與VTK結(jié)合系列:http://blog.csdn.net/tonylk/article/details/341068
這個(gè)教程非常詳細(xì).
QT與VTK的結(jié)合開發(fā)
原文鏈接:http://blog.csdn.net/tonylk/article/details/341068
???????? VTK全名是VisualizationToolKit,它是一套跨平臺的C++庫,對OpenGL作了較全面的封裝,將各種三維模型的存儲,運(yùn)算,顯示,交互等內(nèi)容全都以類的方式封裝起來了,并且提供比OpenGL強(qiáng)大得多的功能。可惜VTK最大的缺點(diǎn)是,沒有免費(fèi)的教程,它只提供免費(fèi)的API手冊,其中只是對各個(gè)類的功能羅列而已,而參考書籍則需要花幾十美元去購買,所以學(xué)習(xí)VTK只能依靠它的大量Example了。
??????? 由于我的項(xiàng)目需要兼故未來跨平臺的可能(目前只在Windows下),所以必須使用一套跨平臺的開發(fā)庫,在VTK/Examples/GUI的例子里,Windows平臺下只有SDK,MFC,C++ Builder這幾個(gè)例子,另外還有Motif,Python,Tcl,選擇是不少,但是Motif聽說編程比較麻煩,我也從來沒有接觸過,Tcl和Python作為腳本語言,其性能和安全性則有些令人擔(dān)憂,也就是說,這里面沒有一個(gè)是我能使用的。。。說起跨平臺,由于單位里項(xiàng)目的關(guān)系,我接觸得比較多的就是QT了,所以,在未選定VTK之前,其實(shí)我是打算使用QT+OpenGL的組合方式來開發(fā)這個(gè)軟件的,其實(shí),如果不考慮跨平臺,我還是會(huì)選擇QT,因?yàn)樗氖录幚矸绞綄τ谟脩TDelphi的我而言,更為順手,那么現(xiàn)在使用了VTK,是否還能將VTK和QT組合起來使用呢。。
?????? ? 作為試驗(yàn),我仿照VTK/Examples/GUI/Win32/SampleMFC,制作了以下這個(gè)小程序,很順利,結(jié)果證明了我的猜想,QT和VTK是可以很方便的結(jié)合起來的,下面就跟我來一步步制作這個(gè)程序吧:對于新建立的QT對話框程序,源碼如下:
#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.exec(); }其中的testwindow.h,是程序的主窗口TestWindow的頭文件,一會(huì)將會(huì)建立該文件。
這段程序所做的是,初始化QApplication,并將TestWindow顯示出來,并進(jìn)入主程序的消息循環(huán)app.exec()。
下面實(shí)現(xiàn)TestWindow類,分別建立testwindow.h和testwindow.cpp
testwindow.h: #include <qmainwindow.h>class TestWindow: public QMainWindow {Q_OBJECT public:TestWindow();~TestWindow(); };testwindow.cpp: #include "testwindow.h" #include "moc_testwindow.h" TestWindow::TestWindow() { } TestWindow::~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,然后在打開的Project Settings對話框中選擇Custom Build頁,在Commands中填入:
moc -i testwindow.h -o moc_testwindow.h
意思是調(diào)用moc程序,根據(jù)testwindow.h的內(nèi)容,自動(dòng)生成一個(gè)名為moc_testwindow.h的moc文件。
在Outputs中填入:
moc_testwindow.h
然后,在Project Settings中選中projct,在Link頁的Object/library modules中加入:qt-mt334.lib。
編譯程序,如果順利,一個(gè)簡單的QT程序就寫好了,它會(huì)在啟動(dòng)后顯示一個(gè)空白的窗口。
3)加入VTK的代碼,這些代碼都可以參考Examples/GUI/Win32/SampleMFC程序,將testwindow.h改造成如下:
#include <qmainwindow.h> #include "vtkRenderer.h" #include "vtkWin32OpenGLRenderWindow.h" #include "vtkWin32RenderWindowInteractor.h"class TestWindow: public QMainWindow {Q_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.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("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; }其中用到了一個(gè)模型文件weldedSpheres.vtk,可以在VTK中找到。可能你需要修改它的路徑。
然后,再次打開Proect Settings對話框,在C/C++頁中,選擇Category:Preprocessor,在Additional include directories:中加入:
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/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:/VTK/bin/debug
以上都假設(shè)VTK安裝在D盤下,如果你安裝在其它目錄,請修改以上的路徑。
好了,重新編譯程序,運(yùn)行,你將看到如下所示的VTK界面。
以上程序只是VTK和QT結(jié)合的簡單應(yīng)用,你完全可以將VTK的顯示窗封裝在一個(gè)QTWidget中,然后將其顯示在整個(gè)程序中的一個(gè)區(qū)域中。
總結(jié)
以上是生活随笔為你收集整理的QT+VTK 对接使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CentOS安装-(CentOS7)最小
- 下一篇: QT与openCV,与PCL结合!