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