2013-07-17 36 views
0

試圖顯示基於當QTimer觸發關上的文字...顯示不在QTimer上更新?

MainWindow::MainWindow(QWidget *parent) 
    : QMainWindow(parent) 
{ 
    m_label1 = new QLabel("My Label not working", this); 

    QTimer* timerDisplay = new QTimer(this); 
    connect(timerDisplay, SIGNAL(Started()), this, SLOT(updateDisplay(this))); 
    timerDisplay->start(10); 

} 


void updateDisplay(MainWindow* m_this) 
{ 
    QString out; 
    out = "hello"; 

    m_this->m_label1->setText("asdf"); 
} 
+0

除了peppe已經回答的問題外,'QTimer'沒有'Started()'信號,甚至沒有'started()'信號。 「QTimer」提供的唯一信號是'timeout()',以及'QObject'繼承的信號。 – Boris

回答

3
connect(timerDisplay, SIGNAL(Started()), this, SLOT(updateDisplay(this))); 

此語句失敗。而你忽略了Qt在控制檯上打印的信息。

問題是,你不能在connect這樣的語句中傳遞變量。順便說一句,什麼?您可以在updateDisplay方法中使用this,而無需明確傳遞它!

+0

我忘了兩件事:1)將聲明放在頭文件的槽中,2)它應該是無效的MainWindow :: updateDisplay()。 – jdl

+0

此外,值得添加的是,在這種情況下,[QObject :: sender()](http://qt-project.org/doc/qt-4.8/qobject.html#sender)是你如何得到的。好吧,發件人。只需在updateDisplay()槽中調用它,並在調試器中斷開,這將是一個令人愉快的驚喜。 – Huy