[vtkusers] QVTKOpenGLWidget usage with QtCreator

Elvis Stansvik elvis.stansvik at orexplore.com
Sat Aug 26 08:05:31 EDT 2017


2017-08-26 14:01 GMT+02:00 Elvis Stansvik <elvis.stansvik at orexplore.com>:
> Here is a minimal example that works here (VTK 8.0.0, Qt 5.5.1). The
> result is as attached. I hope that helps.
>
> If you're using the multiple inheritance approach (like I'm doing
> here), perhaps you were forgetting to call setupUi?
>
> Elvis
>
>
> example.cpp:
>
> #include "MainWindow.h"
>
> #include <QApplication>
> #include <QSurfaceFormat>
>
> #include <QVTKOpenGLWidget.h>
>
> int main(int argc, char *argv[]) {
>   QSurfaceFormat::setDefaultFormat(QVTKOpenGLWidget::defaultFormat());
>
>   QApplication app(argc, argv);
>
>   MainWindow window;
>   window.show();
>
>   return app.exec();
> }
>
>
> MainWindow.h:
>
> #pragma once
>
> #include "ui_MainWindow.h"
>
> class MainWindow : public QMainWindow, private Ui::MainWindow {
>   Q_OBJECT
>
> public:
>   explicit MainWindow(QWidget *parent = 0);
> };
>
>
> MainWindow.cpp:
>
> #include "MainWindow.h"
>
> #include <QVTKOpenGLWidget.h>
>
> #include <vtkCylinderSource.h>
> #include <vtkGenericOpenGLRenderWindow.h>
> #include <vtkPolyDataMapper.h>
> #include <vtkRenderer.h>
> #include <vtkSmartPointer.h>
>
> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
>   setupUi(this);
>
>   auto cylinderSource = vtkSmartPointer<vtkCylinderSource>::New();
>   cylinderSource->SetCenter(0.0, 0.0, 0.0);
>   cylinderSource->SetRadius(5.0);
>   cylinderSource->SetHeight(7.0);
>   cylinderSource->SetResolution(100);
>
>   auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
>   mapper->SetInputConnection(cylinderSource->GetOutputPort());
>
>   auto actor = vtkSmartPointer<vtkActor>::New();
>   actor->SetMapper(mapper);
>
>   auto renderer = vtkSmartPointer<vtkRenderer>::New();
>   renderer->AddActor(actor);
>
>   auto window = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
>   window->AddRenderer(renderer);
>
>   widget->SetRenderWindow(window);
> }
>
>
> MainWindow.ui:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <ui version="4.0">
>  <class>MainWindow</class>
>  <widget class="QMainWindow" name="MainWindow">
>   <property name="geometry">
>    <rect>
>     <x>0</x>
>     <y>0</y>
>     <width>800</width>
>     <height>600</height>
>    </rect>
>   </property>
>   <property name="windowTitle">
>    <string>MainWindow</string>
>   </property>
>   <widget class="QWidget" name="centralwidget">
>    <layout class="QVBoxLayout" name="verticalLayout">
>     <item>
>      <widget class="QVTKOpenGLWidget" name="widget" native="true"/>
>     </item>
>    </layout>
>   </widget>
>   <widget class="QMenuBar" name="menubar">
>    <property name="geometry">
>     <rect>
>      <x>0</x>
>      <y>0</y>
>      <width>800</width>
>      <height>19</height>
>     </rect>
>    </property>
>   </widget>
>   <widget class="QStatusBar" name="statusbar"/>
>  </widget>
>  <customwidgets>
>   <customwidget>
>    <class>QVTKOpenGLWidget</class>
>    <extends>QWidget</extends>
>    <header>QVTKOpenGLWidget.h</header>
>    <container>1</container>
>   </customwidget>
>  </customwidgets>
>  <resources/>
>  <connections/>
> </ui>

I forgot the CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)

project(example)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt5Widgets REQUIRED)
find_package(VTK 8.0.0 REQUIRED COMPONENTS
    vtkCommonCore
    vtkFiltersSources
    vtkGUISupportQt
    vtkInteractionStyle
    vtkRenderingCore
    vtkRenderingOpenGL2
)

add_executable(example WIN32 example.cpp MainWindow.cpp)

target_link_libraries(example PRIVATE
    Qt5::Widgets
    vtkCommonCore
    vtkFiltersSources
    vtkGUISupportQt
    vtkInteractionStyle
    vtkRenderingCore
    vtkRenderingOpenGL2
)

target_include_directories(example SYSTEM PRIVATE
    ${CMAKE_CURRENT_BINARY_DIR}
    ${VTK_INCLUDE_DIRS}
)

target_compile_definitions(example PRIVATE
    ${VTK_DEFINITIONS}
)

set_target_properties(example PROPERTIES
    AUTOMOC ON
    AUTOUIC ON
)


>
>
> 2017-08-26 11:12 GMT+02:00 Mads M. Hansen <madsmh at gmail.com>:
>> I have been trying to use QVTKOpenGLWidget by promoting a QWiget in
>> QtCreator, but that seems to result in in a segfault. I have tried
>> searching for some documentation an found the class reference, but
>> that does not seem to address QtCreator.
>>
>> Thanks in advance.
>> _______________________________________________
>> Powered by www.kitware.com
>>
>> Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html
>>
>> Please keep messages on-topic and check the VTK FAQ at: http://www.vtk.org/Wiki/VTK_FAQ
>>
>> Search the list archives at: http://markmail.org/search/?q=vtkusers
>>
>> Follow this link to subscribe/unsubscribe:
>> http://public.kitware.com/mailman/listinfo/vtkusers


More information about the vtkusers mailing list