Reputation: 137
What is a simple way to display the actual time (Hh: mm: ss) in MainWindow
's title ?
Using slots and signals technology.
Upvotes: 1
Views: 6168
Reputation: 7748
I'm building my answer on the one provided by Riateche. Instead of using a user defined QTimer
and connecting signals/slots, you can use the provided timerEvent()
of any QObject
. It will basically do the same under the hood, but save you a lot of typing. This would look like this:
class MainWindow : public QMainWindow
{
public:
MainWindow();
protected:
void timerEvent(QTimerEvent *event);
};
MainWindow::MainWindow()
{
startTimer(1000); // 1-second timer
}
void MainWindow::timerEvent(QTimerEvent * event)
{
setWindowTitle(QTime::currentTime().toString("hh:mm:ss"));
}
Upvotes: 5
Reputation: 40502
Create a QTimer
with 1 sec interval (or e.g. 100 msec for more accuracy), connect its timeout
signal to your slot. In the slot get the current time using QTime::currentTime()
static function, convert it to string using toString
and assign it to a GUI element (e.g. a label).
Upvotes: 3