History: Integrating Ogre into QT5
Source of version: 11
Copy to clipboard
__Work in Progress__
Original forum link: http://www.ogre3d.org/forums/viewtopic.php?p=515314#p515314
The following document is an up-to-date method of integrating Ogre3D into the latest version of Qt (5.4 as of this writing).
Previous methods of integrating Ogre3D with Qt focused on [http://www.ogre3d.org/tikiwiki/QtOgre|using a QWidget] and overriding the paint methods for drawing.
The method proposed in this document uses a QWindow as opposed to a QWidget. There are several reasons for using a QWindow over a QWidget:
*__Smaller memory and processing footprint__. Simply put, [http://stackoverflow.com/questions/17860604/what-is-the-difference-between-a-qwindow-and-qwidget|QWidgets use a lot of resources in comparison to QWindows] and thus adds a lot of unnecessary overhead. Ogre3D doesn't require much from Qt besides a surface to draw upon and the input/window events from the underlying OS and windowing system that a QWindow provides. As a related sidenote, QWidget actually inherits from QWindow as shown in the below inheritance chain.
*__Easier integration__. Because of the simplicity of QWindow it is actually much easier to integrate Ogre3D into Qt using a QWindow versus using a QWidget. QWidget by default creates a surface to draw on which Ogre3D doesn't need to draw upon. Thus, as shown in earlier Qt-Ogre3D implementation documents you need to override Qt methods to prevent the default QWidget methods from "drawing over" Ogre3D. Using a QWindow this is unnecessary as with a QWindow no "surface" is automatically created; you can simply supply QWindow's native "Window ID" (whether it is a HWND on Windows or the equivalent representations on Linux/Mac OS) and Ogre3D takes care of the rest.
*__All standard Qt functionality can still be implemented__. If you still need to have a QWidget (for integration into a QMainWindow/QTabWidget/etc.) this can still be achieved. You can easily create a QWidget "container" for your QWindow and get all of the standard benefits of a QWidget. A standard use case for this would be needing to place an Ogre3D-integrated QWindow inside of a standard QMainWindow as a "central widget".
Note that the below implementation depends on you following the guides on setting up include/library/runtime binary directories. Since the setup for this is compiler and operating system dependent such details is beyond the scope of this document.
In summary, [http://www.ogre3d.org/tikiwiki/Setting+Up+An+Application+-+Visual+Studio|consult a guide like this] for setting up directory paths, includes, etc. Note that you will need to do this for Qt as well so Q
Now, let's create a header file to include the necessary Qt components as well as including Ogre3D:
{CODE(wrap="1", colors="c++")}
#ifndef QTOGREWINDOW_H_
#define QTOGREWINDOW_H_
/*
For the purposes of creating a bare-bones Qt implementation I found that I only need to include these three (3) Qt headers.
*/
#include <QtWidgets/QApplication>
#include <QtGui/QKeyEvent>
#include <QtGui/QWindow>
/*
As expected; the Ogre3D header.
*/
#include "OGRE/Ogre.h"
#endif
{CODE}