本文将记录实现给每个视图增加按钮和文字提示。
文章目录
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 ,如下图所示
2.1 vtkrenderwidget.h
#ifndef VTKRENDERWIDGET_H #define VTKRENDERWIDGET_H /***********Qt*******************/ #include #include #include #include #include #include #include #include #include #include /***********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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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(); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
在我的这自定义模块中,还使用了自己定义的QVTKOpenGLNativeWidget名字为MyQVTKOpenGLNativeWidget.
3 重写模块“QVTKOpenGLNativeWidget”
其实目前来看,这个类和原生基础的QVTKOpenGLNativeWidget没有太多区别。目前只是增加了鼠标双击事件。
3.1 myqvtkopenglnativewidget.h
#ifndef MYQVTKOPENGLNATIVEWIDGET_H #define MYQVTKOPENGLNATIVEWIDGET_H #include #include #include // for QScopedPointer. #include #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 RenderWindow; QScopedPointer RenderWindowAdapter; private: Q_DISABLE_COPY(MyQVTKOpenGLNativeWidget); bool EnableHiDPI; int UnscaledDPI; QCursor DefaultCursor; }; #endif // MYQVTKOPENGLNATIVEWIDGET_H
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
3.2 myqvtkopenglnativewidget.cpp
#include "myqvtkopenglnativewidget.h" #include #include #include #include #include #include #include #include #include #include #include #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::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 iren; this->RenderWindow->SetInteractor(iren); iren->Initialize(); vtkNew 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::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(); 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
☞ 源码
源码链接:https://github.com/DreamLife-Jianwei/Qt-Vtk
使用方法:☟☟☟
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。