currently i am trying to laod *.obj-files into my programm. My code is based on this example:
Accelerate your widgets with OpenGL(changed link)
The only differeence in my code, is that i have a glwidget-class, in which all the drawing is defined. In the class OpenGLScene only my Gui is defined. In the function drawBackground(QPainter *painter, const QRectF &) i call the function paintGL.
void OpenGLScene::drawBackground(QPainter *painter, const QRectF &)
{
if (painter->paintEngine()->type() != QPaintEngine::OpenGL
&& painter->paintEngine()->type() != QPaintEngine::OpenGL2)
{
qWarning("OpenGLScene: drawBackground needs a QGLWidget to be set as viewport on the graphics view");
return;
}
painter->beginNativePainting();
glClearColor(m_backgroundColor.redF(), m_backgroundColor.greenF(), m_backgroundColor.blueF(), 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (m_model) {
m_model->paintGL();
}
painter->endNativePainting();
}
Simple drawing and stuff works fine. I only get problems until i try to load *.obj-Files. I have tried several things, which didnt work.
Now I ended up with the solution to define all the functions from the model-class(which you can find in the example) in my glWidget-Class and then calling the render-function in paintGL. Afterwards calling the paintGL in OpenGLScene.
Here you can see my header-files: OpenGLScene.h and GLWidget.h:
class GLWidget : public QGLWidget
{
Q_OBJECT
public:
float m_distance;
GLWidget(const QString &filePath);
GLWidget(QWidget *parent = 0);
~GLWidget();
QSize minimumSizeHint() const;
QSize sizeHint() const;
void initializeGL();
void paintGL();
void resizeGL(int width, int height);
void render() const;
QString fileName() const { return m_fileName; }
int faces() const { return m_pointIndices.size() / 3; }
int edges() const { return m_edgeIndices.size() / 2; }
int points() const { return m_points.size(); }
QString m_fileName;
QVector<Point3d> m_points;
QVector<Point3d> m_normals;
QVector<int> m_edgeIndices;
QVector<int> m_pointIndices;
};
#ifndef QT_NO_CONCURRENT
#include <QFutureWatcher>
#endif
class GLWidget;
class OpenGLScene : public QGraphicsScene
{
Q_OBJECT
public:
OpenGLScene();
void drawBackground(QPainter *painter, const QRectF &rect);
QDialog *createDialog(const QString &windowTitle) const;
void setModel(GLWidget *model);
//Model *m_model;
GLWidget *m_model;
QColor m_backgroundColor;
float m_distance;
QLabel *m_labels[4];
QWidget *m_modelButton;
#ifndef QT_NO_CONCURRENT
QFutureWatcher<GLWidget *> m_modelLoader;
#endif
public slots:
void loadModel();
void loadModel(const QString &filePath);
void modelLoaded();
};
Unfortunately this idea isnt working, because i get this error:
“It is not safe to use pixmaps outside the GUI thread”
But I really dont understand whats that supposed to mean.
If you’ve moved all the
Modelstuff intoGLWidgetthen I assume you are now creating yourGLWidgetinstance in::loadModel? If so then be aware that this is being created in a new thread. It looks like it doesn’t automatically get moved into the main (GUI) thread when the loading thread finishes. What actually happens to the QObject is an open question.If you want to disable threading completely in order to simplify things then remove the lines
and
in openglscene.h. In the OpenGLScene constructor body remove the lines
In
OpenGLScene::loadModel(const QString &filePath)changeto just
Finally remove
from
OpenGLScene::modelLoaded().