简体   繁体   中英

Qt Object::connect: No such slot Signal to Thread Slot

i try to invoke Slot in thread object when threas started but getting this error:

Object::connect: No such slot Worker::doWork(pFoo)

the thread executing code:

// main class   

            m_WorkerThread = new QThread();

             FooStack* pfooStack = InternalStorageManager::getInstance()->getStack();
             m_Worker = new Worker();
             bool done = connect(m_WorkerThread,
                                    SIGNAL(started()),
                                    m_Worker,
                                    SLOT(doWork(pfooStack))); 

             m_Worker->moveToThread(m_WorkerThread);          
             m_WorkerThread->start();



// class Worker
 // cpp imple            
void Worker::doWork(FooStack *& rp_urlsStack)
{

}
// header 
class Worker : public QObject
{

    Q_OBJECT

    public :

        Worker();
        ~Worker();
    public slots:
        void doWork(FooStack *&);

};
Object::connect: No such slot Worker::doWork(pFoo)

You can't pass objects in connection declarations.

Can't you pass pfooStack into the Worker constructor?

EDIT:

class Main : ...
{
    ...
    void startThread();  // The method in your example.
private slots:
     void startWork();
    ...
};

void Main::startThread()
{
     m_WorkerThread = new QThread();
     m_Worker = new Worker();
     bool done = connect(m_WorkerThread, SIGNAL(started()),
                         this, SLOT(startWork())); 

     m_Worker->moveToThread(m_WorkerThread);          
     m_WorkerThread->start();
}

void Main::startWork()
{
     m_Worker->doWork(InternalStorageManager::getInstance()->getStack());
}

I have not compiled the code on my computer, but it should imply what you need:

     m_WorkerThread = new QThread();

             FooStack* pfooStack = InternalStorageManager::getInstance()->getStack();
             m_Worker = new Worker(pfooStack);
             bool done = connect(m_WorkerThread,
                                    SIGNAL(started()),
                                    m_Worker,
                                    SLOT(doWork())); 

             m_Worker->moveToThread(m_WorkerThread);          
             m_WorkerThread->start();




void Worker::doWork()
{
      //use stack here
}

class Worker : public QObject
{

    Q_OBJECT

    public :

        Worker(FooStack *& rp_urlsStack):stack(rp_urlsStack);
        ~Worker();
    public slots:
        void doWork();
    private:
        FooStack*& stack;

};

You can't do it that way, you can't pass current variables as slot method parameters in connect, and slot can't have more parameters than the signal. In addition to other answers, you can achieve this with QSignalMapper , but if you have just one connection to the slot, that seems like an overkill.

If you can use Qt5 and C++11, then you can connect signal to lambda functions, not just slots, but I'm not absolutely sure if that supports creating a closure (that is, using the local variable in the lambda function, which you would need here).

I think you need to change the signal and slot signatures. From the QT Documenation:

The rule about whether to include arguments or not in the SIGNAL() and SLOT() macros, if the arguments have default values, is that the signature passed to the SIGNAL() macro must not have fewer arguments than the signature passed to the SLOT() macro.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM