I am trying to learn Qt on an uBuntu 20.04 Linux Desktop Computer by means of writing a simple program to add a date/clock on its main window title bar. So, I did a google search and found only this https://www.qtcentre.org/threads/56410-Placing-Clock-on-Title-Bar?highlight=Clock+in+Window+Title (which drove me to this very forum) article. Unfortunately, the article didn't help at all. So, I decided to write my own as shown below. The program works just fine (no blinking on the separated "". However, the date/clock seems to have replaced the original Window Title. The question is how to I add the date/clock sans losing the original title? While at it, I sure would like to make the separated ":" to blink every second if possible. Anyone?
windowtitleclock.h
#ifndef WINDOWTITLECLOCK_H
#define WINDOWTITLECLOCK_H
#include <QMainWindow>
#include <QTimer>
#include <QDateTime>
QT_BEGIN_NAMESPACE
namespace Ui { class WindowTitleClock; }
QT_END_NAMESPACE
{
Q_OBJECT
public:
WindowTitleClock
(QWidget *parent
= nullptr
);
~WindowTitleClock();
public slots:
void windowTitleClock ();
private:
Ui::WindowTitleClock *ui;
};
#endif // WINDOWTITLECLOCK_H
#ifndef WINDOWTITLECLOCK_H
#define WINDOWTITLECLOCK_H
#include <QMainWindow>
#include <QTimer>
#include <QDateTime>
QT_BEGIN_NAMESPACE
namespace Ui { class WindowTitleClock; }
QT_END_NAMESPACE
class WindowTitleClock : public QMainWindow
{
Q_OBJECT
public:
WindowTitleClock(QWidget *parent = nullptr);
~WindowTitleClock();
public slots:
void windowTitleClock ();
private:
Ui::WindowTitleClock *ui;
QTimer *timer;
};
#endif // WINDOWTITLECLOCK_H
To copy to clipboard, switch view to plain text mode
windowtitleclock.cpp
#include "windowtitleclock.h"
#include "ui_windowtitleclock.h"
WindowTitleClock
::WindowTitleClock(QWidget *parent
) , ui(new Ui::WindowTitleClock)
{
ui->setupUi(this);
connect (timer, SIGNAL (timeout ()), this, SLOT (windowTitleClock ()));
timer->start (1000);
}
WindowTitleClock::~WindowTitleClock()
{
delete ui;
}
void WindowTitleClock::windowTitleClock()
{
this
->setWindowTitle
(QDate::currentDate().
toString() + " " + QTime::currentTime().
toString("[hh:mm:ss]"));
}
#include "windowtitleclock.h"
#include "ui_windowtitleclock.h"
WindowTitleClock::WindowTitleClock(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::WindowTitleClock)
{
ui->setupUi(this);
timer = new QTimer (this);
connect (timer, SIGNAL (timeout ()), this, SLOT (windowTitleClock ()));
timer->start (1000);
}
WindowTitleClock::~WindowTitleClock()
{
delete ui;
}
void WindowTitleClock::windowTitleClock()
{
this->setWindowTitle (QDate::currentDate().toString() + " " + QTime::currentTime().toString("[hh:mm:ss]"));
}
To copy to clipboard, switch view to plain text mode
main.cpp
#include "windowtitleclock.h"
#include <QApplication>
int main(int argc, char *argv[])
{
WindowTitleClock w;
w.show();
return a.exec();
}
#include "windowtitleclock.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
WindowTitleClock w;
w.show();
return a.exec();
}
To copy to clipboard, switch view to plain text mode
WindowTitleClock.pro
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += include
SOURCES += \
src/main.cpp \
src/windowtitleclock.cpp
HEADERS += \
include/windowtitleclock.h
FORMS += \
resources/windowtitleclock.ui
TRANSLATIONS += \
resources/WindowTitleClock_en_US.ts
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
INCLUDEPATH += include
SOURCES += \
src/main.cpp \
src/windowtitleclock.cpp
HEADERS += \
include/windowtitleclock.h
FORMS += \
resources/windowtitleclock.ui
TRANSLATIONS += \
resources/WindowTitleClock_en_US.ts
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
To copy to clipboard, switch view to plain text mode
Bookmarks