Qt C++: static assertion failed: Signal and slot arguments are not compatible

Viewed 11779

I am trying to start a countdown timer in a workerthread when the user clicks a pushbutton. The value at which the timer starts the count down depends on the selected radius button from my GUI.

I use a signalmapper to pass the starting value from the timer as a parameter. However I get this error when trying to compile my code:

 static assertion failed: Signal and slot arguments are not compatible

My code:

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{

    connect(ui->btnTakeSnap, SIGNAL(clicked()), this, SLOT(startTimerWorker()) );

}

void MainWindow::startTimerWorker()
{
    myThread = new QThread(this);
    workerObj = new workerTimer();

    QSignalMapper* signalMapper = new QSignalMapper (this) ;

    connect(myThread, &QThread::started, signalMapper, SLOT(map()) );
    if(ui->rdBtntimer1s->isChecked())
    {signalMapper -> setMapping (myThread, 1000) ; }

    if(ui->rdBtntimer3s->isChecked())
    {signalMapper -> setMapping (myThread, 3000) ;  }

    if(ui->rdBtntimer5s->isChecked())
    {signalMapper -> setMapping (myThread, 5000) ;}

    connect(signalMapper, SIGNAL(mapped(int)), workerObj, SLOT(&workerTimer::countDown(int)));

    workerObj->moveToThread(myThread);
    myThread->start();

}


class workerTimer : public QObject
{
    Q_OBJECT
public:
    explicit workerTimer(QObject *parent = nullptr);

signals:

public slots:
    void countDown(int selectedTimer);
};

Could someone explain me what I may be doing incorrectly?

Thanks

2 Answers

Beside IIBeldus's answer, one more reason for this error to happen is that you use the new syntax of QT's connect() function that is :

connect(
    sender, &Sender::valueChanged,
    receiver, &Receiver::updateValue
);

and the signal (valueChanged) arguments was different from the slot (updateValue) arguments, this will make the compiler to try implicit conversion of the signal and slot arguments and you have one of 2 possibilities:
1. The compiler will try to make auto conversion (implicit conversion) and the conversion succeeded.
2. The compiler will try to make auto conversion (implicit conversion) and the conversion did not succeed.
From QT documentation:

Possibility to automatically cast the types if there is implicit conversion (e.g. from QString to QVariant)

I posted this answer because i faced the same problem and the accepted answer did not solve my problem, so try this answer if the accepted answer did not help you

Related