Important: Please read the Qt Code of Conduct -

Loading files into drawBackground(QPainter *painter, const QRectF &)

  • Hello everybody,

    currently i am trying to laod *.obj-files into my programm. My code is based on this example:

    "Accelerate your widgets with opengl (new 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");

    glClearColor(m_backgroundColor.redF(), m_backgroundColor.greenF(), m_backgroundColor.blueF(), 1.0f);
    if (m_model) {



    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
    float m_distance;
    GLWidget(const QString &filePath);
    GLWidget(QWidget *parent = 0);
    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>

    class GLWidget;

    class OpenGLScene : public QGraphicsScene

    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;
    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.
    Thanks in Advance

    EDIT: I have changed the upper link. The old link referred to an old version of the code which was not correct.

  • Hm...maybe i've asked too much. Maybe I should specify it more.

    My first question would be why it is a problem to define the modelfunctions in glwidget?
    GLWidget's content is the same as the model-class content plus some extras.
    Therefore I dont understand the error: “It is not safe to use pixmaps outside the GUI thread”

    Because in the OpenGLScene-Class I have nothing changed except the datatype-names. I've replaced Model-datatype to GLWidget-datatype.

    Maybe someone would be able to help me? Or is it so obvious that it is to easy to answer. If so I would be happy if you could share your ideas with me.

    Thank you

Log in to reply