Reputation: 109
I have been working on learning how to draw text to the screen with QT
and I just can't figure out why it isn't drawing the text.
This is the code that I use:
#include "dialog.h"
int main(int argc, char ** argv)
{
QApplication a(argc, argv);
Dialog w;
w.show();
QPainter painter(&w);
painter.drawText(100,100,"hello");
return a.exec();
}
It does work when I have it in the dialog class, when I override the paintEvent
function, but should it not work if it is in the main function as well?
Upvotes: 0
Views: 1809
Reputation: 19102
It is well documented that you should only paint inside the paintEvent
.
http://qt-project.org/doc/qt-5/QPainter.html#details
http://qt-project.org/doc/qt-5/qwidget.html#custom-widgets-and-painting
http://qt-project.org/doc/qt-5/qwidget.html#paintEvent
http://qt-project.org/faq/answer/how_can_i_paint_outside_the_paintevent
You can get around the issue by using a QPixmap
as a QPaintDevice
and paint onto the QPixmap
and then render it in the paintEvent
function. Also if you are attempting to use QPixmap outside of the main GUI thread, you will get runtime warnings and maybe some crashes. To use images outside of the GUI thread, you need to use QImage
, and then when you are back on your GUI thread, you can convert it to a QPixmap
.
Hope that helps.
Upvotes: 2