我正在开发一个销售点应用程序,我有一个函数可以从 QLineEdit(产品的条形码)中获取文本并运行查询以查找要显示的产品。问题是每次文本更改时我都在运行查询,也就是说,每次键入新字符时。有没有办法等待用户停止输入然后运行查询?我将使用手持式扫描仪,因此输入的每个字符之间的间隔大约为 100 毫秒。
我想我需要这样的东西:
void PoS::on_lineEdit_textEdited()
{
//check for keys still being pressed
//if 100ms have passed without any key press, run query
}
我曾尝试使用计时器和线程(我对 Qt 5 还很陌生)但到目前为止都失败了。
最佳答案
你需要一个计时器。在此处阅读有关 QTimer
的更多信息:http://qt-project.org/doc/qt-5.1/qtcore/qtimer.html
添加一个QTimer *mTimer
作为私有(private)成员变量。创建一个名为 do_query
的插槽,您将在其中进行查询。把这个放在构造函数的某个地方:
mTimer = new QTimer(this); // make a QTimer
mTimer->setSingleShot(true); // it will fire once after it was started
connect(mTimer, &QTimer::timeout, this, &PoS::do_query); // connect it to your slot
现在在你的函数中:
void PoS::on_lineEdit_textEdited()
{
mTimer->start(100); // This will fire do_query after 100msec.
// If user would enter something before it fires, the timer restarts
}
然后做你的查询:
void PoS::do_query()
{
// your code goes here
}
关于c++ - 等待 QLineEdit::onTextChanged 中输入的最后一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21945044/