1. Qt 安装配置
- 从 Index of /archive/qt 中下载 Qt, 假设版本为 5.14.0,并安装在 /opt 路径下;
- 设置系统默认 Qt 版本;
# 查看系统默认 qt 版本
qmake -v
# 修改系统默认 qt 版本
cd /usr/lib/x86_64-linux-gnu/qt-default/qtchooser
sudo vim default.conf
# 将下面内容
/usr/lib/x86_64-linux-gnu/qt4/bin
/usr/lib/x86_64-linux-gnu
# 修改为
/opt/Qt5.14.0/5.14.0/gcc_64/bin
/opt/Qt5.14.0/5.14.0/gcc_64
# 再次查看 qt 版本
qmake -v
- 添加到环境变量
sudo vim /etc/profile
# 添加如下内容
export PATH=/opt/Qt5.14.0/Tools/QtCreator/bin:$PATH
export PATH=/opt/Qt5.14.0/5.14.0/gcc_64/bin:$PATH
# 刷新环境
sudo source /etc/profile
2. VTK 安装配置
- 从 https://vtk.org/download/ 下载 VTK 源代码并解压,假设版本为 8.2.0,解压路径为
~/software
; - 编译安装;
cd ~/software/VTK-8.2.0
mkdir build && cd build
cmake -DVTK_QT_VERSION:STRING=5
-DVTK_Group_Qt:BOOL=ON
-DBUILD_SHARED_LIBS:BOOL=ON
-DQT_QMAKE_EXECUTABLE:PATH=/opt/Qt5.14.0/5.14.0/gcc_64/bin/qmake
-DCMAKE_PREFIX_PATH:PATH=/opt/Qt5.14.0/5.14.0/gcc_64/lib/cmake ..
make -j8
sudo make install
- 生成 QVTKWidget 控件
cd ~/software/VTK-8.2.0/build/lib
sudo cp ./libQVTKWidgetPlugin.so /opt/Qt5.14.0/5.14.0/gcc_64/plugins/designer
sudo cp ./libQVTKWidgetPlugin.so /opt/Qt5.14.0/Tools/QtCreator/lib/Qt/plugins/designer
重启 Qt 软件,便能看到 QVTKWidget 控件了。

3. PCL + Qt + VTK 实践
参考:Create a PCL visualizer in Qt with cmake
- 从 Github 中下载源码(或者从自己本地的 PCL 目录中拷贝,
pcl/doc/tutorials/content/sources/qt_visualizer
)
项目目录结构如下:
├── build
└── src
├── CMakeLists.txt
├── main.cpp
├── pclviewer.cpp
├── pclviewer.h
├── pclviewer.ui
└── pcl_visualizer.pro
- 在 Qt 中打开此项目,并进行配置:在左侧边栏选择 projects,按下图进行配置,主要是将 qmake 换成 cmake,然后配置路径等;

- 运行结果如下,实现的效果是可以改变点云的颜色和大小

