QPainter in Qt3D (Qt 5.8) / QPaintedTextureImage



  • Hi all,

    I just took a look at the new features of Qt 5.8 and saw that there were some changes for the QPainter:

    Qt 3D Module:
    Added a paint to texture feature using QPainter.

    Looking at the QPainter I wasn't able to find out how to do that. Can anyone help?


  • Lifetime Qt Champion

    Hi,

    Just a wild guess: QPaintedTextureImage.

    Hope it helps



  • That sounds good but I don't quite get what the implementation of paint() needs to look like


  • Lifetime Qt Champion

    Another wild guess: use QPainter like you would in a custom QWidget paint event.



  • So I created a PlaneEntity with a QTransform, QPlaneMesh and a QDiffuseMapMaterial. I subclassed QPaintedTextureImage and tried to implement the paint-method, added the QPaintedTextureImage to the material and the material to the PlaneEntity.
    The PlaneEntity stays black.

    I don't understand how the paint method works or what the painter is supposed to paint to.

    Could you provide an example where a colored shape as TextureImage gets drawn to a planeEntity?


  • Lifetime Qt Champion

    Sorry, no I can't. Like I wrote in my other two posts, these where just wild guesses to try to give you useful hints to help you go further.

    I'd recommend improving your thread title, it doesn't even mention Qt3D so it will not necessarily attract people with more experience with that module.

    You should also share your implementation of your paint method.



  • My paint-method consists of desperate shots in the dark so I better don't post it here.
    This is how the rest of (the important part) of my code looks like, maybe it's also faulty (I bet something needs to be done with the QPainter before passing it):

    planeEntity = new Qt3DCore::QEntity(rootEntity);
    
    planeMesh = new Qt3DExtras::QPlaneMesh;
    planeMesh->setWidth(2);
    planeMesh->setHeight(2);
    
    image = new TextureImage; //see below
    painter = new QPainter; //??
    image->paint(painter)   //??
    
    planeMaterial = new Qt3DExtras::QDiffuseMapMaterial; 
    planeMaterial->diffuse()->addTextureImage(image);
    
    planeEntity->addComponent(planeMesh);
    planeEntity->addComponent(planeMaterial);
    

    TextureImage is the subclassed QPaintedTextureImage with paint function:

    class TextureImage : public Qt3DRender::QPaintedTextureImage
    {
    public:    
        void paint(QPainter* painter);
    };
    
    void TextureImage::paint(QPainter* painter)
    {
     //???
    }
    

  • Moderators

    @guy-incognito You should pass the object on which you want to paint to the painter (http://doc.qt.io/qt-5/qpainter.html):

    void SimpleExampleWidget::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        painter.setPen(Qt::blue);
        painter.setFont(QFont("Arial", 30));
        painter.drawText(rect(), Qt::AlignCenter, "Qt");
    }
    


  • @jsulm said in QPainter in Qt3D (Qt 5.8) / QPaintedTextureImage:

    @guy-incognito You should pass the object on which you want to paint to the painter (http://doc.qt.io/qt-5/qpainter.html):

    void SimpleExampleWidget::paintEvent(QPaintEvent *)
    {
        QPainter painter(this);
        painter.setPen(Qt::blue);
        painter.setFont(QFont("Arial", 30));
        painter.drawText(rect(), Qt::AlignCenter, "Qt");
    }
    

    According to the doc, the paint method doesn't get passed a paintEvent, but a QPainter, plus I can't set a QPaintedTextureImage as PaintDevice

    void QPaintedTextureImage::paint(QPainter *painter)
    
    

    Edit: I found the Painted Item Example, where a QQuickPaintedItem gets used which seems to be similar, but QML specific. I implemented my paint method like this:

    void TextureImage::paint(QPainter *painter)
    {
        QBrush brush(QColor("#007430"));
        QRectF rectangle(10.0, 20.0, 80.0, 60.0);
    
        painter->setBrush(brush);
        painter->setPen(Qt::NoPen);
        painter->setRenderHint(QPainter::Antialiasing);
    
        painter->drawRoundedRect(rectangle, 20.0, 20.0);
    
    

    Running the program I get :

    QPainter::setBrush: Painter not active
    QPainter::setPen: Painter not active
    QPainter::setRenderHint: Painter must be active to set rendering hints



  • Ok at some point I edited out the size of the image and of course all of my following approaches were doomed. This feels pretty damn stupid.

    It now works with:

    image->setSize(QSize(100,100));
    

    and following implemantation (thanks to SO):

    void TextureImage::paint(QPainter* painter)
    {  
      painter->fillRect(0, 0, 100, 100, QColor(255, 255, 255));
      painter->setPen(QPen(QBrush(QColor(255, 0, 255)) ,10));
      painter->setBrush(QColor(0, 0, 255));
      painter->drawEllipse(0, 0, 100, 100);
    }
    

    Thanks for your help!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.