轉載:http://blog.csdn.net/wokaowokaowokao12345/article/details/51314439
前言
上篇博客針對PCL1.6+Qt4+VS2010(http://blog.csdn.net/wokaowokaowokao12345/article/details/51078495)撰寫,在過度到PCL1.8+Qt5+VS2013時由於庫的更新和軟件版本變化,產生了一些小問題,現在針對PCL1.8+Qt5+VS2013遇到的問題進行講解。
過程
主要過程和上篇博客一致,對於變化的地方和出現的錯誤會單獨講解。
錯誤1:PCL1.8.0\3rdParty\VTK\include\vtk-7.0\QVTKWidget.h(41): fatal error C1083: 無法打開包括文件: “QWidgets”: No such file or directory
解決方法1:由錯誤信息定位到QQVTKWidget.h在41行做如下修改:
//#include <QWidgets> //Qt4中寫法
#include <QtWidgets/QWidget> //Qt5中寫法
- 1
- 2
- 1
- 2
說明:Qt5將大部分桌面部件移到了Qt Widgets模塊中。在源文件中,凡是涉及到原QtGui中部件的,全要修改頭文件引用,或者增加QtWidgets頭文件,比如:
#include <QWidgets> 改爲#include <QtWidgets/QWidget>
#include <QtGui/QMainWindow> 改爲#include <QtWidgets/QMainWindow>
Qt::WFlags改爲Qt::WindowFlags
- 1
- 2
- 3
- 1
- 2
- 3
錯誤2:no override found for ‘vtkRenderWindow’錯誤解決方法
解決方法2:在第一次使用vtk的頭文件最前添加下面代碼。
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
- 1
- 2
- 3
- 1
- 2
- 3
錯誤3:error C2653: “sensor_msgs”: 不是類或命名空間名稱
解決方法3:sensor_msgs改爲pcl
說明:在PCL1.8中將PointCloud2加入了pcl名字空間,sensor_msgs是舊的方式。
錯誤4:error C2440: “static_cast”: 無法從“vtkObjectBase *const ”轉換爲“vtkRenderWindow *”
解決方法4:在pclvisualizer.cpp文件裏添加#include <vtkRenderWindow.h>
頭文件和源文件
pclvisualizer.h
#ifndef PCLVISUALIZER_H
#define PCLVISUALIZER_H
#include <vtkAutoInit.h>
VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
#include <QtWidgets/QMainWindow>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>
#include "ui_pclvisualizer.h"
class PCLVisualizer : public QMainWindow
{
Q_OBJECT
public:
PCLVisualizer(QWidget *parent = 0);
~PCLVisualizer();
private:
Ui::PCLVisualizerClass ui;
//點雲數據存儲
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud;
boost::shared_ptr<pcl::visualization::PCLVisualizer> viewer;
//初始化vtk部件
void initialVtkWidget();
private slots:
//創建打開槽
void onOpen();
};
#endif // PCLVISUALIZER_H
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
pclvisualizer.cpp
#include <QFileDialog>
#include <iostream>
#include <vtkRenderWindow.h>
#include "pclvisualizer.h"
PCLVisualizer::PCLVisualizer(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//初始化
initialVtkWidget();
//連接信號和槽
connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(onOpen()));
}
PCLVisualizer::~PCLVisualizer()
{
}
void PCLVisualizer::initialVtkWidget()
{
cloud.reset(new pcl::PointCloud<pcl::PointXYZ>);
viewer.reset(new pcl::visualization::PCLVisualizer("viewer", false));
viewer->addPointCloud(cloud, "cloud");
ui.qvtkWidget->SetRenderWindow(viewer->getRenderWindow());
viewer->setupInteractor(ui.qvtkWidget->GetInteractor(), ui.qvtkWidget->GetRenderWindow());
ui.qvtkWidget->update();
}
//讀取文本型和二進制型點雲數據
void PCLVisualizer::onOpen()
{
//只能打開PCD文件
QString fileName = QFileDialog::getOpenFileName(this,
tr("Open PointCloud"), ".",
tr("Open PCD files(*.pcd)"));
if (!fileName.isEmpty())
{
std::string file_name = fileName.toStdString();
//sensor_msgs::PointCloud2 cloud2;
pcl::PCLPointCloud2 cloud2;
//pcl::PointCloud<Eigen::MatrixXf> cloud2;
Eigen::Vector4f origin;
Eigen::Quaternionf orientation;
int pcd_version;
int data_type;
unsigned int data_idx;
int offset = 0;
pcl::PCDReader rd;
rd.readHeader(file_name, cloud2, origin, orientation, pcd_version, data_type, data_idx);
if (data_type == 0)
{
pcl::io::loadPCDFile(fileName.toStdString(), *cloud);
}
else if (data_type == 2)
{
pcl::PCDReader reader;
reader.read<pcl::PointXYZ>(fileName.toStdString(), *cloud);
}
viewer->updatePointCloud(cloud, "cloud");
viewer->resetCamera();
ui.qvtkWidget->update();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
main.cpp
#include "pclvisualizer.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
PCLVisualizer w;
w.show();
return a.exec();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
效果