简体   繁体   English

Qt Object :: connect:没有这样的插槽信号到线程插槽

[英]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: 我尝试在威胁开始时在线程对象中调用Slot,但出现此错误:

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? 您不能将pfooStack传递给Worker构造函数吗?

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. 除了其他答案之外,您还可以使用QSignalMapper来实现,但是如果您只有一个与插槽的连接,那似乎是过大了。

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). 如果您可以使用Qt5和C ++ 11,则可以将信号连接到lambda函数,而不仅仅是插槽,但是我不确定是否支持创建闭包(也就是说,使用lambda函数中的局部变量,您将需要在这里)。

I think you need to change the signal and slot signatures. 我认为您需要更改信号和插槽签名。 From the QT Documenation: 从QT文档中:

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. 如果参数具有默认值,则是否在SIGNAL()和SLOT()宏中包含参数的规则是,传递给SIGNAL()宏的签名不得少于传递给SLOT的签名。 ()宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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