Results 1 to 4 of 4

Thread: This program runs, but does not work properly

  1. #1
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default This program runs, but does not work properly

    Hello,This program runs, but does not work properly.I copied the codes from this link and Because I have Qt 5.2.1,I changed FALSE to false and TRUE to true.Also I used from the Q_UNUSED for prevent warning.However,the program runs but does not work properly and does not work the game and Only opens a window. Thanks in advance.
    Untitled40.jpg

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: This program runs, but does not work properly

    Did you also copy the PNG files for the images of the paddle, brick, and ball?

    Qt Code:
    1. image.load( "xxx.png" );
    To copy to clipboard, switch view to plain text mode 

    These statements in the paddle, ball, and brick constructors are probably failing and as a result the images are not valid. There is nothing for the painter to paint in the paintEvent().

    If you did add the images, are they in a location where the EXE can find them at run-time to load them properly? The program is expecting these to be in the same directory as the exe. A better solution would be to add the images as resources to your project, and then load them using a resource URL. See the Qt Resource System documentation.
    Last edited by d_stranz; 14th September 2014 at 17:25.

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

    rezas1000 (14th September 2014)

  4. #3
    Join Date
    Jul 2014
    Posts
    95
    Thanks
    67

    Default Re: This program runs, but does not work properly

    Did you also copy the PNG files for the images of the paddle, brick, and ball?
    No.why?I didn't see the PNG files in the link.
    If you did add the images, are they in a location where the EXE can find them at run-time to load them properly?
    No.only I copied the codes from the link and I didn't see nothing else in the link.what do you mean?Can you explain more? thanks

  5. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: This program runs, but does not work properly

    As I said, the Paddle, Brick, and Ball classes each have a QImage member variable called "image". In the constructor for each class, the image is loaded from a PNG file, like this (from the Ball Class):

    Qt Code:
    1. Ball::Ball()
    2. {
    3.  
    4. xdir = 1;
    5. ydir = -1;
    6.  
    7. image.load("ball.png"); // << This code, right here, tries to load a PNG file called "ball.png"
    8.  
    9. rect = image.rect();
    10. resetState();
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    If the file cannot be found, then there is no image loaded. In that case, the image member variables are not valid, and when passed to the painter for drawing on the screen, nothing happens:

    Qt Code:
    1. void Breakout::paintEvent(QPaintEvent *event)
    2. {
    3. QPainter painter(this);
    4.  
    5. // If / else code omitted because this is
    6. // the only code that matters in this case:
    7.  
    8. else {
    9. painter.drawImage(ball->getRect(),
    10. ball->getImage()); // << ball->getImage() returns an invalid image
    11. painter.drawImage(paddle->getRect(),
    12. paddle->getImage()); // << so does paddle->getImage()
    13.  
    14. for (int i=0; i<30; i++) {
    15. if (!bricks[i]->isDestroyed())
    16. painter.drawImage(bricks[i]->getRect(),
    17. bricks[i]->getImage()); // << and bricks[i]->getImage()
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 

    So, you can't just blindly copy code from somewhere without studying it to see what it is trying to do. If the web site where you found that code has a download link that will let you download the complete project (in other words, not just a copy and paste), then that download probably contains the image files you need.

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

    rezas1000 (15th September 2014)

Similar Threads

  1. Program runs from Creator, but not as a standalone?
    By N3wb in forum Qt Programming
    Replies: 12
    Last Post: 8th October 2012, 23:11
  2. Replies: 1
    Last Post: 7th March 2012, 21:34
  3. My showevent does not work properly.
    By tonnot in forum Newbie
    Replies: 7
    Last Post: 18th February 2011, 07:18
  4. Program runs on Windows 7, crashes on Windows XP
    By JovianGhost in forum General Programming
    Replies: 8
    Last Post: 7th June 2010, 00:55
  5. Replies: 7
    Last Post: 4th June 2010, 14:52

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.