QSharedPointer<fugio::PinControlInterface> ContextPrivate::createPinControl( const QUuid &pUUID, QSharedPointer<fugio::PinInterface> pPin ) { const QMetaObject *SMO = mApp->findPinMetaObject( pUUID ); if( !SMO ) { qWarning() << "Unknown fugio::PinControlInterface" << pUUID << pPin->name(); return( QSharedPointer<fugio::PinControlInterface>() ); } QObject *ClassInstance = SMO->newInstance( Q_ARG( QSharedPointer<fugio::PinInterface>, pPin ) ); if( ClassInstance ) { ClassInstance->moveToThread( thread() ); fugio::PinControlInterface *PinControl = qobject_cast<fugio::PinControlInterface *>( ClassInstance ); if( PinControl ) { return( QSharedPointer<fugio::PinControlInterface>( PinControl ) ); } delete ClassInstance; } else { qWarning() << "Can't create Pin ClassInstance of" << mApp->findPinMetaObject( pUUID )->className(); } return( QSharedPointer<fugio::PinControlInterface>() ); }
QNetworkConfigurationManagerPrivate *qNetworkConfigurationManagerPrivate() { QNetworkConfigurationManagerPrivate *ptr = connManager_ptr.loadAcquire(); int shutdown = appShutdown.loadAcquire(); if (!ptr && !shutdown) { static QBasicMutex connManager_mutex; QMutexLocker locker(&connManager_mutex); if (!(ptr = connManager_ptr.loadAcquire())) { ptr = new QNetworkConfigurationManagerPrivate; if (QCoreApplicationPrivate::mainThread() == QThread::currentThread()) { // right thread or no main thread yet ptr->addPreAndPostRoutine(); ptr->initialize(); } else { // wrong thread, we need to make the main thread do this QObject *obj = new QObject; QObject::connect(obj, SIGNAL(destroyed()), ptr, SLOT(addPreAndPostRoutine()), Qt::DirectConnection); ptr->initialize(); // this moves us to the right thread obj->moveToThread(QCoreApplicationPrivate::mainThread()); obj->deleteLater(); } connManager_ptr.storeRelease(ptr); } } return ptr; }
bool checkFileSum(const QString &filePath, const QString &checksum) { bool res = false; WaitDialog wd; QObject *worker = new QObject; worker->moveToThread(new QThread); wd.connect(worker->thread(), &QThread::started, worker, [worker, &checksum, &res, &filePath]() { if (QFile::exists(filePath)) { QFile f(filePath); f.open(QFile::ReadOnly); QByteArray data = f.readAll(); f.close(); QString fSum = QCryptographicHash::hash(data, QCryptographicHash::Sha1).toHex(); res = fSum == checksum; } else res = false; worker->deleteLater(); }); wd.connect(worker, &QObject::destroyed, worker->thread(), &QThread::quit); wd.connect(worker->thread(), &QThread::finished, worker->thread(), &QThread::deleteLater); wd.connect(worker->thread(), &QThread::destroyed, &wd, &WaitDialog::accept); worker->thread()->start(); wd.exec(); return res; }
int main(int argc, char ** argv) { QCoreApplication app{argc, argv}; QObject context; QThread thread; context.moveToThread(&thread); thread.start(); onTimeout(1000, []{ qDebug() << "T+1s"; }); onTimeout(2000, &context, [&]{ qDebug() << "T+2s"; thread.quit(); }); QObject::connect(&thread, &QThread::finished, &app, &QCoreApplication::quit); return app.exec(); }
void test1() { QThread t; QObject o; o.moveToThread(&t); // Execute in given object's thread postToThread([&]{ o.setObjectName("hello"); }, &o); // or postToThread(std::bind(&QObject::setObjectName, &o, "hello"), &o); // Execute in the main thread postToThread([]{ qDebug() << "hello"; }); }
void removePrefix(const QString &prefixHash, QWidget *parent) { WaitDialog wd(parent); QObject *worker = new QObject; worker->moveToThread(new QThread); wd.connect(worker->thread(), &QThread::started, worker, [worker, prefixHash]() { prefix(prefixHash).removeRecursively(); worker->deleteLater(); }); wd.connect(worker, &QObject::destroyed, worker->thread(), &QThread::quit); wd.connect(worker->thread(), &QThread::finished, worker->thread(), &QThread::deleteLater); wd.connect(worker->thread(), &QThread::destroyed, &wd, &WaitDialog::accept); worker->thread()->start(); wd.exec(); }
QSharedPointer<fugio::NodeInterface> ContextPrivate::createNode( const QString &pName, const QUuid &pGlobalId, const QUuid &pControlId, const QVariantHash &pSettings ) { NodePrivate *NODE = new NodePrivate(); if( !NODE ) { return( QSharedPointer<fugio::NodeInterface>() ); } NODE->moveToThread( thread() ); NODE->setName( pName ); NODE->setUuid( pGlobalId ); NODE->setControlUuid( pControlId ); NODE->setSettings( pSettings ); NODE->setContext( this ); QSharedPointer<fugio::NodeInterface> NODE_PTR = QSharedPointer<fugio::NodeInterface>( NODE ); fugio::ClassEntry CE = mApp->findNodeClassEntry( pControlId ); if( CE.mMetaObject ) { QObject *ClassInstance = CE.mMetaObject->newInstance( Q_ARG( QSharedPointer<fugio::NodeInterface>, NODE_PTR ) ); if( ClassInstance ) { ClassInstance->moveToThread( thread() ); fugio::NodeControlInterface *NodeControl = qobject_cast<fugio::NodeControlInterface *>( ClassInstance ); if( NodeControl ) { NODE->setControl( QSharedPointer<fugio::NodeControlInterface>( NodeControl ) ); } } } else { qWarning() << "Unknown NodeControlInterface" << pControlId; } return( NODE_PTR ); }
QObject *QFactoryLoader::instance(int index) const { Q_D(const QFactoryLoader); if (index < 0) return 0; if (index < d->libraryList.size()) { QLibraryPrivate *library = d->libraryList.at(index); if (library->instance || library->loadPlugin()) { if (!library->inst) library->inst = library->instance(); QObject *obj = library->inst.data(); if (obj) { if (!obj->parent()) obj->moveToThread(QCoreApplicationPrivate::mainThread()); return obj; } } return 0; } index -= d->libraryList.size(); QVector<QStaticPlugin> staticPlugins = QLibraryPrivate::staticPlugins(); for (int i = 0; i < staticPlugins.count(); ++i) { const char *rawMetaData = staticPlugins.at(i).metaData(); QJsonObject object = QLibraryPrivate::fromRawMetaData(rawMetaData).object(); if (object.value(QLatin1String("IID")) != QLatin1String(d->iid.constData(), d->iid.size())) continue; if (index == 0) return staticPlugins.at(i).instance(); --index; } return 0; }