Solved ERROR QObject::connect: No such slot MainWindow::Inventory(const QString&)
-
I was wrote simple code but I have this error :
ERROR: QObject::connect: No such slot MainWindow::Inventory(const QString&)thread class:
//.h: #ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QMutex> #include <serial.h> class MyThread : public QThread { Q_OBJECT public slots: void stopRunning(); protected: virtual void run(); signals: void signalValueUpdated(QString); private: bool isRunning; Serial sTest; }; #endif //.cpp: #include "MyThread.h" #include <QString> #include "Serial.h" #include <QDebug> #include <QImage> #include <QPixmap> void MyThread::run() { qDebug("Thread id inside run %d",(int)QThread::currentThreadId()); isRunning = 1; uint8_t Cmd[] ={0x05, 0x07, 0xFF, 0xF0, 0x01,0x00, 0x6A, 0x50}; sTest.Openport(); while(isRunning == 1) { if (sTest.OpenFlag){ QString string = sTest.RxTx(Cmd); msleep(1); emit signalValueUpdated(string); } } } void MyThread::stopRunning() { isRunning = 0; sTest.ClosePort(); }
mainwindow class:
//.h: #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include "MyThread.h" #include <QDebug> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); void Inventory(QString response); private: Ui::MainWindow *ui; MyThread thread; }; #endif // MAINWINDOW_H #ifndef MAINWINDOW_H #define MAINWINDOW_H //.cpp: #include "mainwindow.h" #include "ui_mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QObject::connect(ui->startBut,SIGNAL(clicked()),&thread,SLOT(start()), Qt::QueuedConnection); QObject::connect(ui->stopBut,SIGNAL(clicked()),&thread,SLOT(stopRunning()), Qt::QueuedConnection); QObject::connect(&thread,SIGNAL(signalValueUpdated(const QString&)),this,SLOT(Inventory(const QString&)), Qt::QueuedConnection); } MainWindow::~MainWindow() { delete ui; } void MainWindow::Inventory(QString response) { if(response=="c"){ // /// } }
-
Hi
You are using a mix of QString and const QString &
Make sure the signal and slot are 100% the same -
@mrjj Hi
I edited it and set all to QString but I still get this error when run the programin main window:
//.h: void Inventory(QString response); //.cpp: QObject::connect(&thread,SIGNAL(signalValueUpdated(QString)),this,SLOT(Inventory( QString)), Qt::QueuedConnection); void MainWindow::Inventory(QString response) { /// // }
in thread :
//.h: void signalValueUpdated(QString); //.cpp: QString string; emit signalValueUpdated(string);
-
@isan
Hi
Try to delete your build folder and then run rebuild all.
Seems ok now. -
@mrjj I did this, but it didn't make any difference
QObject::connect: No such slot MainWindow::Inventory( QString) in ..\Inv\mainwindow.cpp:12 QObject::connect: (receiver name: 'MainWindow')
-
In mainwindow.h try :
protected Slots:
void Inventory(QString response); -
@gerhard tnx