OpenGL view in QML
Page |
1 |
Hi,
I’ve been trying to draw opengl stuff in a QML view. I write my plugin on the widget example. And I can’t make it work.
Here my code
- {
- Q_OBJECT
- public:
- protected:
- void paintGL()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- //something here
- }
- void resizeGL(int width, int height)
- {
- int side = qMin(width, height);
- glViewport((width - side) / 2, (height - side) / 2, side, side);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-0.5, +0.5, -0.5, +0.5, 4.0, 15.0);
- glMatrixMode(GL_MODELVIEW);
- }
- };
- {
- Q_OBJECT
- public:
- {
- widget = new GLWidget();
- setWidget(widget);
- }
- private:
- GLWidget *widget;
- };
- class QWidgetsPlugin : public QDeclarativeExtensionPlugin
- {
- Q_OBJECT
- public:
- void registerTypes(const char *uri)
- {
- qmlRegisterType<MyPushButton>(uri, 1, 0, "MyPushButton");
- }
- };
- #include "qwidgets.moc"
- Q_EXPORT_PLUGIN2(qmlqwidgetsplugin, QWidgetsPlugin);
I’ve also updated the .pro bu adding opengl. When I test it I’ve got nothing else than a white window. I’ve expected at least a black square somewhere on my window.
Did I do something wrong? I know this is possible since I’ve seen that [labs.qt.nokia.com]
At least, did someone know where I could the source code of the video?
Thanks
21 replies
QGraphicsProxyWidget is not compatible with QGLWidgets since the proxy widget uses the QWidget::render() function to render to a QPainter but OpenGL can’t be rendered to a QPainter.
If you create a QDeclarativeView backed by a QGLWidget viewport, you can create custom QML components which can render to OpenGL.
- QDeclarativeView *view = new QDeclarativeView;
- view->setViewport(glWidget);
Alternatively, you could use the QML/3D project which will do this for you and provide much more capabilities. The project is found at http://qt.gitorious.org/qt-labs/qt3d. The source code for the “Monkey God” video can be found in http://qt.gitorious.org/qt-labs/qt3d/trees/master/demos/declarative/monkeygod
I’ve failed for now. And after thinking I don’t see any difference between yours solutions.
I’ve tried that : (.hpp)
- #ifndef QWIDGETS_HPP
- #define QWIDGETS_HPP
- #include <QDeclarativeItem>
- class MDE : public QDeclarativeItem
- {
- Q_OBJECT
- public:
- MDE(QDeclarativeItem *parent = 0);
- };
- #endif // QWIDGETS_HPP
(cpp)
- #include "qwidgets.hpp"
- #include <qdeclarative.h>
- #include <QDeclarativeView>
- #include <QApplication>
- #include <QGLWidget>
- #include <QPainter>
- MDE::MDE(QDeclarativeItem *parent )
- {
- }
- {
- painter->beginNativePainting();
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- glBegin(GL_QUADS);
- glColor3ub(0,0,255);
- glVertex2d(-0.75,-0.75);
- glVertex2d(-0.75,0.75);
- glColor3ub(255,0,0);
- glVertex2d(0.75,0.75);
- glVertex2d(0.75,-0.75);
- glEnd();
- painter->endNativePainting();
- }
- int main(int argc, char *argv[])
- {
- qmlRegisterType<MDE>("MDEPlugins", 1, 0, "MDE");
- QDeclarativeView view;
- view.setViewport(glWidget);
- view.show();
- return app.exec();
- }
And that still doesn’t work. I’m not quite sure I’ve understand what setViewport does. Is it creating a valid context for my DeclarativeItem ? So why at least I haven’t my black square from the glclear.
I’ve also look at QML/3D project(I failed to build it an error occurs during link “cannot find -lQt3Dd”). Anyway, it does too much for me. I just need a valid context for my DeclarativeItem. I don’t want to controller the opengl content of my view or does something dynamic, I just want to draw static predefined vertex.
Thanks
Hi,
You are right, the approaches suggested are essentially the same (I just didn’t read Bradley’s answer closely enough before posting mine :-) )
The following works for me (in my case I replaced the current paint function of QDeclarativeRectangle with the following code):
- p->beginNativePainting();
- glBegin(GL_QUADS);
- glColor3ub(0,0,255);
- glVertex2d(0, 0);
- glVertex2d(0, height());
- glColor3ub(255,0,0);
- glVertex2d(width(), height());
- glVertex2d(width(), 0);
- glEnd();
- p->endNativePainting();
Does it work for you?
Regards,
Michael
Great, that work great! Thanks a lot.
For other user who may read that. The code I’ve posted earlier work with the modification of mbrasser. (I’ve used Qt 4.7.1 win/gcc)
I’ve got few last questions about the viewport. The default opengl projection is something like gluOrtho2D(0,width(),0,height()) ? If I want to change it, should I did it between the nativepainting’s call? How does the transformation affect opengl (rotation, z translation) ?
Thanks again for helping me build my opengl declarative item.
thank you for your fast response :)
Actually I am searching for a solution to a different problem.
I started a project to use QML (UI) and QT C++(Logic) on embedded devices (right now I am using the i.MX53).
My animation is quite simple: I am moving an image and fade it in and out.
During this animation tearing appears. OpenGL is used to benefit from hardware accerlation.
My idea is, to use following algorithm (it’s similar to qml scene graph) :
- while (animationIsRunning) {
- paintQMLScene();
- swapAndBlockForNextVSync();
- }
One of my problems is, how to do the blocking and generally to understand the communication between qml and qt c++ regarding painting.
Merinas,
Test.qml:3:1: OpenGLItem is not a type OpenGLItem{ ^
What am I doing wrong?
Could someone just .zip and email somekind of example project using OpenGL in QML.
I’m trying to get this [developer.qt.nokia.com] example to show inside a QML rectangle element.
I second what Merinas said.
I may not be the best Googler, but I have been searching for days for a simple example of OpenGL in QML. Everything I find is either incomplete or shows no QML at all, just C++ code with the Qt API included in it.
Specifically could someone point me to a simple, complete example of an OpenGL context implemented inside a QML element (such as a rectangle). Honestly I find it quite surprising that this isn’t spelled out in the documentation. Some of us really don’t get much from reading 3 page long class definitions like the ones in the Qt Documentation. Not to mention, that none of them say anything specifically about implementing QML with C++/OpenGL.
I will be forever in your debt if you can help me (and all the other new guys) by showing an example of how this is done.
I am posting this reply here on this thread, because I believe it belongs here. Although the original poster found an answer to his question, this thread is named OpenGL view in QML and there are only 5 lines of QML posted here and they are, at best, lonely and incomplete.
Cheers!
You must log in to post a reply. Not a member yet? Register here!





