Quote Originally Posted by wysota View Post
Can you show us the complete code for this widget and its plugin?
qt4direct3dcontainer.h
Qt Code:
  1. #pragma once
  2.  
  3. #ifndef QT4DIRECT3DCONTAINER_H
  4. #define QT4DIRECT3DCONTAINER_H
  5.  
  6. #include <QtGui/QWidget>
  7. #include <qmessagebox.h>
  8. #include <QResizeEvent>
  9. #include <QTimer>
  10.  
  11. #include <QtDesigner/QDesignerExportWidget>
  12.  
  13. // Direct3D Header
  14. #include <d3d9.h>
  15. #include <d3dx9.h>
  16.  
  17. #include "Cube.h"
  18.  
  19. class QDESIGNER_WIDGET_EXPORT Qt4Direct3DContainer : public QWidget
  20. {
  21. Q_OBJECT
  22.  
  23. public:
  24. Qt4Direct3DContainer(QWidget *parent = 0);
  25. ~Qt4Direct3DContainer();
  26.  
  27. BOOL Init();
  28. void Shut();
  29. PDIRECT3DDEVICE9 GetDevice(){ return m_pD3D9dev; };
  30.  
  31. QPaintEngine *paintEngine() const;
  32. void paintEvent(QPaintEvent *p_event);
  33. void resizeEvent(QResizeEvent *p_event);
  34.  
  35. public slots:
  36.  
  37. void turn();
  38.  
  39. private:
  40.  
  41. D3DPRESENT_PARAMETERS m_d3dPP;
  42. PDIRECT3D9 m_pD3D9;
  43. PDIRECT3DDEVICE9 m_pD3D9dev;
  44.  
  45. QTimer *m_pqTimer;
  46.  
  47. D3DXVECTOR3 m_vEye;
  48. D3DXVECTOR3 m_vAt;
  49. D3DXVECTOR3 m_vUp;
  50.  
  51. D3DXMATRIX m_mtxWorld;
  52. D3DXMATRIX m_mtxView;
  53. D3DXMATRIX m_mtxProjection;
  54.  
  55. float m_fTurn;
  56. bool m_bTurn;
  57.  
  58. };
  59.  
  60. #endif // QT4DIRECT3DCONTAINER_H
To copy to clipboard, switch view to plain text mode 

