Results 1 to 6 of 6

Thread: Qt Tutorial #1 Add-on Part II

  1. #1
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Qt Tutorial #1 Add-on Part II

    Hi guys,

    For this tutorial here,

    http://doc.trolltech.com/4.2/tutorial-t11.html

    At the end of the tutorial, there's an exercise that says:

    "Change the color of the cannon when a shot is in the air."

    How do we achieve that? I tried it for like 2-3 hours, but still couldnt get it to work.

  2. #2
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Tutorial #1 Add-on Part II

    In the following function you have to see if the cannon is in the air, meaning autoShootTimer->isActive().
    Qt Code:
    1. void CannonField::paintCannon(QPainter &painter)
    2. {
    3. painter.setPen(Qt::NoPen);
    4. if( autoShootTimer->isActive() //bullet is in the air
    5. painter.setBrush( Qt::red );
    6. else //bullet is not in the air
    7. painter.setBrush(Qt::blue);
    8. painter.save();
    9. painter.translate(0, height());
    10. painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16);
    11. painter.rotate(-currentAngle);
    12. painter.drawRect(barrelRect);
    13. painter.restore();
    14. }
    To copy to clipboard, switch view to plain text mode 

    Regards

  3. The following user says thank you to marcel for this useful post:

    ShaChris23 (25th April 2007)

  4. #3
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Tutorial #1 Add-on Part II

    Hi,

    Has anybody tried this modification? I'm not sure if I did something, but this is what I have tried before and my cannon's color never changed each time I pressed the shoot button.

    Here's the simple code modification that didnt work for me:

    Qt Code:
    1. void CannonField::paintCannon( QPainter& painter )
    2. {
    3. painter.setPen( Qt::NoPen );
    4.  
    5. if( autoShootTimer->isActive() )//bullet is in the air
    6. {
    7. painter.setBrush( Qt::red );
    8. }
    9. else //bullet is not in the air
    10. {
    11. painter.setBrush(Qt::blue);
    12. }
    13.  
    14. painter.save();
    15. painter.translate(0, height());
    16. painter.drawPie(QRect(-35, -35, 70, 70), 0, 90 * 16 );
    17. painter.rotate(-currentAngle);
    18. painter.drawRect(barrelRect);
    19. painter.restore();
    20. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Tutorial #1 Add-on Part II

    Actually the cannon rect wasn't updated at repaint. Try this

    Qt Code:
    1. void CannonField::moveShot()
    2. {
    3. QRegion region = shotRect();
    4. ++timerCount;
    5.  
    6. QRect shotR = shotRect();
    7. QRect cannonR = cannonRect();
    8.  
    9. if (shotR.x() > width() || shotR.y() > height()) {
    10. autoShootTimer->stop();
    11. } else {
    12. region = region.unite(shotR);
    13. region = region.unite(cannonR);
    14. }
    15. update(region);
    16. }
    To copy to clipboard, switch view to plain text mode 


    Regards

  6. The following user says thank you to marcel for this useful post:

    ShaChris23 (25th April 2007)

  7. #5
    Join Date
    Apr 2007
    Posts
    62
    Thanks
    43
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Tutorial #1 Add-on Part II

    Hi Marcel,

    Thank you. Now that I added that code, the cannon changes from blue to red. However, it stays red even when the shot is off screen. Is there a remedy to re-paint it blue?

  8. #6
    Join Date
    Feb 2006
    Location
    Romania
    Posts
    2,744
    Thanks
    8
    Thanked 541 Times in 521 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Qt Tutorial #1 Add-on Part II

    Actually, it is, if you modify the code above a bit:

    Qt Code:
    1. void CannonField::moveShot()
    2. {
    3. QRegion region = shotRect();
    4. ++timerCount;
    5.  
    6. QRect shotR = shotRect();
    7. QRect cannonR = cannonRect();
    8. if (shotR.x() > width() || shotR.y() > height()) {
    9. autoShootTimer->stop();
    10. } else {
    11. region = region.unite(shotR);
    12. }
    13. region = region.unite( cannonR );
    14. update(region);
    15. }
    To copy to clipboard, switch view to plain text mode 


    This way the cannon will be updated even after the timer is stopped ( the bullet is off-screen ). If the timer is stopped, then the cannon will be painted with the original color.

    Regards.

  9. The following user says thank you to marcel for this useful post:

    ShaChris23 (27th April 2007)

Similar Threads

  1. QDevelop QT Tutorial for beginners (Linux)
    By clive in forum General Discussion
    Replies: 8
    Last Post: 27th March 2007, 20:27
  2. Omitting private part of a class definition
    By Raistlin in forum General Programming
    Replies: 2
    Last Post: 23rd March 2007, 11:51
  3. Basic Qt4 tutorial needed
    By pthomas in forum Qt Programming
    Replies: 26
    Last Post: 16th October 2006, 15:11
  4. Q3ListView highlight part of a column
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 5th May 2006, 09:26
  5. Can't Compile Tutorial 7
    By Reenen in forum Newbie
    Replies: 10
    Last Post: 9th February 2006, 14:06

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.