vs2013+qt5.51+vtk 實現的 雙view,顯示並聯動操作
阿新 • • 發佈:2019-02-15
1.首先我們必須將QVTKWidget加入到Qt Designer,只需找到編譯好的vtk的release版本下的QVTKWidgetPlugin.dll,QVTKWidgetPlugin.exp,QVTKWidgetPlugin.lib放入到自己QT下的designer下即可(我的目錄是:F:\Qt\Qt5.5.1\5.5\msvc2013\plugins\designer)
2.寫好CmakeLists.txt
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 ) PROJECT( qt ) #---------------------------------------------------------------------------------- FIND_PACKAGE( VTK REQUIRED ) FIND_PACKAGE( Qt5Widgets ) INCLUDE( ${VTK_USE_FILE} ) #---------------------------------------------------------------------------------- # 定義變數PROJECT_SRCS,其值為所列表的檔案列表,包含所有原始檔 SET( PROJECT_SRCS main.cpp ProjectMainWindow.cpp ProjectMainWindow.h ) # 定義變數PROJECT_UIS,其值為所列表的檔案列表,包含所有的ui檔案 SET( PROJECT_UIS ProjectMainWindow.ui ) # 定義變數PROJECT_MOC_HDRS,其值為所列表的檔案列表,包含所有Q_OBJECT的標頭檔案 SET( PROJECT_MOC_HDRS ProjectMainWindow.h ) #---------------------------------------------------------------------------------- # QT5_WRAP_UI通過QT的uic.exe生成UI檔案對應的ui_xxx.h檔案, # 並將生成的moc_xxx.h檔案列表儲存於變數PROJECT_UIS_H 裡 QT5_WRAP_UI( PROJECT_UIS_H ${PROJECT_UIS} ) # QT5_WRAP_CPP通過QT的moc.exe將包含的Q_OBJECT的檔案生成對應的moc_xxx.cxx檔案, # 並將生成的moc_xxx.cxx檔案列表儲存於變數PROJECT_MOC_SRCS裡 QT5_WRAP_CPP( PROJECT_MOC_SRCS ${PROJECT_MOC_HDRS} ) #---------------------------------------------------------------------------------- # Qt的MOC和UIC程式生成的moc_xxx.cxx和ui.xxx.h等檔案是存放在CMAKE的 # where to bulid the binaries 裡指定的目錄,所以必須把這些路徑包含進來 INCLUDE_DIRECTORIES( ${PROJECT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR} ${VTK_DIR} ) # Qt程式如果有資原始檔(*.qrc),要包含資原始檔, # qt5_add_resources 用的rcc.exe生成相應的qrc_xxx.cpp檔案, # 並將生成的檔案列表儲存於變數Project_RCC_SRCS裡 # set(Project_RCCS YourProjectName.qrc) # qt5_add_resources(Project_RCCS_SRCS ${Project_RCCS}) # 根據程式的cpp檔案、標頭檔案以及生成的ui_xxx.h、moc_xxx.cxx、qrc_xxx.cxx # 等生成可執行檔案,並連結Qt的動態庫(Qt的動態都定義在QT_LIBRARIES變數裡了) ADD_EXECUTABLE( qt ${PROJECT_SRCS} ${PROJECT_UIS_H} ${PROJECT_MOC_SRCS} ) TARGET_LINK_LIBRARIES ( qt ${VTK_LIBRARIES} #QVTK )
3.用cmake編譯
4.main函式
#include "ProjectMainWindow.h" #include <QTextCodec> //程式釋出時,可以不要註釋以下語句,編譯的時候就不會帶控制檯輸出視窗 //#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" ) int main( int argc, char **argv ) { QApplication *app = new QApplication(argc, argv); ProjectMainWindow *window = new ProjectMainWindow(); window->show(); return app->exec(); };
5.ProjectMainwindow.h
#ifndef Project_MainWindow_H #define Project_MainWindow_H #include <QMainWindow> #include "ui_ProjectMainWindow.h" #include <vtkSmartPointer.h> class vtkImageViewer2; class vtkRenderer; class vtkEventQtSlotConnect; class vtkObject; class vtkCommand; class ProjectMainWindow : public QMainWindow { Q_OBJECT public: ProjectMainWindow(QWidget *parent = 0); ~ProjectMainWindow(); private: Ui::ProjectMainWindowClass ui; private slots: //響應開啟影象檔案的槽函式 void onOpenSlot(); private: vtkSmartPointer< vtkRenderer > m_pRenderder_left; vtkSmartPointer< vtkRenderer > m_pRenderder_right; vtkEventQtSlotConnect* m_Connections; }; #endif
6.ProjectMainwindow.cpp
#include "ProjectMainWindow.h"
#include <QFileDialog>
#include <QDir>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkImageViewer2.h>
#include <QVTKWidget.h>
#include <vtkBMPReader.h>
#include <vtkJPEGReader.h>
#include <vtkSTLReader.h>
#include <vtkImageActor.h>
#include <qdebug.h>
#include "Header.h"
//#include "Header2.h"
#include "vtkEventQtSlotConnect.h"
#include "vtkCommand.h"
ProjectMainWindow::ProjectMainWindow(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
m_pRenderder_left = vtkSmartPointer< vtkRenderer >::New();
m_pRenderder_right = vtkSmartPointer< vtkRenderer >::New();
m_Connections = vtkEventQtSlotConnect::New();
// 設定m_QVTKWidget的渲染器
ui.qvtkWidget_1->GetRenderWindow()->AddRenderer(m_pRenderder_left);
ui.qvtkWidget_1->GetRenderWindow()->AddRenderer(m_pRenderder_right);
//連線開啟的訊號與相應的槽
connect(ui.actionOpen, SIGNAL(triggered()), this, SLOT(onOpenSlot()));
connect(ui.actionExit, SIGNAL(triggered()), this, SLOT(close()));
vtkSmartPointer<vtkRenderWindowInteractor>interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
vtkSmartPointer<myInteractorStyle> style = vtkSmartPointer<myInteractorStyle>::New();
style->SetLeftRender(m_pRenderder_left);
style->SetRightRender(m_pRenderder_right);
interactor->SetInteractorStyle(style);
//vtkSmartPointer<vtkCamera> camera = vtkSmartPointer<vtkCamera>::New();
//camera->SetPosition(4, 4, 4);
//m_pRenderder_left->SetActiveCamera(camera);
//m_pRenderder_right->SetActiveCamera(camera);
//vtkSmartPointer<vtkRenderWindowInteractor>interactor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
//vtkSmartPointer<myInteractorStyle> style = vtkSmartPointer<myInteractorStyle>::New();
//style->SetLeftRender(m_pRenderder_left);
//style->SetRightRender(m_pRenderder_right);
//style->SetCamera(camera);
//interactor->SetInteractorStyle(style);
ui.qvtkWidget_1->GetRenderWindow()->SetInteractor(interactor);
ui.qvtkWidget_1->GetRenderWindow()->Render();
}
ProjectMainWindow::~ProjectMainWindow()
{
m_Connections->Delete();
}
void ProjectMainWindow::onOpenSlot()
{
double leftViewStation[4] = { 0.0, 0.0, 0.5, 1.0 };
double rightViewStation[4] = { 0.5, 0.0, 1.0, 1.0 };
QString filter;
filter = "stl file (*.stl)";
QDir dir;
QString fileName = QFileDialog::getOpenFileName(this,
QString(tr("開啟影象")), dir.absolutePath(), filter);
if (fileName.isEmpty() == true) return;
// 支援帶中文路徑的讀取
QByteArray ba = fileName.toLocal8Bit();
const char *fileName_str = ba.data();
qDebug() << fileName_str << endl;
// 用vtkSTLReader讀取STL影象
vtkSmartPointer<vtkSTLReader> reader = vtkSmartPointer<vtkSTLReader>::New();
reader->SetFileName(fileName_str);
reader->Update();
vtkSmartPointer<vtkPolyDataMapper> mapper =
vtkSmartPointer<vtkPolyDataMapper>::New();
mapper->SetInputConnection(reader->GetOutputPort());
vtkSmartPointer<vtkActor> actor =
vtkSmartPointer<vtkActor>::New();
actor->SetMapper(mapper);
m_pRenderder_left->AddActor(actor);
m_pRenderder_left->ResetCamera();
m_pRenderder_left->DrawOn();
m_pRenderder_left->SetViewport(leftViewStation);
m_pRenderder_right->AddActor(actor);
m_pRenderder_right->ResetCamera();
m_pRenderder_right->DrawOn();
m_pRenderder_right->SetViewport(rightViewStation);
}
7.新建一個Header.h
#include <vtkSmartPointer.h>
#include <vtkCubeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkProperty.h>
#include <vtkCallbackCommand.h>
#include <vtkTransform.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkCamera.h>
class myInteractorStyle :public vtkInteractorStyleTrackballCamera
{
public:
static myInteractorStyle* New()
{
return new myInteractorStyle;
}
void SetLeftRender(vtkRenderer *render)
{
this->m_leftRender = render;
}
void SetRightRender(vtkRenderer *render)
{
this->m_rightRender = render;
}
void OnMouseMove()
{
vtkInteractorStyleTrackballCamera::OnMouseMove();
this->CameraSynchronous();
}
void OnLeftButtonDown()
{
vtkInteractorStyleTrackballCamera::OnLeftButtonDown();
this->CameraSynchronous();
}
void OnLeftButtonUp()
{
vtkInteractorStyleTrackballCamera::OnLeftButtonUp();
this->CameraSynchronous();
}
void OnMiddleButtonDown()
{
vtkInteractorStyleTrackballCamera::OnMiddleButtonDown();
this->CameraSynchronous();
}
void OnMiddleButtonUp()
{
vtkInteractorStyleTrackballCamera::OnMiddleButtonUp();
this->CameraSynchronous();
}
void OnRightButtonDown()
{
vtkInteractorStyleTrackballCamera::OnRightButtonDown();
this->CameraSynchronous();
}
void OnRightButtonUp()
{
vtkInteractorStyleTrackballCamera::OnRightButtonUp();
this->CameraSynchronous();
}
void OnMouseWheelForward()
{
vtkInteractorStyleTrackballCamera::OnMouseWheelForward();
this->CameraSynchronous();
}
void OnMouseWheelBackward()
{
vtkInteractorStyleTrackballCamera::OnMouseWheelBackward();
this->CameraSynchronous();
}
void Rotate()
{
vtkInteractorStyleTrackballCamera::Rotate();
this->CameraSynchronous();
}
void Spin()
{
vtkInteractorStyleTrackballCamera::Spin();
this->CameraSynchronous();
}
void Pan()
{
vtkInteractorStyleTrackballCamera::Pan();
this->CameraSynchronous();
}
void Dolly()
{
vtkInteractorStyleTrackballCamera::Dolly();
this->CameraSynchronous();
}
private:
void CameraSynchronous()
{
vtkCamera *leftCamera = m_leftRender->GetActiveCamera();
vtkCamera *rightCamera = m_rightRender->GetActiveCamera();
//獲取視窗大小
int *winSize = this->GetInteractor()->GetRenderWindow()->GetSize();
//獲取事件視窗位置
int eventStation[3];
this->GetInteractor()->GetEventPosition(eventStation);
if (eventStation[0] < winSize[0] / 2)
{
rightCamera->SetPosition(leftCamera->GetPosition());
rightCamera->SetFocalPoint(leftCamera->GetFocalPoint());
rightCamera->SetViewUp(leftCamera->GetViewUp());
m_rightRender->ResetCameraClippingRange();
}
else
{
leftCamera->SetPosition(rightCamera->GetPosition());
leftCamera->SetViewUp(rightCamera->GetViewUp());
leftCamera->SetFocalPoint(rightCamera->GetFocalPoint());
m_leftRender->ResetCameraClippingRange();
}
this->GetInteractor()->GetRenderWindow()->Render();
}
private:
vtkRenderer *m_leftRender;
vtkRenderer *m_rightRender;
};
8.ProjectMainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ProjectMainWindowClass</class>
<widget class="QMainWindow" name="ProjectMainWindowClass">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>607</width>
<height>583</height>
</rect>
</property>
<property name="windowTitle">
<string>ProjectMainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QVTKWidget" name="qvtkWidget_1">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>571</width>
<height>331</height>
</rect>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>607</width>
<height>23</height>
</rect>
</property>
<widget class="QMenu" name="menu">
<property name="title">
<string>檔案</string>
</property>
<addaction name="actionOpen"/>
</widget>
<widget class="QMenu" name="menu_2">
<property name="title">
<string>退出</string>
</property>
<addaction name="actionExit"/>
</widget>
<addaction name="menu"/>
<addaction name="menu_2"/>
</widget>
<widget class="QStatusBar" name="statusBar"/>
<action name="actionPrint">
<property name="text">
<string>Print</string>
</property>
</action>
<action name="actionHelp">
<property name="checkable">
<bool>false</bool>
</property>
<property name="text">
<string>Help</string>
</property>
</action>
<action name="actionSave">
<property name="text">
<string>Save</string>
</property>
</action>
<action name="actionOpen">
<property name="text">
<string>open</string>
</property>
</action>
<action name="actionExit">
<property name="text">
<string>exit</string>
</property>
</action>
</widget>
<customwidgets>
<customwidget>
<class>QVTKWidget</class>
<extends>QWidget</extends>
<header>QVTKWidget.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>
顯示效果圖
無論點那個都可以使影象聯動