Qt+VTK多进程,VTK窗口进程嵌入主进程

效果#

在这里插入图片描述

子窗口头文件ViewWidget3D.h#

#pragma once

#include <QMainWindow>
#include <QVTKOpenGLNativeWidget.h>
#include <vtkActor.h>
#include <vtkCamera.h>
#include <vtkCylinderSource.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPolyDataMapper.h>
#include <vtkProperty.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>

#include <array>

class ViewWidget3D : public QMainWindow
{
	Q_OBJECT

public:
	ViewWidget3D(QWidget*parent = nullptr);
	~ViewWidget3D();
	QVTKOpenGLNativeWidget* getWidget();
private:
	QVTKOpenGLNativeWidget* vtkWidget;
};

子窗口实现文件ViewWidget3D.cpp#

#include "ViewWidget3D.h"
#include <QLabel>
ViewWidget3D::ViewWidget3D(QWidget*parent)
	: QMainWindow(parent)
{
	vtkWidget = new QVTKOpenGLNativeWidget();
	this->setCentralWidget(vtkWidget);
	vtkNew<vtkNamedColors> colors;

	// Set the background color.
	std::array<unsigned char, 4> bkg{ {26, 51, 102, 255} };
	colors->SetColor("BkgColor", bkg.data());

	// This creates a polygonal cylinder model with eight circumferential facets
	// (i.e, in practice an octagonal prism).
	vtkNew<vtkCylinderSource> cylinder;
	cylinder->SetResolution(8);

	// The mapper is responsible for pushing the geometry into the graphics
	// library. It may also do color mapping, if scalars or other attributes are
	// defined.
	vtkNew<vtkPolyDataMapper> cylinderMapper;
	cylinderMapper->SetInputConnection(cylinder->GetOutputPort());

	// The actor is a grouping mechanism: besides the geometry (mapper), it
	// also has a property, transformation matrix, and/or texture map.
	// Here we set its color and rotate it around the X and Y axes.
	vtkNew<vtkActor> cylinderActor;
	cylinderActor->SetMapper(cylinderMapper);
	cylinderActor->GetProperty()->SetColor(
		colors->GetColor4d("Tomato").GetData());
	cylinderActor->RotateX(30.0);
	cylinderActor->RotateY(-45.0);

	// The renderer generates the image
	// which is then displayed on the render window.
	// It can be thought of as a scene to which the actor is added
	vtkNew<vtkRenderer> renderer;
	renderer->AddActor(cylinderActor);
	renderer->SetBackground(colors->GetColor3d("BkgColor").GetData());
	// Zoom in a little by accessing the camera and invoking its "Zoom" method.
	renderer->ResetCamera();
	renderer->GetActiveCamera()->Zoom(1.5);

	vtkWidget->renderWindow()->AddRenderer(renderer);	
}

ViewWidget3D::~ViewWidget3D()
{
}

QVTKOpenGLNativeWidget* ViewWidget3D::getWidget()
{
	return vtkWidget;
}

子窗口启动文件main.cpp#

#include "ViewWidget3D.h"
#include <QtWidgets/QApplication>
#include <QWindow>
#include <iostream>
int main(int argc, char* argv[])
{
	QApplication a(argc, argv);
	ViewWidget3D w;
	w.setWindowFlags(Qt::FramelessWindowHint);
	w.show();

	w.createWinId();
	// 输出
	std::cerr << w.winId() << std::endl;
	return a.exec();
}

参考正常嵌入#

源代码示例#

作者:Esofar

出处:https://www.cnblogs.com/WindSnowLi/p/16998152.html

版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。

posted @   WindSnowLi  阅读(291)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
more_horiz
keyboard_arrow_up light_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示