Народ, столкнулся с проблемой не проходит сигнал/слот в след конструкции:
classes.hpp
#ifndef CLASSES_HPP
#define CLASSES_HPP
#include <QObject>
#include <QTextStream>
#include <QThread>
#include <iostream>
/// controller
class Controller: public QObject {
Q_OBJECT
public:
Controller(QObject *parent = Q_NULLPTR ): QObject(parent),m_enable(false){
std::cout << "Controller" << std::endl;
}
~Controller(){
std::cout << "~Controller" << std::endl;
}
public slots:
void put( const QString & msg ){
QString str( msg );
std::cout << str.toStdString() << std::endl;
}
};
/// Writer
class Writer : public QObject {
Q_OBJECT
signals:
void send( const QString & msg );
public:
Writer( QObject * parent = 0 ): QObject(parent){
std::cout << "Writer" << std::endl;
}
~Writer(){
std::cout << "~Writer" << std::endl;
}
public slots:
void write(){
QThread * pthread = QThread::currentThread();
for ( int i=0; i<1000; i++ ){
QString str;
QTextStream( &str ) << "write: " << i;
emit send( str );
}
pthread->quit();
}
};
#endif // CLASSES_HPP
main.cpp
#include <QCoreApplication>
#include "classes.hpp"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Controller ctrl( &a );
ctrl.put( QString("step1") );
ctrl.put( QString("step2") );
QThread * pThread = new QThread( &a );
Writer * pWriter = new Writer( 0 );
pWriter->moveToThread( pThread );
QObject::connect(pThread, &QThread::finished, pWriter, &QObject::deleteLater );
QObject::connect(pThread, &QThread::finished, pThread, &QObject::deleteLater );
QObject::connect(pWriter, &Writer::send, &ctrl, &Controller::put, Qt::BlockingQueuedConnection/*Qt::QueuedConnection*/ );
QObject::connect(pThread, &QThread::started, pWriter, &Writer::write );
pThread->start();
std::cout << "started" << std::endl;
pThread->wait();
std::cout << "waited" << std::endl;
delete pThread;
return 0;
}
Если указан Qt::BlockingQueuedConnection, то программа лочится на первом emit send во врайтере, если просто Qt::QueuedConnection, то
цикл пролетает и слот контроллера не вызывается. Где собака порылась?
Спасибо