1
votes

I am using QT framework. I have been using SIGNAL-SLOT for a while. I like it. :-) But I cannot make it work when I use QThread. I always create new thread using “moveToThread(QThread …)” function. Any suggestion? :-)

The error message is:

Object::connect: No such slot connection::acceptNewConnection(QString,int) in ..\MultiMITU600\mainwindow.cpp:14 Object::connect: (sender name: 'MainWindow')

I have read about similar problems but those were not connected to QThread.

Thanks, David

EDITED: you asked for source code Here is one:

Here is the code:

The main class which contains the signal and the new thread:

mainwindow header:

class MainWindow : public QMainWindow
{

    …
    QThread cThread;              
    MyClass Connect;
    ...
    signals:

            void NewConnection(QString port,int current);
     …
};

The constructor of the above class: .cpp

{
    …
        Connect.moveToThread(&cThread1);
           cThread.start(); // start new thread
   ….
connect(this,SIGNAL(NewConnection(QString,int)),
            &Connect,SLOT(acceptNewConnection(QString,int))); //start measuring
…
}

The class that contains the new thread and SLOT Header:

class MyClass: public QObject
{
           Q_OBJECT
….
   public slots:
            void acceptNewConnection(QString port, int current);
}

And the .cpp file of the above class:

void MyClass::acceptNewConnection(QString port, int current){
    qDebug() << "This part is not be reached";

 }

Finally I use emit in the class where the connection was made:

void MainWindow::on_pushButton_3_clicked()
{
    …
emit NewConnection(port, 1); 
} 
1
Might be an idea to actually show the codepaulm
"I always create new thread using “moveToThread(QThread …)” function" does not make any sense, that is not how threads are created.hyde

1 Answers

3
votes
class MyClass : public QObject
{
    Q_OBJECT
public:
    explicit MyClass(QObject *parent = 0);

public slots:
    void acceptConnection(QString port, int current) {
        qDebug() << "received data for port " << port;
    }    
};

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    MainWindow(QWidget *parent = 0) : QMainWindow(parent) {
        myClass.moveToThread(&thread);
        thread.start();
        connect(this, SIGNAL(newConnection(QString,int)), &myClass, SLOT(acceptConnection(QString,int)));
        emit newConnection("test", 1234);
    }

signals:
    void newConnection(QString, int);

private:
    QThread thread;
    MyClass myClass;
};

output: received data for port "test"

Is your void MainWindow::on_pushButton_3_clicked() slot connected to a signal?

Also, for the sake of the clarity and readability of your code, keep the established naming convention and use lower case for object instances and member objects and methods.