pclviewer.h
#ifndef PCLVIEWER_H
#define PCLVIEWER_H
#include <iostream>
#include <QMainWindow>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
// Visualization Toolkit (VTK)
#include <vtkRenderWindow.h>
typedef pcl::PointXYZRGBA PointT;
typedef pcl::PointCloud<PointT> PointCloudT;
namespace Ui {class PCLViewer;}
class PCLViewer : public QMainWindow
{
Q_OBJECT
public:
explicit PCLViewer (QWidget *parent = 0);
~PCLViewer ();
public Q_SLOTS:
void randomButtonPressed ();
void RGBsliderReleased ();
void pSliderValueChanged (int value);
void redSliderValueChanged (int value);
void greenSliderValueChanged (int value);
void blueSliderValueChanged (int value);
protected:
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
PointCloudT::Ptr cloud;
unsigned int red;
unsigned int green;
unsigned int blue;
private:
Ui::PCLViewer *ui;
};
#endif // PCLVIEWER_H
pclviewer.cpp
#include "pclviewer.h"
#include "ui_pclviewer.h"
PCLViewer::PCLViewer (QWidget *parent) : QMainWindow (parent), ui (new Ui::PCLViewer)
{
ui->setupUi (this);
this->setWindowTitle ("PCL viewer");
// Setup the cloud pointer
cloud.reset (new PointCloudT);
// The number of points in the cloud
cloud->points.resize (200);
// The default color
red = 128; green = 128; blue = 128;
// Fill the cloud with some points
for (size_t i = 0; i < cloud->points.size (); ++i)
{
cloud->points[i].x = 1024 * rand () / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024 * rand () / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024 * rand () / (RAND_MAX + 1.0f);
cloud->points[i].r = red;
cloud->points[i].g = green;
cloud->points[i].b = blue;
}
// Set up the QVTK window
viewer.reset (new pcl::visualization::PCLVisualizer ("viewer", false));
ui->qvtkWidget->SetRenderWindow (viewer->getRenderWindow ());
viewer->setupInteractor (ui->qvtkWidget->GetInteractor (), ui->qvtkWidget->GetRenderWindow ());
ui->qvtkWidget->update ();
// Connect "random" button and the function
connect (ui->pushButton_random, SIGNAL (clicked ()), this, SLOT (randomButtonPressed ()));
// Connect R,G,B sliders and their functions
connect (ui->horizontalSlider_R, SIGNAL (valueChanged (int)), this, SLOT (redSliderValueChanged (int)));
connect (ui->horizontalSlider_G, SIGNAL (valueChanged (int)), this, SLOT (greenSliderValueChanged (int)));
connect (ui->horizontalSlider_B, SIGNAL (valueChanged (int)), this, SLOT (blueSliderValueChanged (int)));
connect (ui->horizontalSlider_R, SIGNAL (sliderReleased ()), this, SLOT (RGBsliderReleased ()));
connect (ui->horizontalSlider_G, SIGNAL (sliderReleased ()), this, SLOT (RGBsliderReleased ()));
connect (ui->horizontalSlider_B, SIGNAL (sliderReleased ()), this, SLOT (RGBsliderReleased ()));
// Connect point size slider
connect (ui->horizontalSlider_p, SIGNAL (valueChanged (int)), this, SLOT (pSliderValueChanged (int)));
viewer->addPointCloud (cloud, "cloud");
pSliderValueChanged (2);
viewer->resetCamera ();
ui->qvtkWidget->update ();
}
void PCLViewer::randomButtonPressed ()
{
printf ("Random button was pressedn");
// Set the new color
for (size_t i = 0; i < cloud->size(); i++)
{
cloud->points[i].r = 255 *(1024 * rand () / (RAND_MAX + 1.0f));
cloud->points[i].g = 255 *(1024 * rand () / (RAND_MAX + 1.0f));
cloud->points[i].b = 255 *(1024 * rand () / (RAND_MAX + 1.0f));
}
viewer->updatePointCloud (cloud, "cloud");
ui->qvtkWidget->update ();
}
void PCLViewer::RGBsliderReleased ()
{
// Set the new color
for (size_t i = 0; i < cloud->size (); i++)
{
cloud->points[i].r = red;
cloud->points[i].g = green;
cloud->points[i].b = blue;
}
viewer->updatePointCloud (cloud, "cloud");
ui->qvtkWidget->update ();
}
void PCLViewer::pSliderValueChanged (int value)
{
viewer->setPointCloudRenderingProperties (pcl::visualization::PCL_VISUALIZER_POINT_SIZE, value, "cloud");
ui->qvtkWidget->update ();
}
void PCLViewer::redSliderValueChanged (int value)
{
red = value;
printf ("redSliderValueChanged: [%d|%d|%d]n", red, green, blue);
}
void PCLViewer::greenSliderValueChanged (int value)
{
green = value;
printf ("greenSliderValueChanged: [%d|%d|%d]n", red, green, blue);
}
void PCLViewer::blueSliderValueChanged (int value)
{
blue = value;
printf("blueSliderValueChanged: [%d|%d|%d]n", red, green, blue);
}
PCLViewer::~PCLViewer ()
{
delete ui;
}
在某些应用场合中会出现以下错误:
error while loading shared libraries: libvtkCommonCore-8.2.so.1: cannot open shared object file: No such file or directory
但我发现在 /usr/local/lib 目录下是有 libvtkCommonCore-8.2.so.1 这个共享文件的,应该是没有将 /usr/local/lib 这个路径加入到共享库配置文件(/etc/ld.so.conf)中。
cat /etc/ld.so.conf
# 显示:include ld.so.conf.d/*.conf
# 修改权限
sudo chmod 777 /etc/ld.so.conf
# 将 /usr/local/lib 路径追加到 /etc/ld.so.conf 文件中
sudo echo "/usr/local/lib" >> /etc/ld.so.conf
sudo ldconfig
参考:"error while loading shared libraries: xxx.so.x" 错误的原因和解决方法