error: static assertion failed: Signal and slot arguments are not compatible
-
I am getting this error with connect function
QTimer timer; QObject::connect(&timer, &QTimer::timeout,this, &MainWindow::draw); timer.start(1000);t
both timeout and draw are predefined functions. How can I solve this? Even If I use a different function like 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); }
-
Hi, welcome to the forum.
As the error says - the signal and slot arguments need to match as the argument is passed from one to the other. In this case
QTimer::timeout
does not have any arguments andSprite::drawNextBall
takes a QPainter. There's no way Qt can automatically create a painter for you that you'd like. You have to do it yourself.So to solve it you need a function that doesn't take any parameters. Inside you can create a painter that you want and call
drawNextBall
with it. Alternatively you can replace the slot in that connect statement with a parameterless lambda and do the same inside it - create a painter and call the thing you want with it.Also, with painting you don't usually call draw functions directly, but rather schedule a paint event and let Qt call paintEvent at the right time with the right arguments. So something like
QObject::connect(&timer, &QTimer::timeout, this, &MainWindow::update);
update()
is a function that schedules a paint event, so you can overridepaintEvent
method and do your drawing there. -
Christian Ehrlicher Lifetime Qt Championreplied to Chris Kawa on last edited by Christian Ehrlicher
@Chris-Kawa said in error: static assertion failed: Signal and slot arguments are not compatible:
QObject::connect(&timer, &QTimer::timeout, this, &MainWindow::update);
Nearly, sadly QWidget::update() has some overloads so
QObject::connect(&timer, &QTimer::timeout, this, qOverload<>(&MainWindow::update));
-
@Christian-Ehrlicher said in error: static assertion failed: Signal and slot arguments are not compatible:
QObject::connect(&timer, &QTimer::timeout, this, qOverload:<>:of(&MainWindow::update));
I get an error if I use the above connect function
mainwindow.cpp:15:54: error: use of undeclared identifier 'qOverload'
-
Then your compiler is to old - use QOverload
-
@Chris-Kawa
I have already overridden the paintEvent() in the Mainwindow class.
Please refer to this post and provide some help
https://forum.qt.io/topic/135315/ball-creation-at-regular-intervals-falling-ball-game/2