[vtkusers] Trouble using VTK Widget with QVTK
andyjk
andrewkeeling at hotmail.com
Sat Jul 7 03:15:09 EDT 2018
I'm still having no luck and suspect this is a bug in VTK, when using
QVTKOpenGLWidget.
The full QT mainwindow.cpp code is pasted below with the various lines that
don't work in comments.
I've tried every combination and nothing worked - how do I report this as a
bug ?
Thanks!
#include "mainwindow.h"
#include "build/ui_mainwindow.h"
#include <vtkVersion.h>
#include "vtkSmartPointer.h"
#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCellArray.h"
#include "vtkImageDataGeometryFilter.h"
#include "vtkPoints.h"
#include "vtkPolyData.h"
#include "vtkPolyDataCollection.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkSphereSource.h"
#include "vtkTriangleFilter.h"
#include <vtkRendererCollection.h>
#include "vtkContourWidget.h"
#include "vtkOrientedGlyphContourRepresentation.h"
#include "vtkPolygonalSurfacePointPlacer.h"
#include "vtkPolygonalSurfaceContourLineInterpolator.h"
#include <vtkGenericOpenGLRenderWindow.h>
#include <QVTKWidget.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
vtkSmartPointer<vtkPolyData> polyData;
vtkSmartPointer<vtkSphereSource> sphereSource =
vtkSmartPointer<vtkSphereSource>::New();
sphereSource->SetThetaResolution(40);
sphereSource->SetPhiResolution(20);
sphereSource->Update();
polyData = sphereSource->GetOutput();
// The Dijkistra interpolator will not accept cells that aren't triangles
vtkSmartPointer<vtkTriangleFilter> triangleFilter =
vtkSmartPointer<vtkTriangleFilter>::New();
#if VTK_MAJOR_VERSION <= 5
triangleFilter->SetInput(polyData);
#else
triangleFilter->SetInputData(polyData);
#endif
triangleFilter->Update();
vtkSmartPointer<vtkPolyData> pd = triangleFilter->GetOutput();
//Create a mapper and actor
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(triangleFilter->GetOutputPort());
vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
actor->GetProperty()->SetInterpolationToFlat();
// Create the render window, renderer and interactor.
vtkSmartPointer<vtkRenderer> renderer =
vtkSmartPointer<vtkRenderer>::New();
/////////////////////////////////////////////////////////////////////////////////////////////////////
// OPTION 1: Use this for a plain non-QT VTK window. The
Polygonalsurfaceinteractor works fine
//vtkSmartPointer<vtkRenderWindow> renderWindow =
vtkSmartPointer<vtkRenderWindow>::New();
// OPTION 2: ....but if we use this instead and set the renderwindow of the
qvtkOpenGL widget to this, the polygonalsurfaceintaeractor does not work
// although at least the sphere displays. We cannot move the camera though
// Remember to comment out the interactor->Start() line at the bottom of
the code, or everything freezes with no display!
//vtkNew<vtkGenericOpenGLRenderWindow> renderWindow;
vtkSmartPointer<vtkRenderWindow> renderWindow =
ui->widget->GetRenderWindow();
/////////////////////////////////////////////////////////////////////////////////////////////////////
ui->widget->GetRenderWindow()->AddRenderer(renderer);
ui->widget->GetRenderWindow()->Render();
ui->widget->update();
/////////////////////////////////////////////////////////////////////////////////////////////////////
// OPTION A: If we grab the interactor from the QVTKOpenGL widget, then at
least we can move the camera around, but we cannot use the polygonalsurface
placer
vtkSmartPointer<vtkRenderWindowInteractor> interactor =
ui->widget->GetInteractor();
// OPTION B: .....alternatively, if we set a new interactor and try to use
QVTKOpenGL, no interaction is possible.
// Note We must set a new interactor if we are using the pure non-QT VTK
window (OPTION 1 above)
//vtkSmartPointer<vtkRenderWindowInteractor> interactor =
vtkSmartPointer<vtkRenderWindowInteractor>::New();
/////////////////////////////////////////////////////////////////////////////////////////////////////
//interactor->SetRenderWindow(ui->widget->GetRenderWindow());
// Add the actors to the renderer, set the background and size
renderer->AddActor(actor);
renderer->SetBackground(.3, .4, .5);
renderer->ResetCamera();
// Here comes the contour widget stuff...
vtkSmartPointer<vtkContourWidget> contourWidget =
vtkSmartPointer<vtkContourWidget>::New();
contourWidget->SetInteractor(interactor);
vtkSmartPointer<vtkOrientedGlyphContourRepresentation> rep =
vtkOrientedGlyphContourRepresentation::SafeDownCast(
contourWidget->GetRepresentation());
rep->GetLinesProperty()->SetColor(1, 0.2, 0);
rep->GetLinesProperty()->SetLineWidth(3.0);
vtkSmartPointer<vtkPolygonalSurfacePointPlacer> pointPlacer =
vtkSmartPointer<vtkPolygonalSurfacePointPlacer>::New();
pointPlacer->AddProp(actor);
pointPlacer->GetPolys()->AddItem(pd);
rep->SetPointPlacer(pointPlacer);
vtkSmartPointer<vtkPolygonalSurfaceContourLineInterpolator> interpolator =
vtkSmartPointer<vtkPolygonalSurfaceContourLineInterpolator>::New();
interpolator->GetPolys()->AddItem(pd);
rep->SetLineInterpolator(interpolator);
//renderWindow->Render();
//interactor->Initialize();
contourWidget->EnabledOn();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// OPTION 1: We have to uncomment this for the pure VTK version
// OPTION 2: ....But comment it out if using the QVTKOpenGL Widget,
otherwise everything freezes with no rendering
//interactor->Start();
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
MainWindow::~MainWindow()
{
delete ui;
}
--
Sent from: http://vtk.1045678.n5.nabble.com/VTK-Users-f1224199.html
More information about the vtkusers
mailing list