Results 1 to 2 of 2

Thread: How to flush a QPainter in Qt 4?

  1. #1
    Join Date
    Jan 2009
    Posts
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default How to flush a QPainter in Qt 4?

    Hi, I've been working with the cannon game from the Qt tutorials, and as a learning experience I thought I'd try to extend it's capabilities. I added a simple points system for example, but now I was working on something more than just a one liner, so I thought that I could make it display a message every time you hit the target, to achieve it I did the following to cannonField.cpp:

    1.- Created a variable that is set to true when the projectile intersects with the target.

    Qt Code:
    1. if(shotR.intersects(targetRect())) {
    2. autoShootTimer->stop();
    3. targetContact = true; //Added by me
    4. emit hit();
    5. emit canShoot(true);
    6. }
    To copy to clipboard, switch view to plain text mode 

    2.- Added a new if on the paintEvent member function:

    Qt Code:
    1. void CannonField::paintEvent(QPaintEvent * /* event */) {
    2. QPainter painter(this);
    3.  
    4. if(gameEnded) {
    5. painter.setPen(Qt::black);
    6. painter.setFont(QFont("Courier", 18, QFont::Bold));
    7. painter.drawText(rect(), Qt::AlignCenter, tr("Game Over"));
    8. }
    9. /* My code */
    10. if(targetContact) {
    11. painter.setPen(Qt::black);
    12. painter.setFont(QFont("Courier", 20, QFont::Bold));
    13. painter.drawText(rect(), Qt::AlignCenter, tr("Hit!"));
    14. }
    15. /* End of my code */
    16. paintCannon(painter);
    17. paintBarrier(painter);
    18. if(isShooting())
    19. paintShot(painter);
    20. if(!gameEnded)
    21. paintTarget(painter);
    22. }
    To copy to clipboard, switch view to plain text mode 

    The problem with this is that with the code as it looks right now, on the first contact with the target the "Hit!" message appears on screen, but never goes away. So I started to explore possibilities and I came up with the idea of using a flush like you do with C for stdout, and found out that there was a .flush() for QPainter in Qt 3 (found this and this), but couldn't find it for Qt 4.

    With what I thought, the code would look something like this:

    Qt Code:
    1. void CannonField::paintEvent(QPaintEvent * /* event */) {
    2. ...
    3. if(targetContact) {
    4. painter.setPen(Qt::black);
    5. painter.setFont(QFont("Courier", 20, QFont::Bold));
    6. painter.drawText(rect(), Qt::AlignCenter, tr("Hit!"));
    7. painter.flush(); /* So the hit message is shown on screen. */
    8. sleep(1); /* Wait a sec before removing it.*/
    9. targetContact = false; /* Set the variable back to false, so the message disappears. */
    10. }
    11. ...
    To copy to clipboard, switch view to plain text mode 

    Is there a way to achieve this? I guess there must be, I just don't know how. And actually, would that solve it?

    Another alternative I thought of was to locate the text on a newly defined QRect, and afterward to call the method eraseRect(). I didn't have the time to actually try that. Could that work?

    Thanks in advance
    Last edited by choucete; 3rd July 2009 at 08:39. Reason: updated contents

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to flush a QPainter in Qt 4?

    It doesn't go away because the area doesn't need to be repainted, it has nothing to do with flushing anything. Call QWidget::update() after some period of time (i.e. using a QTimer) and paintEvent() will be called again and if the flags are modified properly, the branch responsible for drawing the message will not be called.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. QPainter drawText with different language
    By lni in forum Qt Programming
    Replies: 4
    Last Post: 11th June 2009, 19:54
  2. QPainter with TreeView
    By Kal in forum Qt Programming
    Replies: 1
    Last Post: 25th September 2008, 07:04
  3. QPainter update()
    By csvivek in forum Qt Programming
    Replies: 5
    Last Post: 24th March 2008, 09:42
  4. QPainter and QImage
    By beerkg in forum Newbie
    Replies: 3
    Last Post: 7th September 2006, 14:48
  5. Replies: 7
    Last Post: 20th March 2006, 20:03

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.