Qtimer不会超时QT,C++
问题描述:
我正在学习C++并使用QT。 我有一个小程序,我试图每秒更新一次PushButton的文本。标签是当前时间。我有一个应该每秒超时的计时器,但似乎从未这样做。这是代码。Qtimer不会超时QT,C++
头文件
#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);
}
任何帮助将不胜感激...按钮的文字永远不会改变......
答
你不” t在课程开始时包含Q_OBJECT
宏。如果你的班级宣布任何信号或插槽(至少,如果你想让它们工作),你需要它。事实上,将它包含在从QObject派生的任何类中通常都是一个好习惯。
修改类的声明看起来像这样:
class HelloForm : public QDialog {
Q_OBJECT;
public:
// Actual code here.
};
现在你已经感谢别人的答案...你应该接受它,使他们获得声誉(和你做的一样好)。 – 2010-05-19 16:09:44