有兩種方法:
查詢最後一次更改後100毫秒。爲此,你(重新)在按鍵上啓動一個100ms的定時器。當它到期時,您運行查詢。
查詢每 100毫秒,而用戶正在進行更改。按下按鍵可以開始100ms的時間,但只有當它還沒有運行時。當它到期時,您運行查詢。
如果要實現在一個單獨的QObject查詢處理器,然後你可以平凡它移到一個單獨的線程 - 但要確保你已經創建了一個線程的數據庫連接了。 UI對象和查詢處理器之間唯一的通信方式應該是通過信號/插槽 - 這樣就可以處理線程之間的同步數據交換。
您可以刪除moveToThread
調用:它仍然有效。然後,在GUI線程中運行查詢執行程序,因此數據庫驅動程序通常在查詢處理期間阻塞時,用戶體驗可能會更糟。
#include <QApplication>
#include <QTextEdit>
#include <QBasicTimer>
#include <QSqlDatabase>
#include <QThread>
class ProductData {
};
Q_DECLARE_METATYPE(ProductData)
class PoS : public QWidget {
Q_OBJECT
enum QueryBehavior { FinalQuery, MultipleQuery };
QBasicTimer m_queryTimer;
QueryBehavior m_queryBehavior;
Q_SLOT void on_lineEdit_textEdited() {
if (m_queryBehavior == FinalQuery || !m_queryTimer.isActive())
m_queryTimer.start(100, this);
}
void timerEvent(QTimerEvent * ev) {
if (ev->timerId() != m_queryTimer.timerId()) return;
m_queryTimer.stop();
emit queryRequest();
}
public:
Q_SIGNAL void queryRequest();
Q_SLOT void queryResponse(const ProductData &) { /* ... */ }
// ...
};
class QueryExecutor : public QObject {
Q_OBJECT
QSqlDatabase m_dbConnection;
public:
Q_SLOT void queryRequest() {
if (!m_dbConnection.isOpen()) {
// Open the database connection here, NOT in the constructor.
// The constructor executes in the wrong thread.
// ...
}
ProductData pdata;
// ...
emit queryResponse(pdata);
}
Q_SIGNAL void queryResponse(const ProductData &);
};
//! A thread that's always safe to destruct.
class Thread : public QThread {
private:
using QThread::run; // This is a final class.
public:
Thread(QObject * parent = 0) : QThread(parent) {}
~Thread() { quit(); wait(); }
};
int main(int argc, char ** argv) {
QApplication app(argc, argv);
qRegisterMetaType<ProductData>();
PoS pos;
QueryExecutor executor;
Thread thread; // must come after the executor!
thread.start();
executor.moveToThread(&thread);
executor.connect(&pos, SIGNAL(queryRequest()), SLOT(queryRequest()));
pos.connect(&executor, SIGNAL(queryResponse(ProductData)), SLOT(queryResponse(ProductData)));
pos.show();
return app.exec();
}
#include "main.moc"
工程就像一個魅力!這正是我需要的。謝謝 – user3338859
簡單而優雅!我將在Qt維基上的相關頁面上添加(具有適當的歸屬),以增加其可見性:https://wiki.qt.io/Delay_action_to_wait_for_user_interaction – waldyrious