qt4direct3dcontainer.cpp
Qt Code:
  1. #include "qt4direct3dcontainer.h"
  2.  
  3. Qt4Direct3DContainer::Qt4Direct3DContainer(QWidget *parent)
  4. : QWidget(parent)
  5. {
  6. m_pD3D9 = NULL;
  7. m_pD3D9dev = NULL;
  8. ZeroMemory(&m_d3dPP, sizeof(m_d3dPP));
  9.  
  10. m_pqTimer = NULL;
  11.  
  12. setAttribute(Qt::WA_PaintOnScreen, true);
  13.  
  14. Init();
  15. }
  16.  
  17. Qt4Direct3DContainer::~Qt4Direct3DContainer()
  18. {
  19. Shut();
  20. }
  21.  
  22. BOOL Qt4Direct3DContainer::Init()
  23. {
  24. Shut();
  25.  
  26. // Initialize Direct3D 9
  27. m_pD3D9 = Direct3DCreate9(D3D_SDK_VERSION);
  28.  
  29. // Test for Success
  30. if(m_pD3D9 == NULL)
  31. {
  32. QMessageBox::information(this, "Direct 3D Initialization",
  33. "Unable to initialize Direct3D 9.");
  34. return FALSE;
  35. }
  36.  
  37. // Presentation Parameters
  38.  
  39. // Backbuffer Parameters
  40. m_d3dPP.BackBufferWidth = width();
  41. m_d3dPP.BackBufferHeight = height();
  42. m_d3dPP.BackBufferFormat = D3DFMT_A8R8G8B8;
  43. m_d3dPP.BackBufferCount = 1;
  44. m_d3dPP.SwapEffect = D3DSWAPEFFECT_FLIP;
  45. m_d3dPP.FullScreen_RefreshRateInHz = 0;
  46. m_d3dPP.PresentationInterval = D3DPRESENT_INTERVAL_DEFAULT;
  47.  
  48. // Multisampling(Antialiasing) Parameters
  49. m_d3dPP.MultiSampleType = D3DMULTISAMPLE_NONE;
  50. m_d3dPP.MultiSampleQuality = 0;
  51.  
  52. // Window Parameters
  53. m_d3dPP.hDeviceWindow = winId();
  54. m_d3dPP.Windowed = TRUE;
  55.  
  56. // Depth/Stencil Buffer Parameters
  57. m_d3dPP.EnableAutoDepthStencil = TRUE;
  58. m_d3dPP.AutoDepthStencilFormat = D3DFMT_D24S8;
  59.  
  60. // misc. Parameters
  61. m_d3dPP.Flags = 0;
  62.  
  63. // Initialize Direct3D 9 Device
  64. if(!SUCCEEDED(m_pD3D9->CreateDevice( D3DADAPTER_DEFAULT,
  65. D3DDEVTYPE_HAL,
  66. winId(),
  67. D3DCREATE_SOFTWARE_VERTEXPROCESSING,
  68. &m_d3dPP,
  69. &m_pD3D9dev)))
  70. {
  71. QMessageBox::information(this, "Direct 3D Device Initialization",
  72. "Unable to initialize Direct3D 9 Device.");
  73. return FALSE;
  74. }
  75.  
  76. //Vectors
  77. m_vEye = D3DXVECTOR3(5.0f, 2.0f, 5.0f);
  78. m_vAt = D3DXVECTOR3(0.0f, 0.0f, 0.0f);
  79. m_vUp = D3DXVECTOR3(0.0f, 1.0f, 0.0f);
  80.  
  81. //Matrices
  82. m_fTurn = 0.0f;
  83. m_bTurn = false;
  84. D3DXMatrixRotationY(&m_mtxWorld, m_fTurn);
  85. D3DXMatrixLookAtLH(&m_mtxView, &m_vEye, &m_vAt, &m_vUp);
  86. D3DXMatrixPerspectiveFovLH(&m_mtxProjection, 0.14f, width() / height(), 0.1f, 10.0f);
  87.  
  88. if(InitCubeObjects(m_pD3D9dev))
  89. {
  90. cube_effect->SetMatrix(cube_world, &m_mtxWorld);
  91. cube_effect->SetMatrix(cube_view, &m_mtxView);
  92. cube_effect->SetMatrix(cube_projection, &m_mtxProjection);
  93.  
  94. cube_effect->CommitChanges();
  95. }
  96.  
  97. //QTimer
  98.  
  99. m_pqTimer = new QTimer(this);
  100. connect(m_pqTimer, SIGNAL(timeout()), this, SLOT(repaint()));
  101. m_pqTimer->start(10);
  102.  
  103. return TRUE;
  104. }
  105.  
  106. void Qt4Direct3DContainer::turn()
  107. {
  108. m_bTurn = !m_bTurn;
  109. }
  110.  
  111. void Qt4Direct3DContainer::Shut()
  112. {
  113. // delete QTimer
  114. if(m_pqTimer != NULL && m_pqTimer->isActive())
  115. {
  116. m_pqTimer->stop();
  117. }
  118. delete m_pqTimer;
  119. m_pqTimer = NULL;
  120.  
  121. ReleaseCubeObjects();
  122.  
  123. // Release Direct3D 9 Device
  124. if(m_pD3D9dev != NULL)
  125. {
  126. m_pD3D9dev->Release();
  127. }
  128. m_pD3D9dev = NULL;
  129.  
  130. // Zero Direct3D 9 Presentation Parameters
  131. ZeroMemory(&m_d3dPP, sizeof(m_d3dPP));
  132.  
  133. // Release Direct3D 9
  134. if(m_pD3D9 != NULL)
  135. {
  136. m_pD3D9->Release();
  137. }
  138. m_pD3D9 = NULL;
  139.  
  140. // Matrices
  141. ZeroMemory(&m_mtxWorld, sizeof(D3DXMATRIX));
  142. ZeroMemory(&m_mtxView, sizeof(D3DXMATRIX));
  143. ZeroMemory(&m_mtxProjection, sizeof(D3DXMATRIX));
  144.  
  145. // Vectors
  146. ZeroMemory(&m_vEye, sizeof(D3DXVECTOR3));
  147. ZeroMemory(&m_vAt, sizeof(D3DXVECTOR3));
  148. ZeroMemory(&m_vUp, sizeof(D3DXVECTOR3));
  149. }
  150.  
  151. QPaintEngine *Qt4Direct3DContainer::paintEngine() const
  152. {
  153. return NULL;
  154. }
  155.  
  156. void Qt4Direct3DContainer::paintEvent(QPaintEvent *p_event)
  157. {
  158. m_pD3D9dev->Clear( 0,
  159. NULL,
  160. D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
  161. D3DCOLOR_RGBA(64, 64, 96, 255),
  162. 1.0f,
  163. 0);
  164.  
  165. m_pD3D9dev->BeginScene();
  166.  
  167. m_pD3D9dev->SetVertexDeclaration(pDeclarationPosColorNormal);
  168. m_pD3D9dev->SetStreamSource(0, cube_vertexBuffer, 0, sizeof(VertexPosColorNormal));
  169. m_pD3D9dev->SetIndices(cube_indexBuffer);
  170.  
  171. //m_pD3D9dev->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
  172.  
  173. UINT uiNumberOfPasses,
  174. uiPass;
  175.  
  176. if(m_bTurn)
  177. {
  178. m_fTurn += 0.01f;
  179. D3DXMatrixRotationY(&m_mtxWorld, m_fTurn);
  180. }
  181. cube_effect->SetMatrix(cube_world, &m_mtxWorld);
  182. cube_effect->SetMatrix(cube_view, &m_mtxView);
  183. cube_effect->SetMatrix(cube_projection, &m_mtxProjection);
  184.  
  185. cube_effect->CommitChanges();
  186.  
  187. cube_effect->Begin(&uiNumberOfPasses, 0);
  188. for(uiPass = 0; uiPass < uiNumberOfPasses; uiPass++)
  189. {
  190. cube_effect->BeginPass(uiPass);
  191. /*m_pD3D9dev->DrawIndexedPrimitive( D3DPT_TRIANGLELIST,
  192. 0,
  193. 0,
  194. cube_numberOfVertices,
  195. 0,
  196. 12);*/
  197.  
  198. m_pD3D9dev->DrawIndexedPrimitiveUP( D3DPT_TRIANGLELIST,
  199. 0,
  200. cube_numberOfVertices,
  201. 12,
  202. cube_indices,
  203. D3DFMT_INDEX32,
  204. &cube_vertices,
  205. sizeof(VertexPosColorNormal));
  206.  
  207. cube_effect->EndPass();
  208. }
  209. cube_effect->End();
  210.  
  211. m_pD3D9dev->EndScene();
  212.  
  213. m_pD3D9dev->Present( NULL,
  214. NULL,
  215. NULL,
  216. NULL);
  217. }
  218.  
  219. void Qt4Direct3DContainer::resizeEvent(QResizeEvent *p_event)
  220. {
  221. QSize newSize = p_event->size();
  222.  
  223. m_d3dPP.BackBufferWidth = newSize.rwidth();
  224. m_d3dPP.BackBufferHeight = newSize.rheight();
  225.  
  226. D3DXMatrixPerspectiveFovLH(&m_mtxProjection, 1.6f, (float)m_d3dPP.BackBufferWidth / (float)m_d3dPP.BackBufferHeight, 0.1f, 10.0f);
  227.  
  228. ReleaseCubeObjects();
  229.  
  230. m_pD3D9dev->Reset(&m_d3dPP);
  231.  
  232. InitCubeObjects(m_pD3D9dev);
  233. }
