Irrlicht / Qt4 integration + QtCreator

A forum to store posts deemed exceptionally wise and useful
serengeor
Posts: 1712
Joined: Tue Jan 13, 2009 7:34 pm
Location: Lithuania

Re: Irrlicht / Qt4 integration + QtCreator

Post by serengeor »

YankzzDev wrote:
serengeor wrote:
YankzzDev wrote:hmmm, i have another question...


after i load the model, it's displayed on the screen, but i load another model, it's displayed 2 model with selected one and previous one...

how i can clear the previous one?
Read the F***** API and get some c++ skills :|
Not trying to be rude, but basically it seams that we're programming for you.
the rude one is you... i not asking you to programming for me.....

Because i don't understand so i ask..

i am not expert like you(C++ Expert maybe than C++ god)..

i am newbie in c++ and irrlicht engine too...

my basic languange is vb.net..

if i don't understand, so i ask? it's fault?
It's bad to ask someone for doing something for you without putting any effort into it yourself. If you want to code irrlicht apps/games you will have to learn c++ or use a wrapper for .NET, theres no excuse as "my basic languange is vb.net.." so I need you guys to do this for me!
i am not expert like you(C++ Expert maybe than C++ god)..
I'm far from expert myself and I might have asked these questions myself, but now I see that doing research on your own makes life easier most of the time.

Just try to study and understand how c++ works, than reading api will be easy enough :)
Working on game: Marrbles (Currently stopped).
rookie
Posts: 16
Joined: Thu Dec 29, 2011 9:44 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by rookie »

I am trying to embed kinect with qt & irrlicht. I am able to display irrlicht in 1 window & my kinect stream in other window. When I merge them nothing shows up on top of the kinect stream. I am trying to recreate something like this.
http://www.youtube.com/watch?v=1jbvnk1T4vQ

I am doing all the model rendering A& physics using irrlicht & want hem to display stuff over the body displayed in kinect stream. Right now I am able to display my shirt model in a different window using irrlicht but can't merge them. I used the following to merge them but nothing shows up

Params.WindowId = (void *)winID;

winID is winId() returned from the QMainWindow. My model is derived from Qgraphicsitem. I can' use QWidget since I can't display the widget over the video stream. The widget overwrites the kinect video stream that is the video stream is not displayed if I use QWidget & hence I am using QGraphicsItem. Please help me. Also how to setup the irrlicht camera so that it shows the stream correctly??
aanderse
Posts: 155
Joined: Sun Aug 10, 2008 2:02 pm
Location: Canada

Re: Irrlicht / Qt4 integration + QtCreator

Post by aanderse »

i'm not entirely sure about the issue, but i can tell you that using the id of the main window isn't what you want
in qt the main window has a "centralWidget" which is the widget you want to grab the id from
you can set the central widget to any widget you like via setCentralWidget
rookie
Posts: 16
Joined: Thu Dec 29, 2011 9:44 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by rookie »

Thanks for your help. I tried using this->centralwidget() but that returns 0. I tried setCntralWidget(widget) to the widget which has all qgraphicsviews but no change. I tried adding IrrlichtDevice to the setcentralwidget but obviously doesn't accept irrlichtdeivce parameters. Since I am doing all my irrlicht stuff inside qgraphicsitem would that cause any problems??
aanderse
Posts: 155
Joined: Sun Aug 10, 2008 2:02 pm
Location: Canada

Re: Irrlicht / Qt4 integration + QtCreator

Post by aanderse »

well...

i haven't tested, but you might try something like this:

QWidget *irrlichtWidget = new QWidget ();
irrlichtWidget->setAttribute (Qt::WA_OpaquePaintEvent, true);
irrlichtWidget->setAttribute (Qt::WA_PaintOnScreen, true);

QGraphicsProxyWidget *proxyWidget = yourGraphicsScene->addWidget (irrlichtWidget);

now proxyWidget is your graphics item

i'd be interested to hear if that works or not
rookie
Posts: 16
Joined: Thu Dec 29, 2011 9:44 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by rookie »

