错误:静态断言失败:信号和槽参数不兼容
我在连接函数中遇到此错误,
QTimer timer;
QObject::connect(&timer, &QTimer::timeout,this, &MainWindow::draw);
timer.start(1000);
超时和绘制都是预定义函数。 我该如何解决这个问题? 即使我使用不同的功能,例如
Sprite *sprite = new Sprite(this);
QTimer timer;
QObject::connect(&timer, &QTimer::timeout,sprite, &Sprite::drawNextBall);
timer.start(1000);
void Sprite::drawNextBall(QPainter &painter)
{
qDebug() <<"Sprite::drawNextBall() called";
painter.drawEllipse(x, y, 15, 15);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如错误消息所述,信号和槽参数应该匹配。在您的示例中绝对不是这种情况,因为
QTimer::timeout
没有QPainter&
作为其第一个参数:其次,
QPainter
只能用于在 QWidget 上绘图href="https://doc.qt.io/qt-5/qwidget.html#paintEvent" rel="nofollow noreferrer">QWidget::paintEvent
:您可能想要的是连接
QTimer ::timeout
信号发送给QWidget::update
(MainWindow::update
或Sprite::update
)。相关阅读材料
As stated by the error message, signal and slot arguments should match. This is definitely not the case in your example as
QTimer::timeout
has not aQPainter&
as its first argument:Secondly,
QPainter
should only be used to draw on aQWidget
inside aQWidget::paintEvent
:What you probably want is connecting the
QTimer::timeout
signal to theQWidget::update
(MainWindow::update
orSprite::update
).Relevant reading material