1 // QIrrlichtWidget.cpp 2 3 #include "QIrrlichtWidget.h" 4 #include <QtCore/QDebug> 5 6 QIrrlichtWidget::QIrrlichtWidget(QWidget *parent) : 7 QWidget(parent) 8 { 9 // Indicates that the widget wants to draw directly onto the screen. (From documentation : http://doc.qt.nokia.com/latest/qt.html) 10 // Essential to have this or there will be nothing displayed 11 setAttribute(Qt::WA_PaintOnScreen); 12 // Indicates that the widget paints all its pixels when it receives a paint event. 13 // Thus, it is not required for operations like updating, resizing, scrolling and focus changes to erase the widget before generating paint events. 14 // Not sure this is required for the program to run properly, but it is here just incase. 15 setAttribute(Qt::WA_OpaquePaintEvent); 16 // Widget accepts focus by both tabbing and clicking 17 setFocusPolicy(Qt::StrongFocus); 18 // Not sure if this is necessary, but it was in the code I am basing this solution off of 19 setAutoFillBackground(false); 20 21 device = 0; 22 } 23 24 QIrrlichtWidget::~QIrrlichtWidget() 25 { 26 if(device != 0) 27 { 28 device->closeDevice(); 29 device->drop(); 30 } 31 } 32 33 // Create the Irrlicht device and connect the signals and slots 34 void QIrrlichtWidget::init() 35 { 36 // Make sure we can't create the device twice 37 if(device != 0) 38 return; 39 40 // Set all the device creation parameters 41 SIrrlichtCreationParameters params; 42 params.AntiAlias = 0; 43 params.Bits = 32; 44 params.DeviceType = EIDT_X11; 45 params.Doublebuffer = true; 46 params.DriverType = EDT_OPENGL; 47 params.EventReceiver = 0; 48 params.Fullscreen = false; 49 params.HighPrecisionFPU = false; 50 params.IgnoreInput = false; 51 params.LoggingLevel = ELL_INFORMATION; 52 params.Stencilbuffer = true; 53 params.Stereobuffer = false; 54 params.Vsync = false; 55 // Specify which window/widget to render to 56 params.WindowId = reinterpret_cast<void*>(winId()); 57 params.WindowSize.Width = width(); 58 params.WindowSize.Height = height(); 59 params.WithAlphaChannel = false; 60 params.ZBufferBits = 16; 61 62 // Create the Irrlicht Device with the previously specified parameters 63 64 device = createDeviceEx(params); //问题应该出在这,下面的if语句没有执行 65 66 if(device) 67 { 68 // Create a camera so we can view the scene 69 camera = device->getSceneManager()->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0)); 70 } 71 72 // Connect the update signal (updateIrrlichtQuery) to the update slot (updateIrrlicht) 73 connect(this, SIGNAL(updateIrrlichtQuery(IrrlichtDevice*)), this, SLOT(updateIrrlicht(IrrlichtDevice*))); 74 75 // Start a timer. A timer with setting 0 will update as often as possible. 76 startTimer(0); 77 } 78 79 IrrlichtDevice* QIrrlichtWidget::getIrrlichtDevice() 80 { 81 return device; 82 } 83 84 void QIrrlichtWidget::paintEvent(QPaintEvent* event) 85 { 86 if(device != 0) 87 { 88 emit updateIrrlichtQuery(device); 89 } 90 } 91 92 void QIrrlichtWidget::timerEvent(QTimerEvent* event) 93 { 94 // Emit the render signal each time the timer goes off 95 if (device != 0) 96 { 97 emit updateIrrlichtQuery(device); 98 } 99 100 event->accept(); 101 } 102 103 void QIrrlichtWidget::resizeEvent(QResizeEvent* event) 104 { 105 if(device != 0) 106 { 107 dimension2d<u32> widgetSize; 108 widgetSize.Width = event->size().width(); 109 widgetSize.Height = event->size().height(); 110 device->getVideoDriver()->OnResize(widgetSize); 111 112 ICameraSceneNode *cam = device->getSceneManager()->getActiveCamera(); 113 if (cam != 0) 114 { 115 cam->setAspectRatio((f32)widgetSize.Height / (f32)widgetSize.Width); 116 } 117 } 118 119 QWidget::resizeEvent(event); 120 } 121 122 void QIrrlichtWidget::updateIrrlicht( irr::IrrlichtDevice* device ) 123 { 124 if(device != 0) 125 { 126 device->getTimer()->tick(); 127 128 SColor color (255,100,100,140); 129 130 device->getVideoDriver()->beginScene(true, true, color); 131 device->getSceneManager()->drawAll(); 132 device->getVideoDriver()->endScene(); 133 } 134 135 } 136 137 // Include the extra Qt file for signals and slots 138 #include "moc_QIrrlichtWidget.cpp"