Я изучаю C ++ и использую QT.
У меня есть небольшая программа, в которой я пытаюсь обновлять текст PushButton каждую секунду. Метка является текущим временем. У меня есть таймер, который должен тайм-аут каждую секунду, но кажется, что он никогда не делает. вот код.
Заголовочный файл
#ifndef _HELLOFORM_H
#define _HELLOFORM_H
#include "ui_HelloForm.h"
class HelloForm : public QDialog {
public:
HelloForm();
virtual ~HelloForm();
public slots:
void textChanged(const QString& text);
void updateCaption();
private:
Ui::HelloForm widget;
};
#endif /* _HELLOFORM_H */
Файл CPP
#include "HelloForm.h"
#include <QTimer>
#include <QtGui/QPushButton>
#include <QTime>
HelloForm::HelloForm(){
widget.setupUi(this);
widget.pushButton->setText(QTime::currentTime().toString());
widget.pushButton->setFont(QFont( "Times", 9, QFont::Bold ) );
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(updateCaption()));
timer->start(1000);
connect(widget.pushButton, SIGNAL(clicked()), qApp, SLOT(quit()) );
connect(widget.nameEdit, SIGNAL(textChanged(const QString&)), this, SLOT(textChanged(const QString&)));
}
HelloForm::~HelloForm() {
}
void HelloForm::textChanged(const QString& text) {
if (0 < text.trimmed().length()) {
widget.helloEdit->setText("Hello " + text.trimmed() + "!");
} else {
widget.helloEdit->clear();
}
}
void HelloForm::updateCaption() {
QString myVar;
myVar = QTime::currentTime().toString();
widget.pushButton->setText(myVar);
}
Любая помощь будет принята с благодарностью ... Текст кнопки никогда не меняется ...