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 won't draw lines or images, just text
Qt 6.11 is out! See what's new in the release blog

QPainter won't draw lines or images, just text

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 2 Posters 1.4k 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.
  • S Offline
    S Offline
    Spaced Cowboy
    wrote on last edited by Spaced Cowboy
    #1

    Subject says it all really. I'm trying to figure out how to overlay graphics on top of the OpenGL widget, and following what it says ought to work, doesn't seem to. Here's the code in paintGL()

    /******************************************************************************\
    |* OpenGL Event : Render GL
    \******************************************************************************/
    void Widget::paintGL()
        {
        QPainter painter(this);
        painter.beginNativePainting();
    
        /**************************************************************************\
        |* Draw the map as a background. This fully covers the display, so no need
        |* to clear
        \**************************************************************************/
        Map *map = _game->map();
        map->draw();
    
        /**************************************************************************\
        |* Finally, render the back-buffer to the screen
        \**************************************************************************/
        int w                           = _screenSize.width();
        int h                           = _screenSize.height();
    
        QRect trect                     = QRect(0,0,w*_dpyScale,h*_dpyScale);
        QRect srect                     = QRect(0, 0, RENDER_WD, RENDER_HT);
        GLbitfield what                 = GL_COLOR_BUFFER_BIT;
        GLenum filter                   = GL_LINEAR;
        int which                       = GL_COLOR_ATTACHMENT0;
    
        QOpenGLFramebufferObject::blitFramebuffer(0,             // Target = default
                                                  trect,         // Target rect
                                                  map->render(), // source
                                                  srect,         // source rect
                                                  what,          // what to copy
                                                  filter,        // how to copy
                                                  which,         // which buffer to copy
                                                  0              // No restore
                                                  );
    
    
        painter.endNativePainting();
        painter.setFont(QFont("Arial", 30));
        painter.setPen(QColor("red"));
        painter.setCompositionMode(QPainter::CompositionMode_SourceOver);
        painter.drawText(40,40,"Testing");
        painter.drawLine(50,50,400,400);
    
        QPoint pt(500,500);
        painter.drawImage(pt, *(_game->map()->access()));
        painter.end();
        }
    
    

    which produces the image below:

    shows the results

    The 'Testing' comes up, but the line test, and the image test don't show at all. I did check the image to draw at 'pt' was valid - it saves out as a PNG quite readily.

    This running on an iMac Pro, with QT 5.14.2 installed, if that makes a difference.

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

      I don't know the internal details as to why it does not affect text drawing but it's the face culling that wreaks havoc on your non textual painting. Remove the enabling from initializeGL and add it right after painter.beginNativePainting(); finally add the corresponding glDisable just before painter.endNativePainting(); and you should be good to go.

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

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

        Hi,

        Can you provide a minimal compilable example ?
        Which version of macOS are you using ?

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

        S 1 Reply Last reply
        1
        • SGaistS SGaist

          Hi,

          Can you provide a minimal compilable example ?
          Which version of macOS are you using ?

          S Offline
          S Offline
          Spaced Cowboy
          wrote on last edited by Spaced Cowboy
          #3

          @SGaist

          Hi :)

          I don't know if it's truly minimal (it's about a dozen .cc files) but part of that is setting up the OpenGL system with FBOs, which is probably relevant. Oh, and this is macOS Catalina (10.15.4)

          Anyway it's here

          Thanks :)

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

            No it doesn't fit the definition of minimal but it builds successfully.

            However there seems to be stuff missing for the Resources part.

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

            S 1 Reply Last reply
            0
            • SGaistS SGaist

              No it doesn't fit the definition of minimal but it builds successfully.

              However there seems to be stuff missing for the Resources part.

              S Offline
              S Offline
              Spaced Cowboy
              wrote on last edited by
              #5

              @SGaist Whoops, sorry, I’ll check the resources and fix it. I was trying to reduce the size, may have been over-zealous...

              1 Reply Last reply
              0
              • S Offline
                S Offline
                Spaced Cowboy
                wrote on last edited by
                #6

                Ok, I see the problem :)

                There is a post-make build=step in my project file that copies the 'textures' directory to where the resource-manager expects to find them.

                /******************************************************************************\
                |* Load from the correct place for each platform
                \******************************************************************************/
                QString ResourceManager::resourcePath(void)
                	{
                #if defined(Q_OS_WIN)
                	return QApplication::applicationDirPath() + "/resources";
                #elif defined(Q_OS_OSX)
                	return QApplication::applicationDirPath() + "/../Resources/";
                #elif defined(Q_OS_LINUX)
                	return QApplication::applicationDirPath() + "/resources/";
                #else
                	return QApplication::applicationDirPath() + "/";
                #endif
                	}
                
                

                So in my case ...

                • on a Mac
                • building to a directory called 'build' within the project directory

                ... it's just "cp" with the arguments " -r ../templates Freya.app/Contents/Resources". It'll be different depending on how your build is set up and which machine you're building on, unfortunately.

                If the textures aren't in the right place, you'll get the blue screen when you run it, but they are actually all in that archive. If you set up a default build (again on a Mac), building the app then typing:

                cp -r textures ../build-Freya-Desktop_Qt_5_14_2_clang_64bit-Debug/Freya.app/Contents/Resources
                

                into the shell before running the app will work.

                Cheers :)

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

                  I don't know the internal details as to why it does not affect text drawing but it's the face culling that wreaks havoc on your non textual painting. Remove the enabling from initializeGL and add it right after painter.beginNativePainting(); finally add the corresponding glDisable just before painter.endNativePainting(); and you should be good to go.

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

                  S 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    I don't know the internal details as to why it does not affect text drawing but it's the face culling that wreaks havoc on your non textual painting. Remove the enabling from initializeGL and add it right after painter.beginNativePainting(); finally add the corresponding glDisable just before painter.endNativePainting(); and you should be good to go.

                    S Offline
                    S Offline
                    Spaced Cowboy
                    wrote on last edited by
                    #8

                    @SGaist claps enthusiastically Oh well done! Thank you very much - I doubt I would have found that :)

                    Cheers!

                    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