Well, what else do you want it to do? The title is simply a string. If you want it to do something different, then write code to change the string you have stored and let it update the title the next time the timer fires.
If other parts of your app are also calling setWindowTitle for the main window, then that is no good because whatever they set the title to will change the next clock tick. This is why a clock in the status bar makes more sense - it is a standalone widget that doesn't change the behavior of anything else.
Here is some code I wrote to make a digital clock with a transparent background that I can stick in a corner of my Desktop. All of the QSettings and mouse event stuff is just so I can have the clock appear in the same place on the screen after I move it, every time it starts up.
You can easily modify the DigitalClock class to make it work as a status bar widget. See below.
// main.cpp
#include "DigitalClock.h"
#include <QApplication>
#include <QSettings>
int main(int argc, char *argv[])
{
DigitalClock w;
w.show();
return a.exec();
}
// main.cpp
#include "DigitalClock.h"
#include <QApplication>
#include <QSettings>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QCoreApplication::setApplicationName( "Digital Clock" );
QCoreApplication::setOrganizationName( "D_Stranz" );
QCoreApplication::setOrganizationDomain( "Domains R Us" );
QSettings::setDefaultFormat( QSettings::IniFormat );
DigitalClock w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
// DigitalClock.h
#ifndef DIGITALCLOCK_H
#define DIGITALCLOCK_H
#include <QtWidgets/QWidget>
class DigitalClock
: public QWidget{
Q_OBJECT
public:
~DigitalClock();
protected slots:
void onUpdateDateTime();
protected:
void mouseReleaseEvent
( QMouseEvent * pEvent
) override;
private:
bool mbMoving = false;
};
#endif // DIGITALCLOCK_H
// DigitalClock.h
#ifndef DIGITALCLOCK_H
#define DIGITALCLOCK_H
#include <QtWidgets/QWidget>
class QLabel;
class QTimer;
class DigitalClock : public QWidget
{
Q_OBJECT
public:
DigitalClock(QWidget *parent = 0);
~DigitalClock();
protected slots:
void onUpdateDateTime();
protected:
void mousePressEvent( QMouseEvent * pEvent ) override;
void mouseMoveEvent( QMouseEvent * pEvent ) override;
void mouseReleaseEvent( QMouseEvent * pEvent ) override;
void showEvent( QShowEvent * pEvent ) override;
private:
QLabel * mpTimeLabel;
QLabel * mpDateLabel;
QTimer * mpTimer;
QPoint mPos;
bool mbMoving = false;
};
#endif // DIGITALCLOCK_H
To copy to clipboard, switch view to plain text mode
// DigitalClock.cpp
#include "DigitalClock.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QDateTime>
#include <QTime>
#include <QDate>
#include <QFont>
#include <QMouseEvent>
#include <QAction>
#include <QSettings>
DigitalClock
::DigitalClock(QWidget *parent
) : QWidget( parent , Qt
::FramelessWindowHint ) {
setAttribute( Qt::WA_TranslucentBackground, true );
setAttribute( Qt::WA_TransparentForMouseEvents, false );
curFont.setPointSize( 52 );
mpTimeLabel
= new QLabel( "" );
mpTimeLabel->setFont( curFont );
mpTimeLabel->setAlignment( Qt::AlignCenter );
curFont.setPointSize( 32 );
mpDateLabel
= new QLabel( "" );
mpDateLabel->setAlignment( Qt::AlignCenter );
mpDateLabel->setFont( curFont );
pLayout->addWidget( mpTimeLabel );
pLayout->addWidget( mpDateLabel );
setLayout( pLayout );
connect( pExitAction, &QAction::triggered, this, &QWidget::close );
curFont.setPointSize( 12 );
setFont( curFont );
setContextMenuPolicy( Qt::ActionsContextMenu );
addAction( pExitAction );
mpTimer->setInterval( 1000 );
connect( mpTimer, &QTimer::timeout, this, &DigitalClock::onUpdateDateTime );
mpTimer->start();
mPos = mapToGlobal( pos() );
}
DigitalClock::~DigitalClock()
{
}
void DigitalClock::onUpdateDateTime()
{
QTime nowTime
= now.
time();
int hour = nowTime.hour();
hour = ( hour <= 12 ? hour : hour - 12 );
QString timeStr
= QString( "%1:%2:%3" ).
arg( hour,
2,
10,
QChar(' ') ).
arg( nowTime.
minute(),
2,
10,
QChar( '0' ) ).
arg( nowTime.
second(),
2,
10,
QChar( '0' ) );
mpTimeLabel->setText( timeStr );
QDate nowDate
= now.
date();
QString dateStr
= QString( "%1/%2/%3" ).
arg( nowDate.
day(),
2,
10,
QChar( ' ' ) ).
arg( nowDate.
month(),
2,
10,
QChar( '0' ) ).
arg( nowDate.
year(),
4 );
mpDateLabel->setText( dateStr );
}
void DigitalClock
::mousePressEvent( QMouseEvent * pEvent
) {
if ( pEvent->button() == Qt::LeftButton )
{
mPos = mapToGlobal( pEvent->pos() );
mbMoving = true;
}
}
void DigitalClock
::mouseMoveEvent( QMouseEvent * pEvent
) {
if ( mbMoving )
{
mPos = mapToGlobal( pEvent->pos() );
move( mPos );
}
}
void DigitalClock
::mouseReleaseEvent( QMouseEvent * pEvent
) {
mbMoving = false;
settings.beginGroup( "Global" );
settings.
setValue( "Position",
QVariant( mPos
) );
settings.endGroup();
}
void DigitalClock
::showEvent( QShowEvent * pEvent
) {
settings.beginGroup( "Global" );
QVariant var
= settings.
value( "Position" );
settings.endGroup();
if ( !var.isNull() )
{
mPos = var.toPoint();
move( mPos );
}
}
// DigitalClock.cpp
#include "DigitalClock.h"
#include <QVBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QDateTime>
#include <QTime>
#include <QDate>
#include <QFont>
#include <QMouseEvent>
#include <QAction>
#include <QSettings>
DigitalClock::DigitalClock(QWidget *parent)
: QWidget( parent , Qt::FramelessWindowHint )
{
setAttribute( Qt::WA_TranslucentBackground, true );
setAttribute( Qt::WA_TransparentForMouseEvents, false );
QVBoxLayout * pLayout = new QVBoxLayout( this );
QFont curFont = font();
curFont.setPointSize( 52 );
mpTimeLabel = new QLabel( "" );
mpTimeLabel->setFont( curFont );
mpTimeLabel->setAlignment( Qt::AlignCenter );
curFont.setPointSize( 32 );
mpDateLabel = new QLabel( "" );
mpDateLabel->setAlignment( Qt::AlignCenter );
mpDateLabel->setFont( curFont );
pLayout->addWidget( mpTimeLabel );
pLayout->addWidget( mpDateLabel );
setLayout( pLayout );
QAction * pExitAction = new QAction( "Exit", this );
connect( pExitAction, &QAction::triggered, this, &QWidget::close );
curFont.setPointSize( 12 );
setFont( curFont );
setContextMenuPolicy( Qt::ActionsContextMenu );
addAction( pExitAction );
mpTimer = new QTimer( this );
mpTimer->setInterval( 1000 );
connect( mpTimer, &QTimer::timeout, this, &DigitalClock::onUpdateDateTime );
mpTimer->start();
mPos = mapToGlobal( pos() );
}
DigitalClock::~DigitalClock()
{
}
void DigitalClock::onUpdateDateTime()
{
QDateTime now = QDateTime::currentDateTime();
QTime nowTime = now.time();
int hour = nowTime.hour();
hour = ( hour <= 12 ? hour : hour - 12 );
QString timeStr = QString( "%1:%2:%3" ).arg( hour, 2, 10, QChar(' ') ).arg( nowTime.minute(), 2, 10, QChar( '0' ) ).arg( nowTime.second(), 2, 10, QChar( '0' ) );
mpTimeLabel->setText( timeStr );
QDate nowDate = now.date();
QString dateStr = QString( "%1/%2/%3" ).arg( nowDate.day(), 2, 10, QChar( ' ' ) ).arg( nowDate.month(), 2, 10, QChar( '0' ) ).arg( nowDate.year(), 4 );
mpDateLabel->setText( dateStr );
}
void DigitalClock::mousePressEvent( QMouseEvent * pEvent )
{
if ( pEvent->button() == Qt::LeftButton )
{
mPos = mapToGlobal( pEvent->pos() );
mbMoving = true;
}
}
void DigitalClock::mouseMoveEvent( QMouseEvent * pEvent )
{
if ( mbMoving )
{
mPos = mapToGlobal( pEvent->pos() );
move( mPos );
}
}
void DigitalClock::mouseReleaseEvent( QMouseEvent * pEvent )
{
mbMoving = false;
QSettings settings;
settings.beginGroup( "Global" );
settings.setValue( "Position", QVariant( mPos ) );
settings.endGroup();
}
void DigitalClock::showEvent( QShowEvent * pEvent )
{
QSettings settings;
settings.beginGroup( "Global" );
QVariant var = settings.value( "Position" );
settings.endGroup();
if ( !var.isNull() )
{
mPos = var.toPoint();
move( mPos );
}
}
To copy to clipboard, switch view to plain text mode
You can try that code, and then modify it to work as a statusbar widget. This code makes an ordinary widget, changes the layout to horizontal rather than vertical (so the date and time are side-by-side). If you don't want the date, then simply delete that QLabel and change the update slot to remove the formatting for the date label. Adding the widget as a permanent widget in the status bar means that it will always be visible and never be overwritten with a status bar message.
#ifndef DIGITALCLOCK_H
#define DIGITALCLOCK_H
#include <QtWidgets/QWidget>
class DigitalClock
: public QWidget{
Q_OBJECT
public:
~DigitalClock();
protected slots:
void onUpdateDateTime();
private:
};
#ifndef DIGITALCLOCK_H
#define DIGITALCLOCK_H
#include <QtWidgets/QWidget>
class QLabel;
class QTimer;
class DigitalClock : public QWidget
{
Q_OBJECT
public:
DigitalClock(QWidget *parent = 0);
~DigitalClock();
protected slots:
void onUpdateDateTime();
private:
QLabel * mpTimeLabel;
QLabel * mpDateLabel;
QTimer * mpTimer;
};
To copy to clipboard, switch view to plain text mode
#include "DigitalClock.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QDateTime>
#include <QTime>
#include <QDate>
DigitalClock
::DigitalClock(QWidget *parent
){
mpTimeLabel
= new QLabel( "" );
mpTimeLabel->setAlignment( Qt::AlignLeft );
mpDateLabel
= new QLabel( "" );
mpDateLabel->setAlignment( Qt::AlignRight );
pLayout->addWidget( mpTimeLabel );
pLayout->addWidget( mpDateLabel );
setLayout( pLayout );
mpTimer->setInterval( 1000 );
connect( mpTimer, &QTimer::timeout, this, &DigitalClock::onUpdateDateTime );
mpTimer->start();
}
DigitalClock::~DigitalClock()
{
}
void DigitalClock::onUpdateDateTime()
{
QTime nowTime
= now.
time();
int hour = nowTime.hour();
hour = ( hour <= 12 ? hour : hour - 12 );
QString timeStr
= QString( "%1:%2:%3" ).
arg( hour,
2,
10,
QChar(' ') ).
arg( nowTime.
minute(),
2,
10,
QChar( '0' ) ).
arg( nowTime.
second(),
2,
10,
QChar( '0' ) );
mpTimeLabel->setText( timeStr );
QDate nowDate
= now.
date();
QString dateStr
= QString( "%1/%2/%3" ).
arg( nowDate.
day(),
2,
10,
QChar( ' ' ) ).
arg( nowDate.
month(),
2,
10,
QChar( '0' ) ).
arg( nowDate.
year(),
4 );
mpDateLabel->setText( dateStr );
}
#include "DigitalClock.h"
#include <QHBoxLayout>
#include <QLabel>
#include <QTimer>
#include <QDateTime>
#include <QTime>
#include <QDate>
DigitalClock::DigitalClock(QWidget *parent)
: QWidget( parent )
{
QHBoxLayout * pLayout = new QHBoxLayout( this );
mpTimeLabel = new QLabel( "" );
mpTimeLabel->setAlignment( Qt::AlignLeft );
mpDateLabel = new QLabel( "" );
mpDateLabel->setAlignment( Qt::AlignRight );
pLayout->addWidget( mpTimeLabel );
pLayout->addWidget( mpDateLabel );
setLayout( pLayout );
mpTimer = new QTimer( this );
mpTimer->setInterval( 1000 );
connect( mpTimer, &QTimer::timeout, this, &DigitalClock::onUpdateDateTime );
mpTimer->start();
}
DigitalClock::~DigitalClock()
{
}
void DigitalClock::onUpdateDateTime()
{
QDateTime now = QDateTime::currentDateTime();
QTime nowTime = now.time();
int hour = nowTime.hour();
hour = ( hour <= 12 ? hour : hour - 12 );
QString timeStr = QString( "%1:%2:%3" ).arg( hour, 2, 10, QChar(' ') ).arg( nowTime.minute(), 2, 10, QChar( '0' ) ).arg( nowTime.second(), 2, 10, QChar( '0' ) );
mpTimeLabel->setText( timeStr );
QDate nowDate = now.date();
QString dateStr = QString( "%1/%2/%3" ).arg( nowDate.day(), 2, 10, QChar( ' ' ) ).arg( nowDate.month(), 2, 10, QChar( '0' ) ).arg( nowDate.year(), 4 );
mpDateLabel->setText( dateStr );
}
To copy to clipboard, switch view to plain text mode
#include "DigitalClock.h"
#include <QApplication>
#include <QMainWindow>
#include <QStatusBar>
int main(int argc, char *argv[])
{
DigitalClock * pClock = new DigitalClock( &w );
pBar->addPermanentWidget( pClock );
w.show();
return a.exec();
}
#include "DigitalClock.h"
#include <QApplication>
#include <QMainWindow>
#include <QStatusBar>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QMainWindow w;
DigitalClock * pClock = new DigitalClock( &w );
QStatusBar * pBar = w.statusBar();
pBar->addPermanentWidget( pClock );
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks