1
我想創建一個qt應用程序,其中每10秒調用一次函數以更改qlineedit中的文本。我是qt編程的新手。請幫幫我。實時更改qt應用程序的qlineedit中的文本
我想創建一個qt應用程序,其中每10秒調用一次函數以更改qlineedit中的文本。我是qt編程的新手。請幫幫我。實時更改qt應用程序的qlineedit中的文本
請看QTimer班。 //或者告訴我們更多關於你不知道如何去做的事情。
您想要使用QTimer並將其連接到執行更新的插槽。
本課程將做到這一點(注意,我輸入這直接進入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());
}
您需要修改它做任何你需要的。
這很漂亮。我喜歡你如何將它附加到lineEdit以在銷燬時刪除它。我只補充說,如果你需要它保持超出範圍,那麼應該在堆上創建'TextUpdater'對象(即使用'new')。 – Phlucious