Qt更新调用(QRegion&;)未触发paintEvent(..)调用

Qt更新调用(QRegion&;)未触发paintEvent(..)调用,qt,Qt,我在玩Qt教程中的一些代码,我被卡住了,因为我的QWidget继承的类之一调用update(QRegion&),但它不会触发调用paintEvent(…)-本质上,我正在尝试对教程源代码进行简单的重构,以使用Qt教程第12章中的CannonShot类添加多个快照(请参见链接:) CannonField update()仍能正常工作,并导致调用其paintEvent重新实现。它唯一一个不调用自己的paintEvent的CannonShot的update()。(我已经确认它在更新调用之前和之后都会收

我在玩Qt教程中的一些代码,我被卡住了,因为我的QWidget继承的类之一调用update(QRegion&),但它不会触发调用paintEvent(…)-本质上,我正在尝试对教程源代码进行简单的重构,以使用Qt教程第12章中的CannonShot类添加多个快照(请参见链接:)

CannonField update()仍能正常工作,并导致调用其paintEvent重新实现。它唯一一个不调用自己的paintEvent的CannonShot的update()。(我已经确认它在更新调用之前和之后都会收到)。以下是一些片段:

class CannonShot : public QWidget
{
   Q_OBJECT
public:
   CannonShot(QWidget *parent = 0);
   int angle() const { return shotAngle; }
   int force() const { return shotForce; }
   void shoot();
private slots:
   void moveShot();
   void plotShot();

signals:
   void hit();
   void missed();
   void angleChanged(int);
   void forceChanged(int);
...
...
protected:
   paintEvent(QPaintEvent*);

private:
   int shotTimerCount;
   QTimer* autoShotTimer;
   float shotAngle;
   float shotForce;
};
下面是CannonShot构造函数的一个片段,其中调用了其update():

CannonShot::CannonShot(QWidget *parent): QWidget(parent)
{
   shotTimerCount = 0;
   autoShotTimer = new QTimer(this);
   connect(autoShotTimer, SIGNAL(timeout()), this, SLOT(moveShot()));
}

void CannonShot::shoot()
{
   if (autoShotTimer->isActive())
   return;
   shotTimerCount = 0;
   shotAngle = CannonField::angle();
   shotForce = CannonField::force();
   autoShotTimer->start(5);
}

void CannonShot::moveShot()
{
   QRegion region = shotRect();
   ++shotTimerCount;

   QRect shotR = shotRect();
   if (shotR.x() > width() || shotR.y() > height()) {
      autoShotTimer->stop();
      emit missed();
   }
   else {
      region = region.unite(shotR);
   }
   update(region); //-Checked that code reaches before and after this call!
}

void CannonShot::paintEvent(QPaintEvent*)
{
   //-Never gets here!!
   QPainter painter(this);
   QColor color;
   //-Do whatever
}
这里还有一个简短的CannonField片段,其中包含快照并调用shot()来启动它们

class CannonField : public QWidget
{
   Q_OBJECT

public:
   CannonField(QWidget *parent = 0);
   ~CannonField();
   static int angle() { return currentAngle; }
   static int force() { return currentForce; }

public slots:
   void setAngle(int angle);
   void setForce(int force);
   void shoot();

signals:
   void hit();
   void missed();
...
...

protected:
   void paintEvent(QPaintEvent *event);

public:
   static int currentAngle;
   static int currentForce;

private:
   QVector<CannonShot*> shots;
};
最后,驱动程序如下所示:

class MyWidget : public QWidget {
public:
   MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent): QWidget(parent)
{
   QPushButton *quit = new QPushButton(tr("&Quit"));
   quit->setFont(QFont("Times", 18, QFont::Bold));
   connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));
   LCDRange *angle = new LCDRange(tr("&ANGLE"));
   angle->setRange(0, 90);
   LCDRange *force = new LCDRange(tr("&FORCE"));
   force->setRange(10, 50);
   CannonField *cannonField = new CannonField(this);
   connect(angle, SIGNAL(valueChanged(int)),
          cannonField, SLOT(setAngle(int)));
   connect(cannonField, SIGNAL(angleChanged(int)),
          angle, SLOT(setValue(int)));
   connect(force, SIGNAL(valueChanged(int)),
          cannonField, SLOT(setForce(int)));
   connect(cannonField, SIGNAL(forceChanged(int)),
          force, SLOT(setValue(int)));
   QPushButton *shoot = new QPushButton(tr("&Shoot"));
   shoot->setFont(QFont("Times", 18, QFont::Bold));
   connect(shoot, SIGNAL(clicked()), cannonField, SLOT(shoot()));      
   ...
   ...
   //-Other layout stuff
}

int main(int argc, char *argv[])
{
   QApplication app(argc, argv);
   MyWidget widget;
   widget.setMinimumSize(300, 300);
   widget.setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
   widget.setGeometry(100, 100, 500, 355);
   widget.show();
   return app.exec();
}
如果QRegion::isEmpty()为true,它将不会调用QWidget::paintEvent()。我怀疑这可能发生在你身上

paintEvent(QPaintEvent*)
函数应位于
受保护的部分,函数签名为:

void QWidget::paintEvent ( QPaintEvent * event )   [virtual protected]
目前您有:

private:
paintEvent(QPaintEvent*);
应该是:

protected:
virtual void paintEvent(QPaintEvent*);

相反。

如果更新区域未与帧区域相交,则可能发生:

QRect frameRect = this->rect();

if( frameRect.intersected(updateRec).isEmpty())
{

}

@布拉巴里斯-说得好,我还没有检查过!行。谢谢。我已经确认我的区域不是空的。。通过放置if(region.isEmpty()){std::cout@ismail:我确实在受保护部分有它。谢谢。我不确定虚拟函数访问权的更改是否重要。只要签名相同,并且通过指向基类的指针进行访问,被重写的虚拟函数将具有与基类相同的访问权,无论它在派生类中声明了什么访问权。@ismail:让我澄清一下……我的帖子中有一个打字错误——对不起。在我的代码中,它在受保护的部分(我也编辑了帖子)。所以这不是问题所在。
QRect frameRect = this->rect();

if( frameRect.intersected(updateRec).isEmpty())
{

}