2013-01-31 30 views

回答

1

請看QTimer班。 //或者告訴我們更多關於你不知道如何去做的事情。

2

您想要使用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()); 
} 

您需要修改它做任何你需要的。

+0

這很漂亮。我喜歡你如何將它附加到lineEdit以在銷燬時刪除它。我只補充說,如果你需要它保持超出範圍,那麼應該在堆上創建'TextUpdater'對象(即使用'new')。 – Phlucious

相關問題