OgreApplicationContextQt disappeared after embeded in widget by createWindowContainer(window) Topic is solved

Problems building or running the engine, queries about how to use features etc.
raner
Gnoblar
Posts: 2
Joined: Sat May 25, 2024 4:48 pm
x 1

OgreApplicationContextQt disappeared after embeded in widget by createWindowContainer(window)

Post by raner »

Hi everyone,

I've recently started using Ogre and I'm really enjoying it. I'm currently working on embedding the render window into my Qt application. I found OgreApplicationContextQt and migrated the BasicTutorial code into it, then added some Qt widgets. The program worked fine, except that the Ogre window was located outside the Qt window.

I tried adding the following line of code:

Code: Select all

container->createWindowContainer(window);

However, after adding this line, the Ogre window disappeared.

I also tried calling startTimer() as suggested in the documentation, but it didn't work. Additionally, I attempted to write some code in timerEvent after finding it in the source code, but that didn't work either.

Could anyone please help me figure out what I did wrong?

Ogre Version: 14.2
Operating System: Windows10 22H2 19045
Render System: OpenGL

Code: Select all

#include <QApplication>
#include <QMainWindow>
#include <QHBoxLayout>
#include <QLabel>
#include "Ogre.h"
#include "OgreApplicationContextQt.h"
#include "OgreInput.h"
#include "OgreRTShaderSystem.h"
#include <iostream>

using namespace Ogre;
using namespace OgreBites;

class BasicTutorial1
        : public ApplicationContextQt
                , public InputListener
{
public:
    BasicTutorial1();
    virtual ~BasicTutorial1() {}

void createRoot() override;
void setup();
bool keyPressed(const KeyboardEvent& evt);

// protected:
//     void timerEvent(QTimerEvent* event) override
//     {
//         mRoot->renderOneFrame();
//     }

private:
    std::string mPluginsCfg;
    std::string mResourcesCfg;
    std::string mOgreCfg;
};


BasicTutorial1::BasicTutorial1()
        : ApplicationContextQt("OgreTutorialApp")
{
}

void BasicTutorial1::createRoot() {
    // Define the paths to your configuration files
    mPluginsCfg = "../../config/plugins.cfg";
    mResourcesCfg = "../../config/resources.cfg";
    mOgreCfg = "../../log/ogre.cfg";

// Create the Root object with custom config file paths
mRoot = new Ogre::Root(mPluginsCfg, mOgreCfg, "../../log/custom-ogre-log.log");
}

