#include "database.h" #include #include #include Database::Database(QObject *parent) : QObject{parent} , m_pool{} { m_pool.setMaxThreadCount(1); m_pool.setExpiryTimeout(-1); } QFuture Database::open(const QString &user, const QString &password) { return QtConcurrent::run(&m_pool, [this, user, password]() { QString connectionName("main"); QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL", connectionName); db.setConnectOptions("service=camper; options=-csearch_path=camper,public"); if (!db.open(user, password)) { const QString errorMessage(db.lastError().text()); db = QSqlDatabase(); // Otherwise removeDatabase complains is still being used. QSqlDatabase::removeDatabase(connectionName); emit errorOcurred(errorMessage); } }); } #include "moc_database.cpp"