Hello,
I've done some small updates to this code. I'm posting this update here, because I think that there could be a better/ more modern approach for integrating Irrlicht into Qt. But first I have to try my idea.
That's why I post the slightly modernized version here. It works with Qt 5.x and was freed from some now unnecessary code or updated where it was necessary.
If my idea works, I will publish a new posting.
Have fun!
Code: Select all
// ui_mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QtCore/QVariant>
#include <QtWidgets/QAction>
#include <QtWidgets/QApplication>
#include <QtWidgets/QButtonGroup>
#include <QtWidgets/QHeaderView>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QMenuBar>
#include <QtWidgets/QStatusBar>
#include <QtWidgets/QToolBar>
#include <QtWidgets/QWidget>
QT_BEGIN_NAMESPACE
class Ui_MainWindow
{
public:
QWidget *centralWidget;
QMenuBar *menuBar;
QToolBar *mainToolBar;
QStatusBar *statusBar;
void setupUi(QMainWindow *MainWindow)
{
if (MainWindow->objectName().isEmpty())
MainWindow->setObjectName(QString::fromUtf8("MainWindow"));
MainWindow->resize(800, 600);
centralWidget = new QWidget(MainWindow);
centralWidget->setObjectName(QString::fromUtf8("centralWidget"));
MainWindow->setCentralWidget(centralWidget);
menuBar = new QMenuBar(MainWindow);
menuBar->setObjectName(QString::fromUtf8("menuBar"));
menuBar->setGeometry(QRect(0, 0, 800, 25));
MainWindow->setMenuBar(menuBar);
mainToolBar = new QToolBar(MainWindow);
mainToolBar->setObjectName(QString::fromUtf8("mainToolBar"));
MainWindow->addToolBar(Qt::TopToolBarArea, mainToolBar);
statusBar = new QStatusBar(MainWindow);
statusBar->setObjectName(QString::fromUtf8("statusBar"));
MainWindow->setStatusBar(statusBar);
retranslateUi(MainWindow);
QMetaObject::connectSlotsByName(MainWindow);
} // setupUi
void retranslateUi(QMainWindow *MainWindow)
{
MainWindow->setWindowTitle(QObject::tr("Main Window"));
} // retranslateUi
};
namespace Ui {
class MainWindow: public Ui_MainWindow {};
} // namespace Ui
QT_END_NAMESPACE
#endif // MAINWINDOW_H
Code: Select all
// mainapplicationwindow.h
#ifndef MAINAPPLICATIONWINDOW_H
#define MAINAPPLICATIONWINDOW_H
#include <QMainWindow>
#include <QWidget>
#include "ui_mainwindow.h"
#include "qirrlichtwidget.h"
// Include the namespace from ui_MainWindow.h
namespace Ui {
class MainWindow;
}
// Class that contains the main window as well as the irrlicht widget
class MainApplicationWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainApplicationWindow(QWidget *parent = 0);
~MainApplicationWindow();
QIrrlichtWidget* getIrrlichtWidget(){return &irrWidget;}
private:
Ui::MainWindow *ui;
QIrrlichtWidget irrWidget;
protected:
virtual void resizeEvent(QResizeEvent* event);
};
#endif // MAINAPPLICATIONWINDOW_H
Code: Select all
// qirrlichtwidget.h
#ifndef QIRRLICHTWIDGET_H
#define QIRRLICHTWIDGET_H
#include <QMainWindow>
#include <QWidget>
#include <QResizeEvent>
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
// Our Irrlicht rendering widget
class QIrrlichtWidget : public QWidget
{
Q_OBJECT
public:
explicit QIrrlichtWidget(QWidget *parent = 0);
~QIrrlichtWidget();
// Returns a pointer to the Irrlicht Device
IrrlichtDevice* getIrrlichtDevice();
// Create the Irrlicht device and connect the signals and slots
void init();
signals:
// Signal that its time to update the frame
void updateIrrlichtQuery(IrrlichtDevice* device);
public slots:
// Function called in response to updateIrrlichtQuery. Renders the scene in the widget
void updateIrrlicht(IrrlichtDevice* device);
protected:
virtual void paintEvent(QPaintEvent* event);
virtual void timerEvent(QTimerEvent* event);
virtual void resizeEvent(QResizeEvent* event);
IrrlichtDevice *device;
// We keep the camera inside this widget so we can resize the window dynamically
ICameraSceneNode* camera;
};
#endif // QIRRWIDGET_H
Code: Select all
// main.cpp
#include "mainapplicationwindow.h"
int main(int argc, char* argv[])
{
QApplication app(argc, argv);
MainApplicationWindow mainWindow;
mainWindow.show();
mainWindow.getIrrlichtWidget()->init();
// Make sure the Irrlicht Device exists before trying to use it
if(mainWindow.getIrrlichtWidget()->getIrrlichtDevice())
{
ISceneManager *smgr = mainWindow.getIrrlichtWidget()->getIrrlichtDevice()->getSceneManager();
IVideoDriver *driver = mainWindow.getIrrlichtWidget()->getIrrlichtDevice()->getVideoDriver();
// Just display a simple mesh
IAnimatedMesh* mesh = smgr->getMesh("media/sydney.md2");
if (!mesh)
{
mainWindow.getIrrlichtWidget()->getIrrlichtDevice()->drop();
return 1;
}
IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
if (node)
{
node->setMaterialFlag(EMF_LIGHTING, false);
node->setMD2Animation(scene::EMAT_STAND);
node->setMaterialTexture( 0, driver->getTexture("media/sydney.bmp") );
}
}
return app.exec();
}
Code: Select all
// mainapplicationwindow.cpp
#include "mainapplicationwindow.h"
MainApplicationWindow::MainApplicationWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// Setup for the Irrlicht Widget
irrWidget.setParent(ui->centralWidget);
irrWidget.setGeometry(0, 0, size().width(), size().height());
}
MainApplicationWindow::~MainApplicationWindow()
{
delete ui;
}
void MainApplicationWindow::resizeEvent(QResizeEvent *event)
{
irrWidget.setGeometry(0, 0, size().width(), size().height());
event->accept();
}
Code: Select all
// qirrlichtwidget.cpp
#include "qirrlichtwidget.h"
#include "mainapplicationwindow.h"
QIrrlichtWidget::QIrrlichtWidget(QWidget *parent) :
QWidget(parent)
{
device = 0;
}
QIrrlichtWidget::~QIrrlichtWidget()
{
if(device != 0)
{
device->closeDevice();
device->drop();
}
}
// Create the Irrlicht device and connect the signals and slots
void QIrrlichtWidget::init()
{
// Make sure we can't create the device twice
if(device != 0)
return;
// Set all the device creation parameters
SIrrlichtCreationParameters params;
params.AntiAlias = 0;
params.Bits = 32;
params.DeviceType = EIDT_X11;
params.Doublebuffer = true;
params.DriverType = EDT_OPENGL;
params.EventReceiver = 0;
params.Fullscreen = false;
params.HighPrecisionFPU = false;
params.IgnoreInput = false;
params.LoggingLevel = ELL_INFORMATION;
params.Stencilbuffer = true;
params.Stereobuffer = false;
params.Vsync = false;
// Specify which window/widget to render to
params.WindowId = reinterpret_cast<void*>(winId());
params.WindowSize.Width = width();
params.WindowSize.Height = height();
params.WithAlphaChannel = false;
params.ZBufferBits = 16;
// Create the Irrlicht Device with the previously specified parameters
device = createDeviceEx(params);
if(device)
{
// Create a camera so we can view the scene
camera = device->getSceneManager()->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
}
// Connect the update signal (updateIrrlichtQuery) to the update slot (updateIrrlicht)
connect (this, &QIrrlichtWidget::updateIrrlichtQuery, this, &QIrrlichtWidget::updateIrrlicht);
// Start a timer. A timer with setting 0 will update as often as possible.
startTimer(0);
}
IrrlichtDevice* QIrrlichtWidget::getIrrlichtDevice()
{
return device;
}
void QIrrlichtWidget::paintEvent(QPaintEvent* event)
{
if(device != 0)
{
emit updateIrrlichtQuery(device);
}
event->accept();
}
void QIrrlichtWidget::timerEvent(QTimerEvent* event)
{
// Emit the render signal each time the timer goes off
if (device != 0)
{
emit updateIrrlichtQuery(device);
}
event->accept();
}
void QIrrlichtWidget::resizeEvent(QResizeEvent* event)
{
if(device != 0)
{
dimension2d<u32> widgetSize;
widgetSize.Width = event->size().width();
widgetSize.Height = event->size().height();
device->getVideoDriver()->OnResize(widgetSize);
ICameraSceneNode *cam = device->getSceneManager()->getActiveCamera();
if (cam != 0)
{
cam->setAspectRatio((f32)widgetSize.Width / (f32)widgetSize.Height);
}
}
}
void QIrrlichtWidget::updateIrrlicht( irr::IrrlichtDevice* device )
{
if(device != 0)
{
device->getTimer()->tick();
SColor color (255,100,100,140);
device->getVideoDriver()->beginScene(true, true, color);
device->getSceneManager()->drawAll();
device->getVideoDriver()->endScene();
}
}