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
topLayout->addWidget(shoot);
topLayout->addStretch(1);
topLayout->addWidget( mxLabel );
topLayout->addWidget( myLabel );
connect( cannonField, SIGNAL( updateBulletPosition( int, int ) ), this, SLOT( setBulletPosition( int, int ) ) );
QHBoxLayout* topLayout = new QHBoxLayout;
topLayout->addWidget(shoot);
topLayout->addStretch(1);
QLabel* mxLabel = new QLabel( this );
QLabel* myLabel = new QLabel( this );
topLayout->addWidget( mxLabel );
topLayout->addWidget( myLabel );
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
{
public:
public slots:
void setBulletPosition( int x, int y );
private:
};
class MyWidget : public QWidget
{
public:
MyWidget( QWidget* parent = 0 );
public slots:
void setBulletPosition( int x, int y );
private:
QLabel* mxLabel;
QLabel* myLabel;
};
To copy to clipboard, switch view to plain text mode
From MyWidget::setBulletPosition( int x, int y )
void MyWidget::setBulletPosition( int x, int y )
{
mxLabel
->setText
( QVariant(x
).
toString() );
myLabel
->setText
( QVariant(y
).
toString() );
}
void MyWidget::setBulletPosition( int x, int y )
{
mxLabel->setText( QVariant(x).toString() );
myLabel->setText( QVariant(y).toString() );
}
To copy to clipboard, switch view to plain text mode
From CannonField::moveShot()
void CannonField::moveShot()
{
//
++timerCount;
// Get the new shot
QRect shotR
= shotRect
();
if( shotR.x() > width() || shotR.y() > height() )
{
autoShootTimer->stop();
}
else
{
region = region.unite(shotR);
}
// Repaint the QRegion
update(region);
emit updateBulletPosition( shotRect().topLeft().x(), shotRect().topLeft().y() );
}
void CannonField::moveShot()
{
//
QRegion region = shotRect();
++timerCount;
// Get the new shot
QRect shotR = shotRect();
if( shotR.x() > width() || shotR.y() > height() )
{
autoShootTimer->stop();
}
else
{
region = region.unite(shotR);
}
// Repaint the QRegion
update(region);
emit updateBulletPosition( shotRect().topLeft().x(), shotRect().topLeft().y() );
}
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.
Bookmarks