I have already created a class derived from QGraphicsItem that is Model. I am doing all irrlicht rendering inside Models paint function. So when I create a Models instance & add it to QGraphicsScene it should draw the irrlicht rendering since I have merged the window where kinect stream is being displayed with he irrlicht window. Also I have already added the following lines in my MainWindows constructor
QGLWidget *widget = new QGLWidget(QGLFormat(QGL::SampleBuffers));
widget->makeCurrent();

view->setViewport(widget);
view->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);

Also the documentation says QGLWidget & QgraphicsProxyWidget shouldn't be merged. I tried using Qwidget irrlichwidget instead of QGLWidget without QgraphicsProxyWidget & it still remains the same. Where should I use proxyWidget because when I don't use it it says

QWidget::repaint: It is dangerous to leave painters active on a widget outside of the PaintEvent

& it automatically shuts down the program without displaying any kinect video stream. It doesn't give me this error when I am adding many other Qlabel to the scene.

Also I just found out that the irrlicht window is being displayed behind the window which displays the kinect video. Any idea how to solve that.
Thank a lot for all your help.
rookie
Posts: 16
Joined: Thu Dec 29, 2011 9:44 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by rookie »

@aanderse I got the setCentralWidget working but the model still doesn't show up.

Can anyone help me with the following??
http://irrlicht.sourceforge.net/forum/v ... =1&t=45611
aanderse
Posts: 155
Joined: Sun Aug 10, 2008 2:02 pm
Location: Canada

Re: Irrlicht / Qt4 integration + QtCreator

Post by aanderse »

after looking at your new thread i want to mention that on the first page of this thread there is a QIrrlichtWidget class which should help you out
rookie
Posts: 16
Joined: Thu Dec 29, 2011 9:44 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by rookie »

I know but I don't want it to be QWidget...Anyways thanks for your help
ritti
Posts: 90
Joined: Tue Apr 10, 2012 12:09 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by ritti »

Hi, first of all thx for this nice tutorial.
I have a Problem with actions that were done during the game runs.
I started with the Irrlichts tutorial 07.Collision and built on it so now i can select and send units around with the mouse.
The Problem is that all this is in a while loop:

Code: Select all

    while(mainWindow.getIrrlichtWidget()->getIrrlichtDevice()->run())
    if (mainWindow.getIrrlichtWidget()->getIrrlichtDevice()->isWindowActive())
    {... //code to send units around and watch for collisions
 
If i do so i wont never get to

Code: Select all

return app.exec();
So the window never opens
All works fine if i remove the loop but i cant send units around.
How do I have to do it so that the window opens and the QWidget updates on changes?
I hope u know what I mean :)
Thx Ritti
Virion
Competition winner
Posts: 2148
Joined: Mon Dec 18, 2006 5:04 am

Re: Irrlicht / Qt4 integration + QtCreator

Post by Virion »

hi ritti. check out the first post again. you will see the implementation of timerEvent():

Code: Select all

void QIrrlichtWidget::timerEvent(QTimerEvent* event)
{
    // Emit the render signal each time the timer goes off
    if (device != 0)
    {
        emit updateIrrlichtQuery(device);
    }
 
    event->accept();
}
basically it will run emit updateIrrlichtQuery(device); on every frame, which, works just like while(mainWindow.getIrrlichtWidget()->getIrrlichtDevice()->run())
therefore, you should put your "send units around" and "watch for collisions" code within the updateIrrlichtQuery() function, since it's called on every frame. Hope this help.
ritti
Posts: 90
Joined: Tue Apr 10, 2012 12:09 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by ritti »

Ah jeah cool thx for your answer it works great now :)
fdgonzalez
Posts: 7
Joined: Wed Mar 07, 2012 2:27 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by fdgonzalez »

It is perfect. Thanks so much. It is really what I wanted.
AGameDev
Posts: 5
Joined: Tue Jan 06, 2015 5:15 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by AGameDev »

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();
    }
}
 
rustyk
Posts: 25
Joined: Sat Jan 10, 2015 4:03 pm

Re: Irrlicht / Qt4 integration + QtCreator

Post by rustyk »

This is really nice AGameDev. Thanks for sharing and now with QT5. Awesome work! :)

I'm new to irrlicht but have done some small QTApps. Do you mind posting a downloadable template for a complete reference?

Thanks again.
Post Reply