如何阻止循环直到sqlite查询执行?

时间:2014-01-19 12:40:36

标签: c++ qt sqlite qt4

我在Qt编写程序,下面是我的代码的一部分:

for(int row=0; row < 15; row++)
    {
        for(int column=0; column < 12; column++ )
        {
            if(query->exec(db->getInsertQuery(row,column)))
            {

            }else
                qDebug() << "Failed to insert cell";
        }
    }

当我运行这个程序时,它会进入&#34;没有响应&#34;州。当我跑步时

    query->exec(db->getInsertQuery(0,0));
    query->exec(db->getInsertQuery(0,1));
    ....

代替for循环程序正常运行,但我不能在程序中写出这么多行。你能建议我阻止循环直到查询执行的方法吗?如何在某个框中显示进度条直到循环完成?

2 个答案:

答案 0 :(得分:1)

如果您使用的是QtSql,我认为您应该使用execBatch()方法进行此类任务

Example

QSqlQuery q;
q.prepare("insert into myTable values (?, ?)");

QVariantList ints;
ints << 1 << 2 << 3 << 4;
q.addBindValue(ints);

QVariantList names;
names << "Harald" << "Boris" << "Trond" << QVariant(QVariant::String);
q.addBindValue(names);

if (!q.execBatch())
    qDebug() << q.lastError();

答案 1 :(得分:0)

您应该循环执行查询并使用QProgressDialog和qApp-&gt; processEvents()调用的组合。

基本上,您可以执行以下操作:

// Run queries with numbers 0-99
for ( int i = 0; i < 100; i++ )
{
     // run your query

     // update the progress dialog value

     // Run the Qt event loop to handle the GUI updates and avoid the "not responsing" app state
     qApp->processEvents( QEventLoop::ExcludeUserInputEvents );
}

您可以查看我的视频生成器类:https://sourceforge.net/p/karlyriceditor/code/HEAD/tree/src/videogenerator.cpp - 检查generate()函数,它与您需要的非常相似,生成视频文件并在执行此操作时更新进度对话框。 / p>