class RunThread : public QThread{ Q_OBJECTpublic: explicit RunThread( QObject *parent = 0 ); void run();private slots: void execute();private: QTimer timer;};RunThread::RunThread(QObject *parent) : QThread(parent){ //moveToThread( this ); не влияет}void RunThread::run(){ timer.singleShot( 10, this, SLOT(execute()) ); qDebug() << "starting loop"; exec();}extern "C" int c_library();void RunThread::execute(){ qDebug() << "starting processor"; c_library(); quit();}class plugin2 : public QObject, public plugininterface{ Q_OBJECT Q_INTERFACES( plugininterface )public: virtual ~plugin2(){} virtual void startPlugin(); void lib_message( QString );signals: void sigRelayMessage( QString );public slots: run_c_library(); // сигнал на запуск попадает сюда void threadFinished();private: RunThread* runner;}static plugin2* p;void plugin2::startPlugin(){ runner = new RunThread(); // без родителя connect( runner, SIGNAL(finished()), SLOT(threadFinished()) ); p = this;}extern "C" void lib_message( char* msg ) // этот хук вызывает C-библиотека{ p->lib_message( QObject::tr(msg) );}void plugin2::lib_message( QString msg ){ emit sigRelayMessage( msg );}void plugin2::run_c_library(){ runner->start();}
void RunThread::execute(){ qDebug() << "starting processor"; qDebug() << currentThread(); c_library(); quit();}
void plugin2::run_c_library(){ runner->start(); qDebug() << QThread::currentThread();}
void plugin2::lib_message( QString msg ){ _a->processEvents(); emit sigRelayMessage( msg );}
C++ (Qt)void Widget :: addLog( ... ){ MessageRecord record; record.text = text; ... newRecords.append( record );}
void Widget :: updateLog(){ if ( !newRecords.isEmpty() ) { ui->log->setUpdatesEnabled( false ); while ( !newRecords.isEmpty() ) { MessageRecord message = newRecords.takeLast(); MyItem* item = new MyItem(); item->setText( 0, ... ); ui->log->insert( 0, item ); } ui->log->setUpdatesEnabled( true ); } QCoreApplication::processEvents(); QTimer::singleShot( 1, this, SLOT( updateLog() ) );}
class RunThread : public QThread{ Q_OBJECTpublic: explicit RunThread( QObject *parent = 0 ); void run();};RunThread::RunThread(QObject *parent) : QThread(parent){}void RunThread::run(){ c_library();}