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);
}
相关推荐
甄同学21 小时前
第二十篇:MCP协议集成,Claude Code如何扩展AI Agent的能力边界
开发语言·人工智能·qt
Liknana1 天前
Qt 圆角表格控件实战:setMask + 抗锯齿描边 + 多层阴影
qt·ui
Scott9999HH1 天前
2026 避坑实录:国产品牌压力变送器什么牌子好?从硬件抗扰到 C++ Qt 实时曲线绘制源码剖析
开发语言·c++·qt
皓悦编程记2 天前
【YOLO26 系列】基于YOLO26的垃圾分类检测系统【python源码+Pyqt5界面/WEB+数据集+训练代码】
python·qt·分类
十五年专注C++开发2 天前
qobject_cast转换失败原因分析
c++·qt·dynamic_cast·qobject_cast
sycmancia2 天前
Qt——线程的生命期问题
开发语言·qt
闻道且行之2 天前
TurboOCR:基于PP-OCRv6的极速Windows离线OCR工具,深度解析3.4GB依赖背后的技术架构
c++·人工智能·python·qt·机器学习·ocr
气概3 天前
QT集成basler相机
开发语言·数码相机·qt
GIS阵地3 天前
QgsRasterDataProvider 完整详解(QGIS 3.40.13 C++)
开发语言·c++·qt·开源软件·qgis
sycmancia4 天前
Qt——多线程中的信号与槽(二)
jvm·qt