To copy to clipboard, switch view to plain text mode 

qt4direct3dcontainerplugin.h
Qt Code:
  1. #ifndef QT4DIRECT3DCONTAINERPLUGIN_H
  2. #define QT4DIRECT3DCONTAINERPLUGIN_H
  3.  
  4. #include <QDesignerCustomWidgetInterface>
  5. #include <QDesignerMemberSheetExtension>
  6. #include <QExtensionManager>
  7. #include <QDesignerFormEditorInterface>
  8.  
  9. class Qt4Direct3DContainerPlugin : public QObject, public QDesignerCustomWidgetInterface
  10. {
  11. Q_OBJECT
  12.  
  13. public:
  14. Qt4Direct3DContainerPlugin(QObject *parent = 0);
  15.  
  16. bool isContainer() const;
  17. bool isInitialized() const;
  18. QIcon icon() const;
  19. QString domXml() const;
  20. QString group() const;
  21. QString includeFile() const;
  22. QString name() const;
  23. QString toolTip() const;
  24. QString whatsThis() const;
  25. QWidget *createWidget(QWidget *parent);
  26. void initialize(QDesignerFormEditorInterface *core);
  27.  
  28. private:
  29. bool initialized;
  30. };
  31.  
  32. #endif // QT4DIRECT3DCONTAINERPLUGIN_H
