Solved QPainter won't draw lines or images, just text
-
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:
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.
-
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 afterpainter.beginNativePainting();
finally add the correspondingglDisable
just beforepainter.endNativePainting();
and you should be good to go. -
Hi,
Can you provide a minimal compilable example ?
Which version of macOS are you using ? -
-
No it doesn't fit the definition of minimal but it builds successfully.
However there seems to be stuff missing for the Resources part.
-
@SGaist Whoops, sorry, I’ll check the resources and fix it. I was trying to reduce the size, may have been over-zealous...
-
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 :)
-
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 afterpainter.beginNativePainting();
finally add the correspondingglDisable
just beforepainter.endNativePainting();
and you should be good to go. -
@SGaist claps enthusiastically Oh well done! Thank you very much - I doubt I would have found that :)
Cheers!