Environment:
macOS Mojave 10.14
Qt Creator 4.41
Qt 5.9.2
Clang 7.0 apple, 64 bits
VTK 8.1.1

Our project shows:

Add 3D model in VTK widget means we need to intergrate VTK and Qt development environment. Here are a few notes about it.

I put a cone in QVTKOpenGLWidget, and there is a button on the right side.
There are many problems when using QVTKOpenGLWidget for the first time. We have many setting for it comparing to QVTKWidget.
I wrote a relative article about it at CSDN, here is a link:
QVTKOpenGLWidget使用问题记录

A few important code file shows below:
CMakeLists.txt

cmake_minimum_required(VERSION 2.8)

project(vtkInQtExample)

find_package( VTK REQUIRED )
include( ${VTK_USE_FILE} )

# add header files which are generated by qt
include_directories( ${CMAKE_CURRENT_BINARY_DIR} )

file(GLOB UI_FILES *.ui)
file(GLOB HEADER_FILES
    *.h
    *.hpp
    )
file(GLOB CXX_FILES
    *.cxx
    *.cpp
    )

if(${VTK_VERSION} VERSION_GREATER "6" AND VTK_QT_VERSION VERSION_GREATER "4")
  qt5_wrap_ui(UISrcs ${UI_FILES} )
  # CMAKE_AUTOMOC in ON so the MOC headers will be automatically wrapped.
  set( CMAKE_AUTOMOC ON )
  add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${HEADER_FILES} ${CXX_FILES} ${UISrcs} )
  qt5_use_modules(${PROJECT_NAME} Core Gui)
else()
  QT4_WRAP_UI(UISrcs ${UI_FILES})
  QT4_WRAP_CPP(MOCSrcs ${HEADER_FILES})
  add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${CXX_FILES} ${UISrcs} ${MOCSrcs})
endif()

target_link_libraries( ${PROJECT_NAME} ${VTK_LIBRARIES} )

main.cpp

#include "widget.h"
#include 

#include 
#include 

int main(int argc, char *argv[])
{
    // needed to ensure appropriate OpenGL context is created for VTK rendering.
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());

    QApplication a(argc, argv);
    Widget w;
    w.show();
    return a.exec();
}

widget.h

#include "widget.h"
#include "ui_widget.h"

#include 

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget),
    coneActor( vtkSmartPointer::New() )
{
    ui->setupUi(this);

    vtkSmartPointer cone =
            vtkSmartPointer::New();

    vtkSmartPointer coneMapper =
            vtkSmartPointer::New();
    coneMapper->SetInputConnection( cone->GetOutputPort() );
    coneActor->SetMapper( coneMapper );
    vtkSmartPointer backRenderer =
            vtkSmartPointer::New();
    backRenderer->AddActor( coneActor );
    backRenderer->SetBackground( 1, 1, 1 );

    vtkSmartPointer backWindow =
            vtkSmartPointer::New();
    backWindow->AddRenderer( backRenderer );

    ui->qvtkWidget->SetRenderWindow( backWindow );
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_pushButton_clicked()
{

}

All code files are uploaded to GitHub:
https://github.com/theArcticOcean/CLib/tree/master/VTKLearn/vtkInQtExample


1 Comment

Use CMake To Build Qt5 Project - theArcticOcean · 2019年7月20日 at pm5:51

[…] have an example which uses cmake to build the qt5 project: use-vtk-in-qt-widget-example But that workflow seems a cheat, I used Qt Creator to create a qmake project and wrote […]

Leave a Reply

Your email address will not be published. Required fields are marked *

You cannot copy content of this page