void BasicTutorial1::setup()
{
    // Load resource paths from config file
    Ogre::ConfigFile cf;
    cf.load(mResourcesCfg);

Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String secName, typeName, archName;

while (seci.hasMoreElements()) {
    secName = seci.peekNextKey();
    Ogre::ConfigFile::SettingsMultiMap* settings = seci.getNext();
    Ogre::ConfigFile::SettingsMultiMap::iterator i;

    for (i = settings->begin(); i != settings->end(); ++i) {
        typeName = i->first;
        archName = i->second;
        Ogre::ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
    }
}


// do not forget to call the base first
ApplicationContextQt::setup();
addInputListener(this);

// get a pointer to the already created root
Root* root = getRoot();
SceneManager* scnMgr = root->createSceneManager();

// register our scene with the RTSS
RTShader::ShaderGenerator* shadergen = RTShader::ShaderGenerator::getSingletonPtr();
shadergen->addSceneManager(scnMgr);

// -- tutorial section start --
//! [turnlights]
scnMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
//! [turnlights]

//! [newlight]
Light* light = scnMgr->createLight("MainLight");
SceneNode* lightNode = scnMgr->getRootSceneNode()->createChildSceneNode();
lightNode->attachObject(light);
//! [newlight]

//! [lightpos]
lightNode->setPosition(20, 80, 50);
//! [lightpos]

//! [camera]
SceneNode* camNode = scnMgr->getRootSceneNode()->createChildSceneNode();

// create the camera
Camera* cam = scnMgr->createCamera("myCam");
cam->setNearClipDistance(5); // specific to this sample
cam->setAutoAspectRatio(true);
camNode->attachObject(cam);
camNode->setPosition(0, 0, 140);

// and tell it to render into the main window
getRenderWindow()->addViewport(cam);
//! [camera]

//! [entity1]
Entity* ogreEntity = scnMgr->createEntity("ogrehead.mesh");
//! [entity1]

//! [entity1node]
SceneNode* ogreNode = scnMgr->getRootSceneNode()->createChildSceneNode();
//! [entity1node]

//! [entity1nodeattach]
ogreNode->attachObject(ogreEntity);
//! [entity1nodeattach]

//! [cameramove]
camNode->setPosition(0, 47, 222);
//! [cameramove]

//! [entity2]
Entity* ogreEntity2 = scnMgr->createEntity("ogrehead.mesh");
SceneNode* ogreNode2 = scnMgr->getRootSceneNode()->createChildSceneNode(Vector3(84, 48, 0));
ogreNode2->attachObject(ogreEntity2);
//! [entity2]

//! [entity3]
Entity* ogreEntity3 = scnMgr->createEntity("ogrehead.mesh");
SceneNode* ogreNode3 = scnMgr->getRootSceneNode()->createChildSceneNode();
ogreNode3->setPosition(0, 104, 0);
ogreNode3->setScale(2, 1.2, 1);
ogreNode3->attachObject(ogreEntity3);
//! [entity3]

//! [entity4]
Entity* ogreEntity4 = scnMgr->createEntity("ogrehead.mesh");
SceneNode* ogreNode4 = scnMgr->getRootSceneNode()->createChildSceneNode();
ogreNode4->setPosition(-84, 48, 0);
ogreNode4->roll(Degree(-90));
ogreNode4->attachObject(ogreEntity4);
//! [entity4]

// -- tutorial section end --
}


bool BasicTutorial1::keyPressed(const KeyboardEvent& evt)
{
    if (evt.keysym.sym == SDLK_ESCAPE)
    {
        getRoot()->queueEndRendering();
    }
    return true;
}


int main(int argc, char **argv)
{
    QApplication qapp(argc, argv);
    auto* mainwindow = new QMainWindow();
    auto* label = new QLabel("some text");
    auto* window = new QWindow();
    auto* container = new QWidget();
    BasicTutorial1 app;
    // container->createWindowContainer(window);
    auto* placeholder = new QWidget();
    placeholder->setFixedWidth(600);
    placeholder->setFixedHeight(600);
    auto* layout = new QHBoxLayout(placeholder);
    layout->addWidget(label);
    layout->addWidget(container);
    mainwindow->setCentralWidget(placeholder);
    mainwindow->show();


window->show();
app.injectMainWindow(window);
app.initApp();
app.startTimer(1000);

qapp.exec();

return 0;
}

//! [fullsource]
paroj
OGRE Team Member
OGRE Team Member
Posts: 2106
Joined: Sun Mar 30, 2014 2:51 pm
x 1132

Re: OgreApplicationContextQt disappeared after embeded in widget by createWindowContainer(window)

Post by paroj »

raner
Gnoblar
Posts: 2
Joined: Sat May 25, 2024 4:48 pm
x 1

Re: OgreApplicationContextQt disappeared after embeded in widget by createWindowContainer(window)

Post by raner »

paroj wrote: Sat May 25, 2024 10:23 pm

did you see this discussion? https://github.com/OGRECave/ogre/issues/1583

Thank you for your reply! The link you provided was very helpful. I created a simpler working example based on the information from the discussion and uploaded it to GitHub.

https://github.com/ran-err/tryOgreQt

Although it is not fully functional, I hope it can further assist others who are struggling with OgreBites::ApplicationContextQt.

Here is the link from the discussion that I referred to:
https://github.com/OGRECave/spacescape/ ... peWidget.h
https://github.com/OGRECave/spacescape/ ... Widget.cpp