Results 1 to 12 of 12

Thread: drawText problem

  1. #1

    Default drawText problem

    I tried to draw a text but the text didnot appear on screen. Can anyone help me out?
    Thanks in advance.

    Happylucy



    void MyMainWindow:aintEvent( 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);
    }

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawText problem

    Quote Originally Posted by happylucy
    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.

    Quote Originally Posted by happylucy
    // 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.

  3. #3

    Default Re: drawText problem

    Quote Originally Posted by jacek
    (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.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawText problem

    Quote Originally Posted by happylucy
    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:
    Qt Code:
    1. #include <qapplication.h>
    2. #include <qfont.h>
    3. #include <qpainter.h>
    4. #include <qwidget.h>
    5.  
    6. class Test : public QWidget
    7. {
    8. public:
    9. Test( QWidget *parent = 0 ) : QWidget( parent ) {}
    10.  
    11. protected:
    12. void paintEvent( QPaintEvent * ) {
    13. QPainter p( this );
    14. p.setPen( Qt::blue );
    15. p.setFont( QFont( "Courier", 14, QFont::Bold ) );
    16. p.fillRect( 0, 0, width(), height(), Qt::cyan );
    17. p.drawText( 20, 20, "Hello" );
    18. }
    19. };
    20.  
    21. int main( int argc, char **argv )
    22. {
    23. QApplication app( argc, argv );
    24.  
    25. Test t;
    26. t.show();
    27.  
    28. QObject::connect( qApp, SIGNAL( lastWindowClosed() ),
    29. qApp, SLOT( quit() ) );
    30.  
    31. return app.exec();
    32. }
    To copy to clipboard, switch view to plain text mode 

  5. #5

    Default Re: drawText problem

    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?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawText problem

    Do you actually see the window? Does your application start at all? Which Qt version do you use?

  7. #7

    Default Re: drawText problem

    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.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawText problem

    Are there any fonts installed on that device?

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

  9. #9

    Default Re: drawText problem

    Thank you so much, jacek. You are very helpful.
    I will take a look at the fonts issue and will update with you later.

  10. #10

    Default Re: drawText problem

    I checked the fonts. I was using the available font on the device but still cannot see the text. so frustrated......

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: drawText problem

    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
    Last edited by jacek; 15th April 2006 at 14:28.

  12. #12

    Default Re: drawText problem

    drawText worked after I used our own application class inherited from QApplication.
    Thank you, jacek.

Similar Threads

  1. Problem in using QHttp with QTimer
    By Ferdous in forum Newbie
    Replies: 2
    Last Post: 6th September 2008, 13:48
  2. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 10:12
  3. Steps in solving a programming problem?
    By triperzonak in forum General Programming
    Replies: 8
    Last Post: 5th August 2008, 09:47
  4. problem with paint and erase in frame
    By M.A.M in forum Qt Programming
    Replies: 9
    Last Post: 4th May 2008, 21:17
  5. Replies: 16
    Last Post: 7th March 2006, 16:57

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.