-
1, Open Qt Creator. In the file menu select new project and create a Qt widget Application.
-
2, Open pro file and VTK include path, add used libs.
INCLUDEPATH += ./work/include/vtk-8.2 ... LIBS += -L/usr/local/lib/ -lvtkChartsCore-8.2.1 LIBS += -L/usr/local/lib/ -lvtkCommonColor-8.2.1 LIBS += -L/usr/local/lib/ -lvtkCommonComputationalGeometry-8.2.1 LIBS += -L/usr/local/lib/ -lvtkCommonCore-8.2.1 ...
-
3, setQSurfaceFormat
#include "mainwindow.h" #include <QApplication> #include <vtkQImageToImageSource.h> #include <vtkSmartPointer.h> #include <QLabel> #include <QVTKOpenGLNativeWidget.h> int main(int argc, char *argv[]) { QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat()); QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); }
-
4, Create a QVTKOpenGLWidget
//step 0: set it to a window: auto vw = new QVTKOpenGLWidget(this) ; vtkRenderWindow *renWin = vw->GetRenderWindow(); ui->tabWidget->addTab( vw, __FUNCTION__);
vtkRenderer *render= vtkRenderer::New();
render->SetBackground( 0.1, 0.2, 0.4 );
vtkSmartPointer
coneSource = vtkSmartPointer ::New(); coneSource->SetRadius(.2); coneSource->SetHeight(.5); coneSource->SetCenter(0, 0, 0); vtkSmartPointer<vtkPolyDataMapper> coneMapper = vtkSmartPointer<vtkPolyDataMapper>::New(); coneMapper->SetInputConnection(coneSource->GetOutputPort()); vtkSmartPointer<vtkActor> coneActor = vtkSmartPointer<vtkActor>::New(); coneActor->SetMapper(coneMapper); vtkSmartPointer<vtkActor> oriConeActor = vtkSmartPointer<vtkActor>::New(); oriConeActor->SetMapper(coneMapper);
render->AddActor(oriConeActor); renWin->AddRenderer( render );
-
5, Build and run You will get:
You can download the whole source code from Course-2