从'QCell*'类型的static_cast无效到'QWidget*'类型

Rob*_*ert 0 qt static-cast

第4行出现此错误:

void QPiece::setPosition( QPoint value )
{
    _position = value;
    QWidget* parentWidget = static_cast<QWidget *>( _board->Cells[value.x() ][ value.y() ]);
if (parentWidget->layout()) {
    parentWidget->layout()->addWidget( this ); }
else { 
     QHBoxLayout *layout = new QHBoxLayout( parentWidget );
     layout->setMargin(0); 
     layout->addWidget(this); 
     parentWidget->setLayout(layout);
}
    this->setParent( _board->Cells[ value.x() ][ value.y() ] );
}
Run Code Online (Sandbox Code Playgroud)

这是函数Cells()的定义:

class QBoard : public QWidget
{
     Q_OBJECT
     public:
          QCell *Cells[8][8];
          QBoard(QWidget *parent = 0);
          void drawCells();

     private:
          void positionCells();
};
Run Code Online (Sandbox Code Playgroud)

我想我做错了什么,但是什么?提前致谢. 这是QCell的类型,我认为QWidget是QLabel的父级

class QCell:public QLabel

{
     Q_OBJECT

public:
     QCell( QPoint position, QWidget *parent = 0 );

private:
     QGame *_game;
     QPoint _position;
protected:
      void mousePressEvent( QMouseEvent *ev );
 };
Run Code Online (Sandbox Code Playgroud)

Mat*_*Mat 5

这应该在没有强制转换的情况下工作,从派生到基础的转换是隐含的.

导致此错误的一个可能原因是您QCell在该编译单元中只有一个可见的前向声明,这将触发此错误.您需要让编译器可以看到完整的类声明,以了解该转换是否合法.

例:

#include <QWidget>

class QCell;

int main(int argc, char **argv)
{
    QCell *w = 0;
    QWidget *q = static_cast<QWidget*>(w);
}
Run Code Online (Sandbox Code Playgroud)
main.cpp: In function ‘int main(int, char**)’:
main.cpp:8:41: error: invalid static_cast from type ‘QCell*’ to type ‘QWidget*’
Run Code Online (Sandbox Code Playgroud)