Sign in to follow this  
kszewczyk

Integration with Qt

Recommended Posts

Hello all,
I would like to integrate my framework with Qt
for example I want to achive something like this:


void QWidget::paintEvent(QPaintEvent* e)
{
myFramework->render()
}

where method render look like:

while(true)
{
update();
draw();
}


but I dont have a clue, how can I do this :(

I will be grateful for any hint.

Best Regards
kszewczyk

Share this post


Link to post
Share on other sites
Qt has it's own main loop, and from what I understand it is very hard or not possible to bypass that. However, you can use a QTimer with interval set to 0 so it triggers every frame. Inside the timer event function you call your update function and after that the repaint() function of the widget. The repaint function will trigger the paintEvent function immediately, from which you can then repaint the window.

Share this post


Link to post
Share on other sites
I want to be able to paint on QWidget with my framework,my gole is to do something similar to Ogre Engine:


QPaintEngine* paintEngine() const { return NULL; }

void GLWidget::paintEvent(QPaintEvent *e)
{
ogreRoot->_fireFrameStarted();
ogreRenderWindow->update();
ogreRoot->_fireFrameEnded();

e->accept();
}


basically for now I have render module, core module and resources module, the core module is responsible to connect all of modules and provide framework API to user.

Share this post


Link to post
Share on other sites
When creating your framework's graphics device you need to pass the widget's platform handle to it. This is done so that the device can actually render to the right window. You obtain it the handle by calling the following function: QWidget::winID.

The second thing is to update the logic at a certain frequency, probably 30hz. This can be done by using a timer that runs every 33ms. This timer also causes the widget to paint itself (by calling update) so that the framework can paint to the window (painting outside paintEvent is never a good idea).

I usually create a widget for that purpose:

class CustomWidget : public QWidget
{
Q_OBJECT

QTimer m_timer;
YourFramework* m_framework;

CustomWidget(QWidget* parent)
: QWidget(parent)
{
QObject::connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateFramework()));
m_timer.start(33);

m_framework = new YourFrame(winId());
}

virtual ~CustomWidget()
{
delete m_framework.
}

private slots:

void updateFramework()
{
m_framework->update();
update(); //< Posts a QPaintEvent in the main loop
}

protected:

virtual void paintEvent(QPaintEvent* event)
{
m_framework->render();
event->accept();
}
};



Usually you need to handle a QResizeEvent as well, to resize frame buffers and the like.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this