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:




I put a cone in QVTKOpenGLWidget, and there is a button at right side.
There are many problems when using QVTKOpenGLWidget at first time. We has many setting for it comparing to QVTKWidget.
I wrote relative airticle about it at CSDN, here is 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 <QApplication>

#include <qsurfaceformat.h>
#include <QVTKOpenGLWidget.h>

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 <vtkGenericOpenGLRenderWindow.h>

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

    vtkSmartPointer<vtkConeSource> cone =
            vtkSmartPointer<vtkConeSource>::New();

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

    vtkSmartPointer<vtkGenericOpenGLRenderWindow> backWindow =
            vtkSmartPointer<vtkGenericOpenGLRenderWindow>::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

分类: C plus plus

发表评论

电子邮件地址不会被公开。 必填项已用*标注