PDA

View Full Version : drawText problem



happylucy
14th April 2006, 00:29
I tried to draw a text but the text didnot appear on screen. Can anyone help me out?
Thanks in advance.

Happylucy



void MyMainWindow::paintEvent( QPaintEvent * )
{
QPainter p;

//
p.begin( this );
QFont f( "courier", 14, QFont::Bold );
QColor background = backgroundColor();
p.setPen( Qt::blue );
p.setFont( f );
p.fillRect(0, 0, width(), height(), Qt::cyan);
p.flush();

QFontMetrics fm = p.fontMetrics();
nMaxTextWidth = fm.width("Hello");
nMaxTextHeight = fm.height();


p.drawText( 10, 10, "Hello");
p.flush();

//
fflush(stdout);
p.end();
// exit
exit(EXIT_SUCCESS);
}

jacek
14th April 2006, 00:37
p.drawText( 10, 10, "Hello");
(10, 10) is the lower-left corner of your text. If the window is smaller, you might not see the text.


// exit
exit(EXIT_SUCCESS);
Why do you exit from your application in paintEvent()? Your program will close itself before you will be able to see anything.

happylucy
14th April 2006, 00:43
(10, 10) is the lower-left corner of your text. If the window is smaller, you might not see the text.

Even I use (100,100), I still have the same problem.

Why do you exit from your application in paintEvent()? Your program will close itself before you will be able to see anything.

Actually I used a loop to draw 1,000,000 same texts before exit. I still cannot see any text.

jacek
14th April 2006, 01:18
Actually I used a loop to draw 1,000,000 same texts before exit. I still cannot see any text.
You don't need any loops and you shouldn't call exit.

Here is a small example:
#include <qapplication.h>
#include <qfont.h>
#include <qpainter.h>
#include <qwidget.h>

class Test : public QWidget
{
public:
Test( QWidget *parent = 0 ) : QWidget( parent ) {}

protected:
void paintEvent( QPaintEvent * ) {
QPainter p( this );
p.setPen( Qt::blue );
p.setFont( QFont( "Courier", 14, QFont::Bold ) );
p.fillRect( 0, 0, width(), height(), Qt::cyan );
p.drawText( 20, 20, "Hello" );
}
};

int main( int argc, char **argv )
{
QApplication app( argc, argv );

Test t;
t.show();

QObject::connect( qApp, SIGNAL( lastWindowClosed() ),
qApp, SLOT( quit() ) );

return app.exec();
}

happylucy
14th April 2006, 01:42
Thank you very much, jacek.
I tried but still nothing showed up. I was running the test on an embedded device. Is it possible that I missed some library? What settings should I set properly so that I can draw texts?

jacek
14th April 2006, 01:45
Do you actually see the window? Does your application start at all? Which Qt version do you use?

happylucy
14th April 2006, 01:51
Yes, I can see the window and can see the color of the background changed.
I am using the version 2.3.8 of Qt.
Actually I can draw rectangle and can see it. But I cannot see text.

jacek
14th April 2006, 01:59
Are there any fonts installed on that device?

http://doc.trolltech.com/3.0/emb-fonts.html

happylucy
14th April 2006, 02:03
Thank you so much, jacek. You are very helpful.
I will take a look at the fonts issue and will update with you later.

happylucy
15th April 2006, 03:14
I checked the fonts. I was using the available font on the device but still cannot see the text. so frustrated......

jacek
15th April 2006, 14:25
What does QFont::family(), QFont::pixelSize() and QFont::rawName() return? Did you try to use QApplication::font()? Maybe there is not enough memory to render the font?

PS. Moving to Qtopia forum

happylucy
20th April 2006, 01:31
drawText worked after I used our own application class inherited from QApplication.
Thank you, jacek.