You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Tuesday, January 22nd 2008, 12:26pm

QPainter

Hello

I am trying to do quite a simple thing. I just want to write the text "My Text" on a widget. But when I execute this piece of code, which in my opinion was the solution, I see only the widget but nothing is written on that. Can anyone tell me what have I done wrong? Here is the code

#include <qapplication.h>
#include <qpainter.h>
#include <qwidget.h>

int main(int argc, char **argv)
{
QApplication a(argc, argv);
QWidget w;
QPainter p(&w);
p.drawText(w.rect(), Qt::AlignHCenter, "My Text" );
w.show();

return a.exec();
}
Thanks beforehand,
Lusy

macabre13

Trainee

  • "macabre13" is male

Posts: 127

Location: Poland/ Wroclaw

Occupation: sw developer

  • Send private message

2

Tuesday, January 22nd 2008, 1:34pm

I achieved this by

Source code

1
2
3
4
5
6
7
8
9
class MyWidget:
   public QWidget
{
   void paintEvent( QPaintEvent *event )
   {
      QPainter painter(this);
      painter.drawText(rect(), Qt::AlignCenter, "Qt");
   }
};


in main.cpp

Source code

1
2
MyWidget w;
s.show();
- ebm - noise - industrial -
I always could be wrong.
Lets share knowledge!

3

Tuesday, January 22nd 2008, 1:42pm

Thanks for your answer Macabre
Thanks beforehand,
Lusy