问题描述:
项目编译,报错,无法打开包括文件:“qvtkwidget.h”: No such file or directory
造成的原因:
1、VTK7.x早期版本中,官方仅提供QVTKWidget,需要手动将QVTKWidgetPlugin.dll和QVTKWidgetPlugin.lib放到qt design下的plugin里。
2、在Qt版本为5.4之后时,在VTK8.2之前的版本,一般是使用QVTKOpenGLWidget;在VTK8.2及之后的版本里,引入了QVTKOpenGLNativeWidget;
3、VTK9.0版本删除了QVTKWidget,但是有QVTKOpenGLNativeWidget,QVTKOpenGLStereoWidget,QVTKOpenGLWindow。
QVTKOpenGLNativeWidget在新版本源码的头文件路径是:
v9.0.3
/vtk/vtk/-/blob/v9.0.3/GUISupport/Qt/QVTKOpenGLNativeWidget.h
v9.1.0
/vtk/vtk/-/blob/v9.1.0/GUISupport/Qt/QVTKOpenGLNativeWidget.h
解决办法:
1、我们一般使用QVTKOpenGLNativeWidget来代替旧版本的QVTKWidget。新版本不再像QVTKWidget以前那样有所谓的Plugin.dll和Plugin.lib了。
2、新旧版本的详细的头文件和函数代替的情况如下:
注释掉的是旧版本,没注释的是新版本
//#include <QVTKWidget.h>#include <QVTKOpenGLNativeWidget.h>//class VtkWidget : public QVTKWidgetclass VtkWidget : public QVTKOpenGLNativeWidget//m_vtkWidget->GetInteractor()m_vtkWidget->interactor()//m_vtkWidget->GetRenderWindow()m_vtkWidget->renderWindow()//m_vtkWidget->SetRenderWindow()m_vtkWidget->setRenderWindow()//#define vtkRenderingCore_AUTOINIT 3(vtkInteractionStyle, vtkRenderingFreeType, vtkRenderingOpenGL)#define vtkRenderingCore_AUTOINIT 3(vtkInteractionStyle, vtkRenderingFreeType, vtkRenderingOpenGL2)//#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL)#define vtkRenderingVolume_AUTOINIT 1(vtkRenderingVolumeOpenGL2)//VTK_MODULE_INIT(vtkRenderingOpenGL)VTK_MODULE_INIT(vtkRenderingOpenGL2)//VTK_MODULE_INIT(vtkRenderingContextOpenGL)VTK_MODULE_INIT(vtkRenderingContextOpenGL2)
3、QVTKOpenGLNativeWidget怎么在Qt使用起来?
打开Qt设计师界面,先手动拖入一个普通的Widget控件,然后再提升这个窗口部件
提升之后:
源码举例,功能是打开*.stl文件,并显示在QVTKOpenGLNativeWidget控件上
头文件
#ifndef MAINWINDOW_H#define MAINWINDOW_H#include <QDebug>#include <QMainWindow>#include <vtkAutoInit.h>#include <vtkBMPReader.h>#include <vtkFloatArray.h>#include <vtkGenericOpenGLRenderWindow.h>#include <vtkImageActor.h>#include <vtkImageChangeInformation.h>#include <vtkImageData.h>#include <vtkImageImport.h>#include <vtkImageViewer2.h>#include <vtkInteractorStyleImage.h>#include <vtkJPEGReader.h>#include <vtkLookupTable.h>#include <vtkMetaImageReader.h>#include <vtkNamedColors.h>#include <vtkNew.h>#include <vtkPointData.h>#include <vtkPolyDataMapper.h>#include <vtkProperty.h>#include <vtkRenderWindow.h>#include <vtkRenderWindowInteractor.h>#include <vtkRenderer.h>#include <vtkSTLReader.h>#include <vtkSmartPointer.h>#include <vtkXMLImageDataWriter.h>VTK_MODULE_INIT(vtkRenderingOpenGL2)VTK_MODULE_INIT(vtkInteractionStyle)namespace Ui{class MainWindow;}class MainWindow : public QMainWindow{Q_OBJECTpublic:explicit MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void openFileSlot();private:Ui::MainWindow *ui;};#endif // MAINWINDOW_H
源文件
#include "mainwindow.h"#include "ui_mainwindow.h"#include <QDebug>#include <QFileDialog>#include <vtkImageBlend.h>#include <vtkImageCanvasSource2D.h>#include <vtkImageChangeInformation.h>#include <vtkImageData.h>#include <vtkImageIterator.h>#include <vtkImageLuminance.h>#include <vtkImageStencil.h>#include <vtkImageStencilData.h>#include <vtkImageViewer.h>#include <vtkInteractorStyleImage.h>#include <vtkPNGReader.h>#include <vtkRenderWindow.h>#include <vtkRenderWindowInteractor.h>MainWindow::MainWindow(QWidget *parent): QMainWindow(parent),ui(new Ui::MainWindow){ui->setupUi(this);connect(ui->pushButton, SIGNAL(clicked(bool)), this, SLOT(openFileSlot()));}MainWindow::~MainWindow(){delete ui;}void MainWindow::openFileSlot(){QString selectFilePath = QFileDialog::getOpenFileName(this, QString("选择STL文件"), QString(""), QString("文件(*.stl)"));if (selectFilePath.isEmpty()){ui->textBrowser->append("选择STL文件路径为空!");return;}// 原始图像vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();reader->SetFileName(selectFilePath.toStdString().c_str());reader->Update();//将source转换成mappervtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();mapper->SetInputConnection(reader->GetOutputPort());//送入渲染引擎进行显示vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();actor->SetMapper(mapper);//渲染vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();renderer->AddActor(actor);renderer->SetBackground(.0, .0, .0);//设置渲染窗口vtkSmartPointer<vtkGenericOpenGLRenderWindow> window = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();ui->qvtkWidget->setRenderWindow(window);ui->qvtkWidget->renderWindow()->AddRenderer(renderer); //等价于window->AddRenderer(renderer);//okui->textBrowser->append(QString("载入文件:") + selectFilePath + QString(" 成功 !"));}
更详细的解释
1、QVTKWidget、QVTKOpenGLWidget、QVTKOpenGLNativeWidget、QVTKWidget2 区别
/a15005784320/article/details/99460999
2、VTK笔记-Qt5.12.11编译VTK9.0.3-QVTKOpenGLNativeWidget【推荐阅读】
/liushao1031177/article/details/118719782
推荐若干VTK博客及开源案例
Beyond欣
/a15005784320/article/details/104855111
阿兵-AI医疗
/webzhuce/category_9268767.html
黑山老妖的博客
/liushao1031177
/lorensen/VTKExamples
/kimtaikee/QVTKDemo
/kimtaikee/QVTKDemo_data
如果觉得《VTK v9.0新版本找不到QVTKWidget.h头文件了?Qt设计师怎么办?》对你有帮助,请点赞、收藏,并留下你的观点哦!