2、VDK 使用QVTKOpenGLNativeWidget嵌入到QT窗体中

MainWindow.h

cpp 复制代码
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QVTKOpenGLNativeWidget.h>

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private:
    Ui::MainWindow *ui;
    QVTKOpenGLNativeWidget *m_vtkWd = nullptr;

    void InitVDK();
};
#endif // MAINWINDOW_H

MainWindow.cpp

cpp 复制代码
#include "MainWindow.h"
#include "./ui_MainWindow.h"
#include <QVBoxLayout>
#include <vtkConeSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkProperty.h>
#include <vtkRenderer.h>
#include <vtkRenderWindow.h>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    InitVDK();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::InitVDK()
{
    m_vtkWd = new QVTKOpenGLNativeWidget(this);

    auto ly = new QVBoxLayout();
    ui->centralwidget->setLayout(ly);
    ly->addWidget(m_vtkWd);

    auto cone = vtkSmartPointer<vtkConeSource>::New();
    cone->SetRadius(1);
    cone->SetHeight(3);
    cone->SetResolution(100);

    auto mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper->SetInputConnection(cone->GetOutputPort());

    auto actor = vtkSmartPointer<vtkActor>::New();
    actor->SetMapper(mapper);
    actor->GetProperty()->SetColor(0, 1, 0);
    actor->GetProperty()->SetRepresentationToWireframe();


    auto mapper2 = vtkSmartPointer<vtkPolyDataMapper>::New();
    mapper2->SetInputConnection(cone->GetOutputPort());

    auto actor2 = vtkSmartPointer<vtkActor>::New();
    actor2->SetMapper(mapper2);
    actor2->GetProperty()->SetColor(1, 0, 0);
    actor2->GetProperty()->SetOpacity(0.5);

    auto render = vtkSmartPointer<vtkRenderer>::New();
    render->AddActor(actor);
    render->AddActor(actor2);

    m_vtkWd->renderWindow()->AddRenderer(render);
}
相关推荐
常乐か8 小时前
在 Ubuntu 24.04 中编译 Qt 5.15.2 项目完全指南
linux·qt·ubuntu
我是小灰灰吖11 小时前
Ubuntu 22.04 切换为 X11 会话以解决 QT 桌面程序兼容性问题
linux·qt·ubuntu
≮傷£≯√11 小时前
QT配置FFmpeg
开发语言·qt·ffmpeg
≮傷£≯√12 小时前
配置 opencv Qt项目
人工智能·qt·opencv
qq_4017004113 小时前
Qt Modbus协议0x00
开发语言·qt
Zenova EdgeOS14 小时前
C++ 工业边缘 Qt 桌面应用实战:从 QWidget 到模型视图、网络与多线程
网络·c++·qt
沫璃染墨1 天前
《Qt从零入门系列(一):Qt框架初识——从发展历史到跨平台开发》
c++·qt·系统架构·gui
郝学胜-神的一滴2 天前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
我是小灰灰吖2 天前
Ubuntu 22.04 安装 SSH 服务与远程调试环境配置指南
qt·ubuntu·ssh
雨田言炎2 天前
四、关于Qt项目需要知道的
开发语言·笔记·qt