1、编译VTK-7.1.0
下载VTK,解压后在解压文件中创建build文件
mkdir build
cd build
用cmake进行编译,注意一定要编译release版本,否则不会生成QVTKWidget所需要的动态库libQVTKWidgetPlugin.so
编译命令如下:
cmake -DVTK_QT_VERSION:STRING=5
-DQT_QMAKE_EXECUTABLE:PATH=/opt/Qt5.11.2/5.11.2/gcc_64/bin/qmake
-DVTK_Group_Qt:BOOL=ON
-DCMAKE_PREFIX_PATH:PATH=/opt/Qt5.11.2/5.11.2/gcc_64/lib/cmake
-DCMAKE_BUILD_TYPE=RELEASE
-DBUILD_SHARED_LIBS:BOOL=ON ..
make -j
sudo make install
参考官方教程:https://vtk.org/Wiki/VTK/Building/Linux
2、加载动态库
sudo find / -name libQVTKWidgetPlugin.so
动态链接库的路径。/opt/Qt5.11.2/5.11.2/gcc_64/plugins/designer
/opt/Qt5.11.2/Tools/QtCreator/lib/Qt/plugins/designer
即可。此时打开designer和QTCreator都会有对应的插件。效果如下图所示:
注意
- 当出现
undefined reference to `QVTKWidget::QVTKWidget(QWidget*, QFlags<Qt::WindowType>)'
的错误时请引入:libvtkGUISupportQt
这个库!
Qt VTK编程过程遇到的错误
1、QT编译时报如下错误:error while loading shared libraries: lib***.so
error while loading shared libraries: lib***.so.1: cannot open shared object file: No such file or directory
原因是没有设置环境变量,解决办法:
然后在ld.so.conf中添加动态链接库的目录(我的是),如下:
然后运行如下命令更新动态链接库目录:
2、编译时报:link error:no override found for ‘vtkRenderWindow'。
添加如下内容
#include <vtkAutoInit.h>
//VTK_MODULE_INIT(vtkRenderingOpenGL2); //这个必须要加,不然会报错
//VTK_MODULE_INIT(vtkInteractionStyle);
3、Warning: In D:VTKVTK-6.3.0RenderingCorevtkInteractorStyleSwitchBase.cxx, line 43
vtkInteractorStyleSwitchBase (00000293C389B540): Warning: Link to vtkInteractionStyle for default style selection.
添加:
VTK_MODULE_INIT(vtkInteractionStyle)
4、ERROR: In F:VTKVTK-7.0RenderingCorevtkTextMapper.cxx, line 543
vtkTextMapper (000001215D0AA770): Could not locate vtkTextRenderer object.
添加
VTK_MODULE_INIT(vtkRenderingFreeType)
5、.... VTK-7.1.1/GUISupport/Qt/QVTKInteractor.cxx, line 147 QVTKInteractor (0x240be90): QVTKInteractor cannot control the event loop.
代码如下:
renderWindowInteractor = ui->qvtkWidget->GetInteractor(); ui->qvtkWidget->GetInteractor()->SetRenderWindow(renderwind); style = vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New(); style->SetDefaultRenderer(renderer); ui->qvtkWidget->SetRenderWindow(renderwind); ui->qvtkWidget->GetRenderWindow()->GetInteractor()->SetInteractorStyle(style); ui->qvtkWidget->GetRenderWindow()->Render(); ui->qvtkWidget->GetRenderWindow()->GetInteractor()->Initialize(); ui->qvtkWidget->GetRenderWindow()->GetInteractor()->Start(); ui->qvtkWidget->update();
编译时报错:
ERROR: .../VTK-7.1.1/GUISupport/Qt/QVTKInteractor.cxx, line 147
QVTKInteractor (0x240be90): QVTKInteractor cannot control the event loop.
qvtkWidget会在创建时完成Interactor创建和开启,保证其对窗口的控制,因此
ui->qvtkWidget->GetRenderWindow()->GetInteractor()->Initialize();
ui->qvtkWidget->GetRenderWindow()->GetInteractor()->Start();
这两句话是不必要的。删除这两句话后,程序不再产生vtk错误窗口。
同样在QVTKOpenGLWidget也会出这样的问题。