【VTK】VTK 显示小球例子,在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK

知识不是单独的,一定是成体系的。更多我的个人总结和相关经验可查阅这个专栏:Visual Studio

编号 内容
1 【Visual Studio】在 Windows 上使用 Visual Studio 构建 VTK
2 【Visual Studio】在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK
3 【VTK】VTK 显示小球例子,在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK
4 【VTK】官方示例,移植到自己的 Qt 工程,含代码

关于更多此例子的资料,可以参考:【Visual Studio】在 Windows 上使用 Visual Studio 配合 Qt 构建 VTK

完成了使用 Qt 来显示一个小球的例子后,我还尝试从 VTK 官网上下载了一个例子,然后自己尝试着将其移植到了自己的 Qt 工程中,感兴趣可以看:【VTK】官方示例,移植到自己的 Qt 工程,含代码

文章目录

版本环境

版本环境为:

  • win11
  • visual studio 2022
  • VTK-9.2.6
  • CMake 3.26.3
  • Qt 6.2.8

VTKTest.ui

VTKTest.h

cpp 复制代码
#pragma once

#include <QtWidgets/QMainWindow>
#include "ui_VTKTest.h"

#include <qsurfaceformat.h>
#include <QVTKOpenGLNativeWidget.h>

#include <vtkSphereSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkGenericOpenGLRenderWindow.h>
#include <vtkNamedColors.h>
#include <vtkProperty.h>
#include <vtkSmartPointer.h>

#include "vtkAutoInit.h"

class VTKTest : public QMainWindow
{
    Q_OBJECT

public:
    VTKTest(QWidget* parent = nullptr);
    //VTKTest(QWidget* parent = Q_NULLPTR);
    ~VTKTest();

private slots:
    void on_pushButton_clicked();

private:
    Ui::VTKTestClass ui;
};

VTKTest.cpp

cpp 复制代码
#include "VTKTest.h"

VTK_MODULE_INIT(vtkRenderingOpenGL2);
VTK_MODULE_INIT(vtkInteractionStyle);
VTK_MODULE_INIT(vtkRenderingVolumeOpenGL2);
VTK_MODULE_INIT(vtkRenderingFreeType);

VTKTest::VTKTest(QWidget *parent)
    : QMainWindow(parent)
{
    ui.setupUi(this);
}

VTKTest::~VTKTest()
{}

void VTKTest::on_pushButton_clicked()
{
    QSurfaceFormat::setDefaultFormat(QVTKOpenGLNativeWidget::defaultFormat());
    //QVTKOpenGLNativeWidget* widget = new QVTKOpenGLNativeWidget();

    vtkSmartPointer<vtkNamedColors> colors = vtkSmartPointer<vtkNamedColors>::New();

    vtkSmartPointer<vtkSphereSource> sphereSource = vtkSmartPointer<vtkSphereSource>::New();

    vtkSmartPointer<vtkPolyDataMapper> sphereMapper = vtkSmartPointer<vtkPolyDataMapper>::New();

    sphereMapper->SetInputConnection(sphereSource->GetOutputPort());
    vtkSmartPointer<vtkActor> sphereActor = vtkSmartPointer<vtkActor>::New();

    sphereActor->SetMapper(sphereMapper);
    sphereActor->GetProperty()->SetColor(colors->GetColor4d("Tomato").GetData());

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(sphereActor);
    renderer->SetBackground(colors->GetColor3d("SteelBlue").GetData());

    vtkSmartPointer<vtkGenericOpenGLRenderWindow> renderWindow = vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New();
    renderWindow->AddRenderer(renderer);
    renderWindow->SetWindowName("RenderWindowNoUIFile");

    ui.qvtkWidget->setRenderWindow(renderWindow);
    ui.qvtkWidget->resize(200, 160);
    ui.qvtkWidget->show();

}

main.cpp

cpp 复制代码
#include "VTKTest.h"
#include <QtWidgets/QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    VTKTest w;
    w.show();
    return a.exec();
}

运行结果

Ref.

  1. QVTKOpenGLNativeWidget在Qt中的用法
  2. VTK+Qt的第一个例子
  3. C++ 泛型编程
  4. C++ 模板
  5. C语言带参数的宏定义
相关推荐
love530love6 分钟前
【PyCharm必会基础】正确移除解释器及虚拟环境(以 Poetry 为例 )
开发语言·ide·windows·笔记·python·pycharm
笨笨马甲44 分钟前
附加模块--Qt OpenGL模块功能及架构
开发语言·qt
黄交大彭于晏2 小时前
发送文件脚本源码版本
java·linux·windows
uyeonashi4 小时前
【QT控件】输入类控件详解
开发语言·c++·qt
vfvfb12 小时前
bat批量去掉本文件夹中的文件扩展名
服务器·windows·批处理·删除扩展名·bat技巧
galaxy_strive14 小时前
绘制饼图详细过程
开发语言·c++·qt
我命由我1234518 小时前
VSCode - VSCode 放大与缩小代码
前端·ide·windows·vscode·前端框架·编辑器·软件工具
PT_silver18 小时前
tryhackme——Abusing Windows Internals(进程注入)
windows·microsoft
爱炸薯条的小朋友19 小时前
C#由于获取WPF窗口名称造成的异常报错问题
windows·c#·wpf
Lw老王要学习20 小时前
VScode 使用 git 提交数据到指定库的完整指南
windows·git·vscode