[Interest] QThreadPool & writing to the DB
Igor Mironchik
igor.mironchik at gmail.com
Mon Aug 18 08:26:27 CEST 2014
Hi.
I want to move write to DB operation to another thread (from the GUI thread). And I implemented the following QRunnable:
//// SourcesLogWritter//class SourcesLogWritter : public QRunnable{public: SourcesLogWritter() { setAutoDelete( false ); } void setData( const QDateTime & dateTime, const QString & channelName, Como::Source::Type type, const QString & sourceName, const QString & typeName, const QVariant & value, const QString & desc ) { m_dateTime = dateTime; m_channelName = channelName; m_type = type; m_sourceName = sourceName; m_typeName = typeName; m_value = value; m_desc = desc; } void run() { QSqlQuery insert( QLatin1String( "INSERT INTO sourcesLog ( dateTime, channelName, type, " "sourceName, typeName, value, desc ) " "VALUES ( ?, ?, ?, ?, ?, ?, ? )" ) ); insert.addBindValue( dateTimeToString( m_dateTime ) ); insert.addBindValue( m_channelName ); insert.addBindValue( (int) m_type ); insert.addBindValue( m_sourceName ); insert.addBindValue( m_typeName ); insert.addBindValue( m_value.toString() ); insert.addBindValue( m_desc ); insert.exec(); }private: QDateTime m_dateTime; QString m_channelName; Como::Source::Type m_type; QString m_sourceName; QString m_typeName; QVariant m_value; QString m_desc;}; // class SourcesLogWritter
Is it OK to launch this runnable every time when I need to write to the log?
I do the following:
d->m_sourcesLogWritter->setData( dateTime, channelName, type, sourceName, typeName, value, desc );QThreadPool::globalInstance()->start( d->m_sourcesLogWritter.data() );
And what if previous operation is still not finished and I will launch another?
Thanks.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140818/1b3cfe17/attachment.html>
More information about the Interest
mailing list