Results 1 to 11 of 11

Thread: Qt Tutorial #1 Add-on

  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

    Hi guys,

    For this tutorial here,

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

    Does someone know how to do this?

    "Modify the program such that the X Y location of each shot bullet is displayed on the GUI on the top right. X Y should become 0, 0 once the shot is completely gone. The definition of gone is not just the bullet being off screen since it's possible for a bullet to go off the screen and fall down."

    And, no, this is not a homework! I'm just curious how such feature would be implemented!

  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

    Sure...
    First you have to create two labels in the GUI ( or just one ) - one for X and one for Y.
    The moving of the bullet is made in CannonField::moveShot(). You should emit a signal from here with the x and y position. The signal should be connected to a slot in the dialog that just updates the labels with the X and Y values.

    Qt Code:
    1. void CannonField::moveShot()
    2. {
    3. QRegion region = shotRect();
    4. ++timerCount;
    5.  
    6. QRect shotR = shotRect();
    7.  
    8. if (shotR.x() > width() || shotR.y() > height()) {
    9. autoShootTimer->stop();
    10. } else {
    11. region = region.unite(shotR);
    12. }
    13. update(region);
    14. emit updateBulletPosition(shotRect().topLeft().x(), shotRect().topLeft().y() );
    15. }
    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 (20th April 2007)

  4. #3
    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

    You have to create the labels in main.cpp:
    Qt Code:
    1. QHBoxLayout *topLayout = new QHBoxLayout;
    2. topLayout->addWidget(shoot);
    3. topLayout->addStretch(1);
    4. QLabel* mxLabel = new QLabel( this );
    5. QLabel* myLabel = new QLabel( this );
    6. topLayout->addWidget( xLabel );
    7. topLayout->addWidget( yLabel );
    8. connect( cannonField, SIGNAL( updateBulletPosition(int,int) ), this, SLOT( setBulletPosition(int, int ) ) );
    9.  
    10. QVBoxLayout *leftLayout = new QVBoxLayout;
    11. leftLayout->addWidget(angle);
    12. leftLayout->addWidget(force);
    13.  
    14. QGridLayout *gridLayout = new QGridLayout;
    15. gridLayout->addWidget(quit, 0, 0);
    16. gridLayout->addLayout(topLayout, 0, 1);
    17. gridLayout->addLayout(leftLayout, 1, 0);
    18. gridLayout->addWidget(cannonField, 1, 1, 2, 1);
    19. gridLayout->setColumnStretch(1, 10);
    20. setLayout(gridLayout);
    21.  
    22. angle->setValue(60);
    23. force->setValue(25);
    24. angle->setFocus();
    To copy to clipboard, switch view to plain text mode 

    In the MyWidget class declaration:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget(QWidget *parent = 0);
    5.  
    6. public slots:
    7. void setBulletPosition( int x, int y );
    8.  
    9. private:
    10. QLabel* mxLabel;
    11. QLabel* myLabel;
    12. };
    To copy to clipboard, switch view to plain text mode 

    the slot:
    Qt Code:
    1. void MyWidget::setBulletPostion( int x, int y )
    2. {
    3. mxLabel->setText( QVariant(x).toString() );
    4. myLabel->setText( QVariant(y).toString() );
    5. }
    To copy to clipboard, switch view to plain text mode 

    This is it, pretty much. I'm not sure if the labels will appear correctly ( int the top-right corner ), but that can be fixed easily.

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

    ShaChris23 (20th April 2007)

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

    Question Re: Qt Tutorial #1 Add-on

    Hi Marcel,

    Thanks for a very helpful post. For some reason though, when I run the program, Qt reports the following:

    Object::connect: No such slot QWidget::setBulletPosition(int,int)

    I also dont see the x y label on the top right. Here's my current code:


    From MyWidget constructor
    Qt Code:
    1. QHBoxLayout* topLayout = new QHBoxLayout;
    2. topLayout->addWidget(shoot);
    3. topLayout->addStretch(1);
    4.  
    5. QLabel* mxLabel = new QLabel( this );
    6. QLabel* myLabel = new QLabel( this );
    7.  
    8. topLayout->addWidget( mxLabel );
    9. topLayout->addWidget( myLabel );
    10. connect( cannonField, SIGNAL( updateBulletPosition( int, int ) ), this, SLOT( setBulletPosition( int, int ) ) );
    To copy to clipboard, switch view to plain text mode 

    From MyWidget class declaration
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. public:
    4. MyWidget( QWidget* parent = 0 );
    5.  
    6. public slots:
    7. void setBulletPosition( int x, int y );
    8.  
    9. private:
    10. QLabel* mxLabel;
    11. QLabel* myLabel;
    12. };
    To copy to clipboard, switch view to plain text mode 

    From MyWidget::setBulletPosition( int x, int y )
    Qt Code:
    1. void MyWidget::setBulletPosition( int x, int y )
    2. {
    3. mxLabel->setText( QVariant(x).toString() );
    4. myLabel->setText( QVariant(y).toString() );
    5. }
    To copy to clipboard, switch view to plain text mode 


    From CannonField::moveShot()
    Qt Code:
    1. void CannonField::moveShot()
    2. {
    3. //
    4. QRegion region = shotRect();
    5. ++timerCount;
    6.  
    7. // Get the new shot
    8. QRect shotR = shotRect();
    9.  
    10. if( shotR.x() > width() || shotR.y() > height() )
    11. {
    12. autoShootTimer->stop();
    13. }
    14. else
    15. {
    16. region = region.unite(shotR);
    17. }
    18.  
    19. // Repaint the QRegion
    20. update(region);
    21. emit updateBulletPosition( shotRect().topLeft().x(), shotRect().topLeft().y() );
    22. }
    To copy to clipboard, switch view to plain text mode 

    Please let me know if I missed anything...I cant think of a reason why this doesnt work.

  7. #5
    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: Qt Tutorial #1 Add-on

    Add the Q_OBJECT macro:
    Qt Code:
    1. class MyWidget : public QWidget
    2. {
    3. Q_OBJECT
    4. public:
    5. ...
    To copy to clipboard, switch view to plain text mode 
    and re-run qmake and make.

  8. The following user says thank you to jacek for this useful post:

    ShaChris23 (20th April 2007)

  9. #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

    Did you declare the signal updateBulletPosition, in the CannonField class declaration?
    Should be something like:
    Qt Code:
    1. class CanonField : ....
    2. {
    3. ...
    4. signals:
    5. void updateBulletPosition( int, int );
    6. ...
    7. }
    To copy to clipboard, switch view to plain text mode 

    Regards

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

    ShaChris23 (20th April 2007)

  11. #7
    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

    Hi Marcel,

    Yes, here's my CannonField class declaration:

    Qt Code:
    1. class CannonField : public QWidget
    2. {
    3. Q_OBJECT
    4.  
    5. public:
    6. CannonField( QWidget* parent = 0 );
    7.  
    8. int angle() const { return currentAngle; }
    9. int force() const { return currentForce; }
    10.  
    11. public slots:
    12. void setAngle( int angle );
    13. void setForce( int force );
    14. void shoot();
    15.  
    16. private slots:
    17. void moveShot();
    18.  
    19. signals:
    20. void angleChanged( int newAngle );
    21. void forceChanged( int newForce );
    22. void updateBulletPosition( int x, int y );
    23.  
    24. protected:
    25. void paintEvent( QPaintEvent* event );
    26.  
    27. private:
    28. void paintShot( QPainter& painter );
    29. void paintCannon( QPainter& painter );
    30.  
    31. QRect cannonRect() const;
    32. QRect shotRect() const;
    33.  
    34. int currentAngle;
    35. int currentForce;
    36.  
    37. int timerCount;
    38. QTimer* autoShootTimer;
    39. float shootAngle;
    40. float shootForce;
    41. };
    To copy to clipboard, switch view to plain text mode 

  12. #8
    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

    Hmm..this is strange. I put in Q_OBJECT into MyWidget class declaration, and it doesnt complain anymore. But when I press shoot, the program crashes at

    qstring.h

    here -->
    Qt Code:
    1. inline int size() const { return d->size; }
    To copy to clipboard, switch view to plain text mode 

    under class

    Qt Code:
    1. class Q_CORE_EXPORT QString
    To copy to clipboard, switch view to plain text mode 

    But when I take out Q_OBJECT, the program doesnt crash. Of course, the x y coordinate still doesnt show up when I press the shoot button.

    I then looked around a little bit, turns out these 2 lines caused the crash:

    Qt Code:
    1. void MyWidget::setBulletPosition( int x, int y )
    2. {
    3. mxLabel->setText( QVariant(x).toString() );
    4. myLabel->setText( QVariant(y).toString() );
    5. }
    To copy to clipboard, switch view to plain text mode 

    Once I comment out those two lines, it doesnt crash anymore. Do you happen to know why?

    I also tried putting a cout debugging like this:

    Qt Code:
    1. void MyWidget::setBulletPosition( int x, int y )
    2. {
    3. cout << x << " " << y << "\n";
    4. }
    To copy to clipboard, switch view to plain text mode 

    and I saw that the x y are "real" numbers, not some garbage values.

    I'm using Qt 4.2.3 with VS STUDIO 05.

  13. #9
    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

    Are mxLabel and myLabel valid pointers when you try to set their value?
    Do you delete them somehow after you create them and add them to the layout?

    Because I can't really see how it could crash in those lines...

    Try not setting the actual coordinate. Call the setText methods for a constant string, "1" for example. See what it does then. If it still crashes, then your two pointers gost somehow corrupted.

    Regards

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

    ShaChris23 (24th April 2007)

  15. #10
    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

    Hi Marcel,

    I'm sorry to waste your time...the bug was in my MyWidget's constructor

    where

    Qt Code:
    1. QLabel* mxLabel = new QLabel( this );
    2. QLabel* myLabel = new QLabel( this );
    To copy to clipboard, switch view to plain text mode 

    should have been

    Qt Code:
    1. mxLabel = new QLabel( this );
    2. myLabel = new QLabel( this );
    To copy to clipboard, switch view to plain text mode 


    Since I made mxLabel and myLabel part of the class private data members. +_=. So you were right. Sorry about that.

    Anyway, how can I make the labels reset to 0 0 when the shot is completely off the screen?

  16. #11
    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

    Also, does anyone know how to do the following?

    "Re-paint the cannon to red when the shot is in the air. Then paint the cannon blue when the shot is gone."

    Thanks!

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. Basic Qt4 tutorial needed
    By pthomas in forum Qt Programming
    Replies: 26
    Last Post: 16th October 2006, 15:11
  3. Can't Compile Tutorial 7
    By Reenen in forum Newbie
    Replies: 10
    Last Post: 9th February 2006, 14:06
  4. Is There any Tutorial for non GUI Tcp Application
    By pilgermann in forum Qt Programming
    Replies: 1
    Last Post: 8th February 2006, 18:12
  5. Where are tutorial 2 code examples?
    By Mariane in forum Newbie
    Replies: 6
    Last Post: 25th January 2006, 15:02

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.