Important: Please read the Qt Code of Conduct -

How to use QPaint object in QML?

  • I'm referencing this doc: but am still missing a crucial step I believe

    Basically, I'm making a custom loader using QPaint. I make a class for the custom loader, put all methods in the header and include the paint event in the corresponding cpp file.

    CustomLoader::CustomLoader(QQuickItem *parent)
        : QQuickPaintedItem(parent)
    Q_ASSERT(m_maxValue>=m_minValue); // make sure max is the same or more than the min
        QWidget::paintEvent(event); // call the base class implementation
        QPainter painter(this); // set a painter to paint on the widget
    void CustomLoader::paint(QPainter *painter){
    void CustomLoader::setProgress(int progress) //this sets the percentage of completed the loader is 

    I then use the application engine to setContextProperty

    CustomLoader customLoader(NULL);
        engine.rootContext()->setContextProperty("customLoader", customLoader);

    And get the following error:

    main.cpp:15: error: member access into incomplete type 'QQmlContext'
        engine.rootContext()->setContextProperty("customLoader", customLoader);

    my CustomLoader.qml is simply this:

    import QtQuick 2.0
    Item {
        width: 300
        height: 300

    I am very new to this and thus have a few questions.
    1.) Any ideas what is causing the error I am receiving about accessing an incomplete type?
    2.) How do I pass the CustomLoader paint object to the CustomLoader QML so that it displays in the QML element?

    Thank you!

  • maybe include <QQmlContext> in your main.cpp? . It seems it does not find the definition of the Context class.

    Look up, definition vs declaration, in a c++ book or online.

  • Moderators

    Hi @abanksdev

    It is not safe to have a widget inside QQuickPaintedItem since it is not supported to create a QWidget on anything but the GUI thread, and your QQuickPaintedItem might end up painting via the render thread. Therefore you should change it so that you don't need to create a QWidget just for the purpose of painting it in QQuickPaintedItem.

    In order to at least get the rendering to work, assuming you have changed the QWidget to be a different paint device, such as QImage for example. Then you would reimplement paint() and then use the QPainter passed in to render with. Although I would strongly advise against using the following, but in case it helps you could do:

    void CustomLoader::paint(QPainter *painter)
        MyWidget w;

    But that is purely for demostration purposes only.


  • did you qmlregistertype your new qml type?


Log in to reply