[英]Changing text in qlineedit of qt application in real time
I want to create a qt application in which a function is called every 10 seconds to change the text in a qlineedit. 我想创建一个qt应用程序,其中每隔10秒调用一个函数来更改qlineedit中的文本。 I'm a newbie to qt programming.
我是qt编程的新手。 Please help me.
请帮我。
You want to use QTimer and connect it to a slot that does the update. 您想使用QTimer并将其连接到执行更新的插槽。
This class will do it (note, I typed this directly into StackOverflow, so there are probably compilation errors): 这个类会这样做(注意,我直接将它键入StackOverflow,因此可能存在编译错误):
class TextUpdater : public QObject {
public:
TextUpdater(QLineEdit* lineEdit);
public slots:
void updateText();
};
TextUpdater::TextUpdater(QLineEdit* edit)
:QObject(lineEdit), lineEdit(edit)
// make the line edit the parent so we'll get destroyed
// when the line edit is destroyed
{
QTimer* timer = new QTimer(this);
timer->setSingleShot(false);
timer->setInterval(10 * 1000); // 10 seconds
connect(timer, SIGNAL(timeout()), this, SLOT(updateText()));
}
void TextUpdater::updateText()
{
// Set the text to whatever you want. This is just to show it updating
lineEdit->setText(QTime::currentTime().toString());
}
You will need to modify it to do whatever you need. 您需要修改它以执行您需要的任何操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.