Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. How to use QPaint object in QML?
Qt 6.11 is out! See what's new in the release blog

How to use QPaint object in QML?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
4 Posts 4 Posters 2.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abanksdev
    wrote on last edited by
    #1

    I'm referencing this doc: http://doc.qt.io/qt-5/qtquick-customitems-painteditem-example.html 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);
        customLoader.setProgress(50);
    

    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!

    1 Reply Last reply
    0
    • H Offline
      H Offline
      hsolter
      wrote on last edited by hsolter
      #2

      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.

      1 Reply Last reply
      0
      • AndySA Offline
        AndySA Offline
        AndyS
        Moderators
        wrote on last edited by
        #3

        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;
            w.render(painter);
        }
        

        But that is purely for demostration purposes only.

        Andy

        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        0
        • J Offline
          J Offline
          jw23578
          wrote on last edited by
          #4

          did you qmlregistertype your new qml type?
          https://doc.qt.io/qt-5/qtqml-cppintegration-definetypes.html

          jens

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved