Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QPainter in Qt3D (Qt 5.8) / QPaintedTextureImage

QPainter in Qt3D (Qt 5.8) / QPaintedTextureImage

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 4.3k Views 2 Watching
  • 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.
  • G Offline
    G Offline
    guy incognito
    wrote on last edited by guy incognito
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Just a wild guess: QPaintedTextureImage.

      Hope it helps

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • G Offline
        G Offline
        guy incognito
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • G Offline
            G Offline
            guy incognito
            wrote on last edited by
            #5

            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?

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • G Offline
                G Offline
                guy incognito
                wrote on last edited by guy incognito
                #7

                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)
                {
                 //???
                }
                
                jsulmJ 1 Reply Last reply
                0
                • G guy incognito

                  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)
                  {
                   //???
                  }
                  
                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @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");
                  }
                  

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  G 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @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");
                    }
                    
                    G Offline
                    G Offline
                    guy incognito
                    wrote on last edited by guy incognito
                    #9

                    @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

                    1 Reply Last reply
                    0
                    • G Offline
                      G Offline
                      guy incognito
                      wrote on last edited by
                      #10

                      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!

                      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