C++中的动态时钟
Dynamic clock in C++
我想在我的 C++ 程序中实现一个动态时钟,它不仅会显示程序启动时的当前时间,而且还会在程序 运行 时保持滴答作响。有这样做的简单方法吗?在执行此操作时,程序也不应陷入循环,因为时钟只是其他功能之一。
我想到的是使用观察者模式,但是如果有更简单的方法,我将不胜感激,因为我正在为这种模式而苦苦挣扎。
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onEverySecond()));
timer->start(1000);
}
void Foo::onEverySecond()
{
//do fancy stuff
}
如果你像之前说的那样使用qt。最好使用新的信号槽语法:
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &ThisClassName::onEverySecond);
timer->start(1000);
我想在我的 C++ 程序中实现一个动态时钟,它不仅会显示程序启动时的当前时间,而且还会在程序 运行 时保持滴答作响。有这样做的简单方法吗?在执行此操作时,程序也不应陷入循环,因为时钟只是其他功能之一。 我想到的是使用观察者模式,但是如果有更简单的方法,我将不胜感激,因为我正在为这种模式而苦苦挣扎。
{
QTimer *timer = new QTimer(this);
connect(timer, SIGNAL(timeout()), this, SLOT(onEverySecond()));
timer->start(1000);
}
void Foo::onEverySecond()
{
//do fancy stuff
}
如果你像之前说的那样使用qt。最好使用新的信号槽语法:
QTimer *timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &ThisClassName::onEverySecond);
timer->start(1000);