本文将记录实现给每个视图增加按钮和文字提示。
文章目录
1 增加按钮、增打开文档提示
2 自定义模块"VTKRenderWidget"
2.1 vtkrenderwidget.h
2.2 vtkrenderwidget.cpp
3 重写模块“QVTKOpenGLNativeWidget”
3.1 myqvtkopenglnativewidget.h
3.2 myqvtkopenglnativewidget.cpp
☞ 源码
源码
1 增加按钮、增打开文档提示
今天又来更新文档了,今天这个四视图中又增加了点东西,今天这篇是添上上篇的坑的。三个按钮终于实现了。在实现三个按钮的情况下,还给他增加了一个打开文件夹的提示,不过这英文用的好像是不咋对,凑乎看吧,如下图:
还记得在上上片文中中增加按钮失败那,哪里使用了QVTKOpenGLWidget,死活都没法实现,我个人感觉应该是因为布局的问题。这次再升级VTK9.0.3后,使用了QVTKOpenGLNativeWidget 实现了这个功能。
2 自定义模块"VTKRenderWidget"
这里我自己定义了模块VTKRenderWidget,包含一个QVTKOpenGLNativeWidget 和三个QPushbutton ,如下图所示
#ifndef VTKRENDERWIDGET_H #define VTKRENDERWIDGET_H /***********Qt*******************/ #include <QWidget> #include <QMouseEvent> #include <QPainter> #include <QPaintEvent> #include <QPen> #include <QColor> #include <QMenu> #include <QAction> #include <QLabel> #include <QResizeEvent> /***********VTK*******************/ #include "vtkRenderWindow.h" #include "QVTKInteractor.h" #include "myqvtkopenglnativewidget.h" namespace Ui { class VTKRenderWidget; } class VTKRenderWidget : public QWidget { Q_OBJECT public: explicit VTKRenderWidget(QWidget *parent = nullptr); ~VTKRenderWidget(); /** * @brief set_BackGroundColor * 设置背景颜色 RGBA * @param r * @param g * @param b * @param a */ void set_BackGroundColor(int r=0,int g=0,int b = 0,int a = 255); void set_OpenFolderEnable(bool enable = false); //------------------------------------------------------------------------------------ vtkRenderWindow* renderWindow() const; QVTKInteractor* interactor() const; void setRenderWindow(vtkGenericOpenGLRenderWindow* win); void setRenderWindow(vtkRenderWindow* win); signals: void signal_mouseDoubleClicked(); private: void paintEvent(QPaintEvent *event); void resizeEvent(QResizeEvent *event); private: Ui::VTKRenderWidget *ui; QColor m_BackGroundColor = QColor(255,255,0,255); //背景颜色 QLabel *m_OpenFolderText = nullptr; //打开文件夹提示 QMenu *m_Menu_1 = nullptr; QAction *test = nullptr; QAction *test1 = nullptr; }; #endif // VTKRENDERWIDGET_H
2.2 vtkrenderwidget.cpp
#include "vtkrenderwidget.h" #include "ui_vtkrenderwidget.h" VTKRenderWidget::VTKRenderWidget(QWidget *parent) : QWidget(parent), ui(new Ui::VTKRenderWidget) { ui->setupUi(this); connect(ui->openGLWidget,&MyQVTKOpenGLNativeWidget::signal_mouseDoubleClicked,this,[=](){emit signal_mouseDoubleClicked();}); m_OpenFolderText = new QLabel(this); m_OpenFolderText->setText("Open Folder!"); m_OpenFolderText->setStyleSheet("QLabel{color: rgb(136, 136, 136);}QLabel{font: 24pt '黑体';}"); m_OpenFolderText->raise(); m_Menu_1 = new QMenu(this); test = new QAction(m_Menu_1); test->setText("ABC"); test->setCheckable(true); test1 = new QAction(m_Menu_1); test1->setText("EFG"); test1->setCheckable(true); m_Menu_1->addAction(test); m_Menu_1->addAction(test1); ui->pushButton_1->setMenu(m_Menu_1); ui->pushButton_2->setMenu(m_Menu_1); ui->pushButton_3->setMenu(m_Menu_1); ui->pushButton_1->setStyleSheet("QPushButton::menu-indicator{image:none;}" //不显示下拉图标 "QPushButton{border-image:url(:/ImageManage/Images/ImageManage/btn_n_1.png);}" "QPushButton:hover{border-image:url(:/ImageManage/Images/ImageManage/btn_p_1.png);}" "QPushButton:pressed{border-image:url(:/ImageManage/Images/ImageManage/btn_n_1.png);}"); ui->pushButton_2->setStyleSheet("QPushButton::menu-indicator{image:none;}" //不显示下拉图标 "QPushButton{border-image:url(:/ImageManage/Images/ImageManage/btn_n_2.png);}" "QPushButton:hover{border-image:url(:/ImageManage/Images/ImageManage/btn_p_2.png);}" "QPushButton:pressed{border-image:url(:/ImageManage/Images/ImageManage/btn_n_2.png);}"); ui->pushButton_3->setStyleSheet("QPushButton::menu-indicator{image:none;}" //不显示下拉图标 "QPushButton{border-image:url(:/ImageManage/Images/ImageManage/btn_n_3.png);}" "QPushButton:hover{border-image:url(:/ImageManage/Images/ImageManage/btn_p_3.png);}" "QPushButton:pressed{border-image:url(:/ImageManage/Images/ImageManage/btn_n_3.png);}"); } VTKRenderWidget::~VTKRenderWidget() { delete ui; } void VTKRenderWidget::set_BackGroundColor(int r, int g, int b, int a) { m_BackGroundColor.setRgb(r,g,b,a); this->update(); } void VTKRenderWidget::set_OpenFolderEnable(bool enable) { if(enable) m_OpenFolderText->show(); else m_OpenFolderText->hide(); } vtkRenderWindow *VTKRenderWidget::renderWindow() const { if(ui->openGLWidget) return ui->openGLWidget->renderWindow(); return nullptr; } QVTKInteractor *VTKRenderWidget::interactor() const { if(ui->openGLWidget) return ui->openGLWidget->interactor(); else return nullptr; } void VTKRenderWidget::setRenderWindow(vtkGenericOpenGLRenderWindow *win) { if(ui->openGLWidget) ui->openGLWidget->setRenderWindow(win); } void VTKRenderWidget::setRenderWindow(vtkRenderWindow *win) { if(ui->openGLWidget) ui->openGLWidget->setRenderWindow(win); } void VTKRenderWidget::paintEvent(QPaintEvent *event) { Q_UNUSED(event); QPainter p(this); p.setPen(Qt::NoPen); p.setBrush(m_BackGroundColor); p.drawRect(rect()); } void VTKRenderWidget::resizeEvent(QResizeEvent *event) { Q_UNUSED(event) ui->openGLWidget->move(2,2); ui->openGLWidget->resize(this->width()-4,this->height()-4); ui->pushButton_1->move(this->width()-ui->pushButton_1->width()-7,7); ui->pushButton_2->move(this->width()-ui->pushButton_1->width()*2-12,7); ui->pushButton_3->move(this->width()-ui->pushButton_1->width()*3-17,7); m_OpenFolderText->move((this->width()-m_OpenFolderText->width())/2,(this->height()-m_OpenFolderText->height())/2); m_OpenFolderText->raise(); }
在我的这自定义模块中,还使用了自己定义的QVTKOpenGLNativeWidget名字为MyQVTKOpenGLNativeWidget.
3 重写模块“QVTKOpenGLNativeWidget”
其实目前来看,这个类和原生基础的QVTKOpenGLNativeWidget没有太多区别。目前只是增加了鼠标双击事件。
3.1 myqvtkopenglnativewidget.h
#ifndef MYQVTKOPENGLNATIVEWIDGET_H #define MYQVTKOPENGLNATIVEWIDGET_H #include <QObject> #include <QOpenGLWidget> #include <QScopedPointer> // for QScopedPointer. #include <QMouseEvent> #include "QVTKInteractor.h" // needed for QVTKInteractor #include "vtkGUISupportQtModule.h" // for export macro #include "vtkNew.h" // needed for vtkNew #include "vtkSmartPointer.h" // needed for vtkSmartPointer class QVTKInteractor; class QVTKInteractorAdapter; class QVTKRenderWindowAdapter; class vtkGenericOpenGLRenderWindow; class MyQVTKOpenGLNativeWidget : public QOpenGLWidget { Q_OBJECT typedef QOpenGLWidget Superclass; public: MyQVTKOpenGLNativeWidget(QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); MyQVTKOpenGLNativeWidget(vtkGenericOpenGLRenderWindow* window, QWidget* parent = nullptr, Qt::WindowFlags f = Qt::WindowFlags()); ~MyQVTKOpenGLNativeWidget() override; void setRenderWindow(vtkGenericOpenGLRenderWindow* win); void setRenderWindow(vtkRenderWindow* win); vtkRenderWindow* renderWindow() const; QVTKInteractor* interactor() const; static QSurfaceFormat defaultFormat(bool stereo_capable = false); void setEnableHiDPI(bool enable); bool enableHiDPI() const { return this->EnableHiDPI; } void setUnscaledDPI(int dpi); int unscaledDPI() const { return this->UnscaledDPI; } void setDefaultCursor(const QCursor& cursor); const QCursor& defaultCursor() const { return this->DefaultCursor; } VTK_LEGACY(void SetRenderWindow(vtkGenericOpenGLRenderWindow* win)); VTK_LEGACY(void SetRenderWindow(vtkRenderWindow* win)); VTK_LEGACY(vtkRenderWindow* GetRenderWindow()); VTK_LEGACY(QVTKInteractor* GetInteractor()); VTK_LEGACY(QVTKInteractorAdapter* GetInteractorAdapter()); VTK_LEGACY(void setQVTKCursor(const QCursor& cursor)); VTK_LEGACY(void setDefaultQVTKCursor(const QCursor& cursor)); signals: void signal_mouseDoubleClicked(); protected slots: virtual void cleanupContext(); void updateSize(); protected: bool event(QEvent* evt) override; void initializeGL() override; void paintGL() override; void mouseDoubleClickEvent(QMouseEvent *event)override; protected: vtkSmartPointer<vtkGenericOpenGLRenderWindow> RenderWindow; QScopedPointer<QVTKRenderWindowAdapter> RenderWindowAdapter; private: Q_DISABLE_COPY(MyQVTKOpenGLNativeWidget); bool EnableHiDPI; int UnscaledDPI; QCursor DefaultCursor; }; #endif // MYQVTKOPENGLNATIVEWIDGET_H
3.2 myqvtkopenglnativewidget.cpp
#include "myqvtkopenglnativewidget.h" #include <QApplication> #include <QDesktopWidget> #include <QOpenGLContext> #include <QOpenGLFramebufferObject> #include <QOpenGLFunctions> #include <QOpenGLFunctions_3_2_Core> #include <QOpenGLTexture> #include <QPointer> #include <QScopedValueRollback> #include <QSize> #include <QtDebug> #include "QVTKInteractor.h" #include "QVTKInteractorAdapter.h" #include "QVTKRenderWindowAdapter.h" #include "vtkCommand.h" #include "vtkGenericOpenGLRenderWindow.h" #include "vtkInteractorStyleTrackballCamera.h" #include "vtkNew.h" #include "vtkObjectFactory.h" #include "vtkOpenGLState.h" MyQVTKOpenGLNativeWidget::MyQVTKOpenGLNativeWidget(QWidget* parentWdg, Qt::WindowFlags f) : MyQVTKOpenGLNativeWidget(vtkSmartPointer<vtkGenericOpenGLRenderWindow>::New().GetPointer(), parentWdg, f) { } MyQVTKOpenGLNativeWidget::MyQVTKOpenGLNativeWidget(vtkGenericOpenGLRenderWindow* renderWin, QWidget* parentWdg, Qt::WindowFlags f): Superclass(parentWdg, f) , RenderWindow(nullptr) , RenderWindowAdapter(nullptr) , EnableHiDPI(true) , UnscaledDPI(72) , DefaultCursor(QCursor(Qt::ArrowCursor)) { this->setFocusPolicy(Qt::StrongFocus); this->setUpdateBehavior(QOpenGLWidget::NoPartialUpdate); this->setMouseTracking(true); this->connect(this, SIGNAL(resized()), SLOT(updateSize())); this->setRenderWindow(renderWin); this->grabGesture(Qt::PinchGesture); this->grabGesture(Qt::PanGesture); this->grabGesture(Qt::TapGesture); this->grabGesture(Qt::TapAndHoldGesture); this->grabGesture(Qt::SwipeGesture); } MyQVTKOpenGLNativeWidget::~MyQVTKOpenGLNativeWidget() { this->makeCurrent(); this->cleanupContext(); } void MyQVTKOpenGLNativeWidget::setRenderWindow(vtkGenericOpenGLRenderWindow *win) { if (this->RenderWindow == win) { return; } if (this->RenderWindowAdapter) { this->makeCurrent(); this->RenderWindowAdapter.reset(nullptr); } this->RenderWindow = win; if (this->RenderWindow) { this->RenderWindow->SetReadyForRendering(false); if (!this->RenderWindow->GetInteractor()) { vtkNew<QVTKInteractor> iren; this->RenderWindow->SetInteractor(iren); iren->Initialize(); vtkNew<vtkInteractorStyleTrackballCamera> style; iren->SetInteractorStyle(style); } if (this->isValid()) { this->makeCurrent(); this->initializeGL(); this->updateSize(); } } } void MyQVTKOpenGLNativeWidget::setRenderWindow(vtkRenderWindow *win) { auto gwin = vtkGenericOpenGLRenderWindow::SafeDownCast(win); if (win != nullptr && gwin == nullptr) { qDebug() << "QVTKOpenGLNativeWidget requires a `vtkGenericOpenGLRenderWindow`. `" << win->GetClassName() << "` is not supported."; } this->setRenderWindow(gwin); } vtkRenderWindow *MyQVTKOpenGLNativeWidget::renderWindow() const { return this->RenderWindow; } QVTKInteractor *MyQVTKOpenGLNativeWidget::interactor() const { return this->RenderWindow ? QVTKInteractor::SafeDownCast(this->RenderWindow->GetInteractor()) : nullptr; } QSurfaceFormat MyQVTKOpenGLNativeWidget::defaultFormat(bool stereo_capable) { return QVTKRenderWindowAdapter::defaultFormat(stereo_capable); } void MyQVTKOpenGLNativeWidget::setEnableHiDPI(bool enable) { this->EnableHiDPI = enable; if (this->RenderWindowAdapter) { this->RenderWindowAdapter->setEnableHiDPI(enable); } } void MyQVTKOpenGLNativeWidget::setUnscaledDPI(int dpi) { this->UnscaledDPI = dpi; if (this->RenderWindowAdapter) { this->RenderWindowAdapter->setUnscaledDPI(dpi); } } void MyQVTKOpenGLNativeWidget::setDefaultCursor(const QCursor &cursor) { this->DefaultCursor = cursor; if (this->RenderWindowAdapter) { this->RenderWindowAdapter->setDefaultCursor(cursor); } } void MyQVTKOpenGLNativeWidget::cleanupContext() { this->RenderWindowAdapter.reset(nullptr); } void MyQVTKOpenGLNativeWidget::updateSize() { if (this->RenderWindowAdapter) { this->RenderWindowAdapter->resize(this->width(), this->height()); } } bool MyQVTKOpenGLNativeWidget::event(QEvent *evt) { if (this->RenderWindowAdapter) { this->RenderWindowAdapter->handleEvent(evt); } return this->Superclass::event(evt); } void MyQVTKOpenGLNativeWidget::initializeGL() { this->Superclass::initializeGL(); if (this->RenderWindow) { Q_ASSERT(this->RenderWindowAdapter.data() == nullptr); this->RenderWindowAdapter.reset(new QVTKRenderWindowAdapter(this->context(), this->RenderWindow, this)); this->RenderWindowAdapter->setDefaultCursor(this->defaultCursor()); this->RenderWindowAdapter->setEnableHiDPI(this->EnableHiDPI); this->RenderWindowAdapter->setUnscaledDPI(this->UnscaledDPI); } this->connect(this->context(), SIGNAL(aboutToBeDestroyed()), SLOT(cleanupContext()),static_cast<Qt::ConnectionType>(Qt::UniqueConnection | Qt::DirectConnection)); } void MyQVTKOpenGLNativeWidget::paintGL() { this->Superclass::paintGL(); if (this->RenderWindow) { Q_ASSERT(this->RenderWindowAdapter); this->RenderWindowAdapter->paint(); this->makeCurrent(); QOpenGLFunctions_3_2_Core* f = QOpenGLContext::currentContext()->versionFunctions<QOpenGLFunctions_3_2_Core>(); if (f) { const QSize deviceSize = this->size() * this->devicePixelRatioF(); this->RenderWindowAdapter->blit(this->defaultFramebufferObject(), GL_COLOR_ATTACHMENT0, QRect(QPoint(0, 0), deviceSize)); } } else { QOpenGLFunctions* f = QOpenGLContext::currentContext()->functions(); f->glClearColor(1.0f, 1.0f, 1.0f, 1.0f); f->glClear(GL_COLOR_BUFFER_BIT); } } void MyQVTKOpenGLNativeWidget::mouseDoubleClickEvent(QMouseEvent *event) { Q_UNUSED(event); emit signal_mouseDoubleClicked(); } //----------------------------------------------------------------------------- #if !defined(VTK_LEGACY_REMOVE) void MyQVTKOpenGLNativeWidget::SetRenderWindow(vtkRenderWindow* win) { VTK_LEGACY_REPLACED_BODY( QVTKOpenGLNativeWidget::SetRenderWindow, "VTK 9.0", QVTKOpenGLNativeWidget::setRenderWindow); vtkGenericOpenGLRenderWindow* gwin = vtkGenericOpenGLRenderWindow::SafeDownCast(win); if (gwin == nullptr && win != nullptr) { qDebug() << "QVTKOpenGLNativeWidget requires a `vtkGenericOpenGLRenderWindow`. `" << win->GetClassName() << "` is not supported."; } this->setRenderWindow(gwin); } #endif //----------------------------------------------------------------------------- #if !defined(VTK_LEGACY_REMOVE) void MyQVTKOpenGLNativeWidget::SetRenderWindow(vtkGenericOpenGLRenderWindow* win) { VTK_LEGACY_REPLACED_BODY( QVTKOpenGLNativeWidget::SetRenderWindow, "VTK 9.0", QVTKOpenGLNativeWidget::setRenderWindow); this->setRenderWindow(win); } #endif //----------------------------------------------------------------------------- #if !defined(VTK_LEGACY_REMOVE) vtkRenderWindow* MyQVTKOpenGLNativeWidget::GetRenderWindow() { VTK_LEGACY_REPLACED_BODY( QVTKOpenGLNativeWidget::GetRenderWindow, "VTK 9.0", QVTKOpenGLNativeWidget::renderWindow); return this->renderWindow(); } #endif //----------------------------------------------------------------------------- #if !defined(VTK_LEGACY_REMOVE) QVTKInteractorAdapter* MyQVTKOpenGLNativeWidget::GetInteractorAdapter() { VTK_LEGACY_BODY(QVTKOpenGLNativeWidget::GetInteractorAdapter, "VTK 9.0"); return nullptr; } #endif //----------------------------------------------------------------------------- #if !defined(VTK_LEGACY_REMOVE) QVTKInteractor* MyQVTKOpenGLNativeWidget::GetInteractor() { VTK_LEGACY_REPLACED_BODY( QVTKOpenGLNativeWidget::GetInteractor, "VTK 9.0", QVTKOpenGLNativeWidget::interactor); return this->interactor(); } #endif //----------------------------------------------------------------------------- #if !defined(VTK_LEGACY_REMOVE) void MyQVTKOpenGLNativeWidget::setQVTKCursor(const QCursor& cursor) { VTK_LEGACY_REPLACED_BODY( QVTKOpenGLNativeWidget::setQVTKCursor, "VTK 9.0", QVTKOpenGLNativeWidget::setCursor); this->setCursor(cursor); } #endif //----------------------------------------------------------------------------- #if !defined(VTK_LEGACY_REMOVE) void MyQVTKOpenGLNativeWidget::setDefaultQVTKCursor(const QCursor& cursor) { VTK_LEGACY_REPLACED_BODY(QVTKOpenGLNativeWidget::setDefaultQVTKCursor, "VTK 9.0", QVTKOpenGLNativeWidget::setDefaultCursor); this->setDefaultCursor(cursor); } #endif