错误:静态断言失败:信号和槽参数不兼容

发布于 2025-01-17 01:34:18 字数 533 浏览 3 评论 0 原文

我在连接函数中遇到此错误,

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);
}

I am getting this error with connect function

QTimer timer;
QObject::connect(&timer, &QTimer::timeout,this, &MainWindow::draw);
timer.start(1000);

both timeout and draw are predefined functions.
How can I solve this?
Even If I use a different function like

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2025-01-24 01:34:18

正如错误消息所述,信号和槽参数应该匹配。在您的示例中绝对不是这种情况,因为 QTimer::timeout 没有 QPainter& 作为其第一个参数:

信号的签名必须与接收槽的签名匹配。 (事实上​​,槽的签名可能比它接收到的信号更短,因为它可以忽略额外的参数。) [1]

其次,QPainter 只能用于在 QWidget 上绘图href="https://doc.qt.io/qt-5/qwidget.html#paintEvent" rel="nofollow noreferrer">QWidget::paintEvent

警告:当paintdevice是一个widget时,QPainter只能在paintEvent()函数内部或由paintEvent()调用的函数中使用。 [2]

您可能想要的是连接QTimer ::timeout 信号发送给 QWidget::updateMainWindow::updateSprite::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 a QPainter& as its first argument:

The signature of a signal must match the signature of the receiving slot. (In fact a slot may have a shorter signature than the signal it receives because it can ignore extra arguments.) [1]

Secondly, QPainter should only be used to draw on a QWidget inside a QWidget::paintEvent:

Warning: When the paintdevice is a widget, QPainter can only be used inside a paintEvent() function or in a function called by paintEvent(). [2]

What you probably want is connecting the QTimer::timeout signal to the QWidget::update (MainWindow::update or Sprite::update).

Relevant reading material

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文