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);
}
相关推荐
Quz15 分钟前
QML 字体度量与文本度量:FontMetrics 与 TextMetrics
qt
Quz16 分钟前
QML 字体加载:FontLoader 与 FontInfo
qt
奇树谦1 小时前
Qt | 基于 GVCP 协议的 GigE 工业相机 IP 配置工具
数码相机·qt·tcp/ip
扶尔魔ocy1 天前
【qt openGL入门笔记】windows编译MinGW版本的osgearth(2.10.1)
qt·opengl·osg
小王C语言1 天前
Qt常用控件:按钮类控件,普通按键、单选、复选
开发语言·qt
初阳7852 天前
【基于Qt5仿音乐播放器(六)】——细节打磨与总结
qt·1024程序员节
小鱼仙官2 天前
使用Qt封装C/C++ 动态库 用于第三方调用
c语言·c++·qt
扶尔魔ocy2 天前
【QT OpenGL】知识点编目及传送门
qt·opengl·osg
qq_401700412 天前
Qt Modbus 源码剖析:从 QModbusClient 到工业级通信框架
开发语言·qt·系统架构
GreenHandBruce2 天前
基于Qt 6.12.0 + FFmpeg 9.0.2的4k视频播放器
qt·ffmpeg·音视频