本文大部分內(nèi)容轉(zhuǎn)自七星重劍的在Qt窗口里用D3D畫轉(zhuǎn)動的三角形一文,加上點滴個人研究筆記。
先上個自己的運行結(jié)果圖吧:

現(xiàn)在的問題是只有“臟”了才畫,也就是才去調(diào)用如下的method;不是三角形沒動,是動了但是沒畫出來,我們看不見。
用個timer去解決這個問題?
這老外的帖子對我?guī)椭艽螅桓阆旅娴膬蓷l整個QD3DWidget都看不到:
Using Direct3D 9 with Qt - flicker problem
According to the Qt docs, if you want to use GDI or Direct3D on Windows with Qt, you need to:
1) Override QWidget::paintEngine to return NULL
2) Call QWidget::setAttribute(Qt::WA_PaintOnScreen, true)
完整代碼:
D3D相關(guān)代碼
紅字部分是根據(jù)原作者的文章自己研究后加上去的,現(xiàn)在的代碼已經(jīng)可以實現(xiàn)窗體中三角型的轉(zhuǎn)動,關(guān)鍵就是利用定時器每隔20ms強(qiáng)制窗體重繪(update()),因為不太熟悉Qt,搞了半天才知道是update這個函數(shù)。
用如下測試代碼就可以得到開頭的結(jié)果畫面:
先上個自己的運行結(jié)果圖吧:
現(xiàn)在的問題是只有“臟”了才畫,也就是才去調(diào)用如下的method;不是三角形沒動,是動了但是沒畫出來,我們看不見。
1 void QD3DWidget::paintEvent(QPaintEvent *)
2 {
3 if (updatesEnabled())
4 {
5 d3dDraw();
6 }
7 }
2 {
3 if (updatesEnabled())
4 {
5 d3dDraw();
6 }
7 }
用個timer去解決這個問題?
這老外的帖子對我?guī)椭艽螅桓阆旅娴膬蓷l整個QD3DWidget都看不到:
Using Direct3D 9 with Qt - flicker problem
According to the Qt docs, if you want to use GDI or Direct3D on Windows with Qt, you need to:
1) Override QWidget::paintEngine to return NULL
2) Call QWidget::setAttribute(Qt::WA_PaintOnScreen, true)
完整代碼:
1 // QD3DWidget.h
2
3 #pragma once
4
5 #include <Qt/qwidget.h>
6
7 class QD3DWidget : public QWidget
8 {
9 Q_OBJECT
10
11 public:
12 QD3DWidget(QWidget* parent = 0);
13 ~QD3DWidget();
14
15 QPaintEngine* paintEngine() const;
16
17 protected:
18 virtual void initializeD3D();
19 virtual void paintD3D();
20
21 void paintEvent(QPaintEvent *);
22
23 virtual void d3dInit();
24 virtual void d3dDraw();
25
26 bool initialized() const {return m_bInit;}
27
28 void MsgBox();
29 private:
30 bool m_bInit;
31 };
2
3 #pragma once
4
5 #include <Qt/qwidget.h>
6
7 class QD3DWidget : public QWidget
8 {
9 Q_OBJECT
10
11 public:
12 QD3DWidget(QWidget* parent = 0);
13 ~QD3DWidget();
14
15 QPaintEngine* paintEngine() const;
16
17 protected:
18 virtual void initializeD3D();
19 virtual void paintD3D();
20
21 void paintEvent(QPaintEvent *);
22
23 virtual void d3dInit();
24 virtual void d3dDraw();
25
26 bool initialized() const {return m_bInit;}
27
28 void MsgBox();
29 private:
30 bool m_bInit;
31 };
1 // QD3DWidget.cpp
2
3 #include "QtD3DWidget.h"
4 #include "D3D.h"
5 #include <qtimer.h>
6
7 void QD3DWidget::MsgBox()
8 {
9 QMessageBox mb(this);
10
11 mb.exec();
12 }
13
14 QD3DWidget::QD3DWidget(QWidget* parent /* = 0 */) : QWidget(parent), m_bInit(false)
15 {
16 resize(QSize(400, 300));
17 setAttribute(Qt::WA_PaintOnScreen, true);
18
19 QTimer *timer = new QTimer(this);
20 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
21 timer->start(20);
22 }
23
24 QD3DWidget::~QD3DWidget()
25 {
26
27 }
28
29 void QD3DWidget::initializeD3D()
30 {
31 InitD3D(winId());
32 InitVB();
33
34 m_bInit = true;
35 }
36
37 void QD3DWidget::paintD3D()
38 {
39 Render();
40 }
41
42 void QD3DWidget::paintEvent(QPaintEvent *)
43 {
44 if (updatesEnabled())
45 {
46 d3dDraw();
47 }
48 }
49
50 void QD3DWidget::d3dInit()
51 {
52 initializeD3D();
53 }
54
55 void QD3DWidget::d3dDraw()
56 {
57 if (!initialized())
58 {
59 d3dInit();
60 }
61
62 paintD3D();
63 }
64
65 QPaintEngine* QD3DWidget::paintEngine() const
66 {
67 return NULL;
68 }
2
3 #include "QtD3DWidget.h"
4 #include "D3D.h"
5 #include <qtimer.h>
6
7 void QD3DWidget::MsgBox()
8 {
9 QMessageBox mb(this);
10
11 mb.exec();
12 }
13
14 QD3DWidget::QD3DWidget(QWidget* parent /* = 0 */) : QWidget(parent), m_bInit(false)
15 {
16 resize(QSize(400, 300));
17 setAttribute(Qt::WA_PaintOnScreen, true);
18
19 QTimer *timer = new QTimer(this);
20 connect(timer, SIGNAL(timeout()), this, SLOT(update()));
21 timer->start(20);
22 }
23
24 QD3DWidget::~QD3DWidget()
25 {
26
27 }
28
29 void QD3DWidget::initializeD3D()
30 {
31 InitD3D(winId());
32 InitVB();
33
34 m_bInit = true;
35 }
36
37 void QD3DWidget::paintD3D()
38 {
39 Render();
40 }
41
42 void QD3DWidget::paintEvent(QPaintEvent *)
43 {
44 if (updatesEnabled())
45 {
46 d3dDraw();
47 }
48 }
49
50 void QD3DWidget::d3dInit()
51 {
52 initializeD3D();
53 }
54
55 void QD3DWidget::d3dDraw()
56 {
57 if (!initialized())
58 {
59 d3dInit();
60 }
61
62 paintD3D();
63 }
64
65 QPaintEngine* QD3DWidget::paintEngine() const
66 {
67 return NULL;
68 }
紅字部分是根據(jù)原作者的文章自己研究后加上去的,現(xiàn)在的代碼已經(jīng)可以實現(xiàn)窗體中三角型的轉(zhuǎn)動,關(guān)鍵就是利用定時器每隔20ms強(qiáng)制窗體重繪(update()),因為不太熟悉Qt,搞了半天才知道是update這個函數(shù)。
用如下測試代碼就可以得到開頭的結(jié)果畫面:
1 #include "stdafx.h"
2 #include "QtD3DWidget.h"
3 #include <qapplication.h>
4
5 int _tmain(int argc, char* argv[])
6 {
7 QApplication app(argc, argv);
8
9 QD3DWidget* d3dWidget = new QD3DWidget();
10
11 d3dWidget->show();
12
13 return app.exec();
14 }
2 #include "QtD3DWidget.h"
3 #include <qapplication.h>
4
5 int _tmain(int argc, char* argv[])
6 {
7 QApplication app(argc, argv);
8
9 QD3DWidget* d3dWidget = new QD3DWidget();
10
11 d3dWidget->show();
12
13 return app.exec();
14 }



