从 QFutureWatcher 中获取数据
Getting data out QFutureWatcher
我正在尝试将我的 Qt 应用程序连接到数据库。由于我有一个 GUI,当然唯一的方法是在一个单独的线程中。我发现我可以通过 QtConcurrent::run
做到这一点。这是我的代码:
MainWindow::MainWindow(QWidget *parent) {
// ...
QFuture<bool> worker = QtConcurrent::run(this, &MainWindow::connectDatabase);
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>;
connect(watcher, &QFutureWatcher<bool>::finished, this, &MainWindow::databaseConnected);
watcher->setFuture(worker);
}
bool MainWindow::connectDatabase() {
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("127.0.0.1");
db.setUserName("user");
db.setPassword("pass");
db.setDatabaseName("mydb");
return db.open();
}
它有效,但我无法(显然)从该过程中获取任何数据。比如我想知道连接是否成功,如果能通过插槽就比较理想了。
我可以将 watcher
添加为 class 的成员并从槽中查询它,但我相信这种方法对于许多异步任务来说会很乏味。
我该怎么办?
您必须使 worker
成为 class 成员(因为局部变量将在退出构造函数时被删除):
MainWindow::MainWindow(QWidget *parent) {
// ...
m_worker = QtConcurrent::run(this, &MainWindow::connectDatabase);
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>;
connect(watcher, &QFutureWatcher<bool>::finished, this, &MainWindow::databaseConnected);
watcher->setFuture(m_worker);
}
方式二:
MainWindow::MainWindow(QWidget *parent) {
connect(this, &MainWindow::mySignalAboutDBOpen,
this, &MainWindow::databaseConnected, Qt::QueuedConnection);
QtConcurrent::run(this, &MainWindow::connectDatabase);
}
//and in the connectDatabase:
bool MainWindow::connectDatabase() {
//...
bool ret = db.open();
emit mySignalAboutDBOpen(ret);
return ret;
}
请注意 QSqlDatabase db
变量也是本地变量,将在 connectDatabase()
退出时删除。
我正在尝试将我的 Qt 应用程序连接到数据库。由于我有一个 GUI,当然唯一的方法是在一个单独的线程中。我发现我可以通过 QtConcurrent::run
做到这一点。这是我的代码:
MainWindow::MainWindow(QWidget *parent) {
// ...
QFuture<bool> worker = QtConcurrent::run(this, &MainWindow::connectDatabase);
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>;
connect(watcher, &QFutureWatcher<bool>::finished, this, &MainWindow::databaseConnected);
watcher->setFuture(worker);
}
bool MainWindow::connectDatabase() {
QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setHostName("127.0.0.1");
db.setUserName("user");
db.setPassword("pass");
db.setDatabaseName("mydb");
return db.open();
}
它有效,但我无法(显然)从该过程中获取任何数据。比如我想知道连接是否成功,如果能通过插槽就比较理想了。
我可以将 watcher
添加为 class 的成员并从槽中查询它,但我相信这种方法对于许多异步任务来说会很乏味。
我该怎么办?
您必须使 worker
成为 class 成员(因为局部变量将在退出构造函数时被删除):
MainWindow::MainWindow(QWidget *parent) {
// ...
m_worker = QtConcurrent::run(this, &MainWindow::connectDatabase);
QFutureWatcher<bool> *watcher = new QFutureWatcher<bool>;
connect(watcher, &QFutureWatcher<bool>::finished, this, &MainWindow::databaseConnected);
watcher->setFuture(m_worker);
}
方式二:
MainWindow::MainWindow(QWidget *parent) {
connect(this, &MainWindow::mySignalAboutDBOpen,
this, &MainWindow::databaseConnected, Qt::QueuedConnection);
QtConcurrent::run(this, &MainWindow::connectDatabase);
}
//and in the connectDatabase:
bool MainWindow::connectDatabase() {
//...
bool ret = db.open();
emit mySignalAboutDBOpen(ret);
return ret;
}
请注意 QSqlDatabase db
变量也是本地变量,将在 connectDatabase()
退出时删除。