To copy to clipboard, switch view to plain text mode 

qt4direct3dcontainerplugin.cpp
Qt Code:
  1. #include "qt4direct3dcontainer.h"
  2.  
  3. #include <QtCore/QtPlugin>
  4. #include "Qt4D3DCExtensionFactory.h"
  5. #include "qt4direct3dcontainerplugin.h"
  6.  
  7.  
  8. Qt4Direct3DContainerPlugin::Qt4Direct3DContainerPlugin(QObject *parent)
  9. : QObject(parent)
  10. {
  11. initialized = false;
  12. }
  13.  
  14. void Qt4Direct3DContainerPlugin::initialize(QDesignerFormEditorInterface *formEditor)
  15. {
  16. if (initialized)
  17. return;
  18.  
  19. initialized = true;
  20. }
  21.  
  22. bool Qt4Direct3DContainerPlugin::isInitialized() const
  23. {
  24. return initialized;
  25. }
  26.  
  27. QWidget *Qt4Direct3DContainerPlugin::createWidget(QWidget *parent)
  28. {
  29. return new Qt4Direct3DContainer(parent);
  30. }
  31.  
  32. QString Qt4Direct3DContainerPlugin::name() const
  33. {
  34. return "Direct3D Container";
  35. }
  36.  
  37. QString Qt4Direct3DContainerPlugin::group() const
  38. {
  39. return "Containers";
  40. }
  41.  
  42. QIcon Qt4Direct3DContainerPlugin::icon() const
  43. {
  44. return QIcon(":/icons/directx.png");
  45. }
  46.  
  47. QString Qt4Direct3DContainerPlugin::toolTip() const
  48. {
  49. return QString();
  50. }
  51.  
  52. QString Qt4Direct3DContainerPlugin::whatsThis() const
  53. {
  54. return QString();
  55. }
  56.  
  57. bool Qt4Direct3DContainerPlugin::isContainer() const
  58. {
  59. return false;
  60. }
  61.  
  62. QString Qt4Direct3DContainerPlugin::domXml() const
  63. {
  64. return "<widget class=\"Qt4Direct3DContainer\" name=\"qt4Direct3DContainer\">\n"
  65. " <property name=\"geometry\">\n"
  66. " <rect>\n"
  67. " <x>0</x>\n"
  68. " <y>0</y>\n"
  69. " <width>100</width>\n"
  70. " <height>100</height>\n"
  71. " </rect>\n"
  72. " </property>\n"
  73. "</widget>\n";
  74. }
  75.  
  76. QString Qt4Direct3DContainerPlugin::includeFile() const
  77. {
  78. return "qt4direct3dcontainer.h";
  79. }
  80.  
  81. Q_EXPORT_PLUGIN2(qt4direct3dcontainer, Qt4Direct3DContainerPlugin)
To copy to clipboard, switch view to plain text mode