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
Qt Code:
  1. #ifndef WINDOWTITLECLOCK_H
  2. #define WINDOWTITLECLOCK_H
  3.  
  4. #include <QMainWindow>
  5. #include <QTimer>
  6. #include <QDateTime>
  7.  
  8. QT_BEGIN_NAMESPACE
  9. namespace Ui { class WindowTitleClock; }
  10. QT_END_NAMESPACE
  11.  
  12. class WindowTitleClock : public QMainWindow
  13. {
  14. Q_OBJECT
  15.  
  16. public:
  17. WindowTitleClock(QWidget *parent = nullptr);
  18. ~WindowTitleClock();
  19.  
  20. public slots:
  21. void windowTitleClock ();
  22.  
  23. private:
  24. Ui::WindowTitleClock *ui;
  25. QTimer *timer;
  26. };
  27. #endif // WINDOWTITLECLOCK_H
To copy to clipboard, switch view to plain text mode 

windowtitleclock.cpp
Qt Code:
  1. #include "windowtitleclock.h"
  2. #include "ui_windowtitleclock.h"
  3.  
  4. WindowTitleClock::WindowTitleClock(QWidget *parent)
  5. : QMainWindow(parent)
  6. , ui(new Ui::WindowTitleClock)
  7. {
  8. ui->setupUi(this);
  9. timer = new QTimer (this);
  10. connect (timer, SIGNAL (timeout ()), this, SLOT (windowTitleClock ()));
  11. timer->start (1000);
  12. }
  13.  
  14. WindowTitleClock::~WindowTitleClock()
  15. {
  16. delete ui;
  17. }
  18.  
  19. void WindowTitleClock::windowTitleClock()
  20. {
  21. this->setWindowTitle (QDate::currentDate().toString() + " " + QTime::currentTime().toString("[hh:mm:ss]"));
  22. }
To copy to clipboard, switch view to plain text mode 


main.cpp
Qt Code:
  1. #include "windowtitleclock.h"
  2.  
  3. #include <QApplication>
  4.  
  5. int main(int argc, char *argv[])
  6. {
  7. QApplication a(argc, argv);
  8. WindowTitleClock w;
  9. w.show();
  10. return a.exec();
  11. }
To copy to clipboard, switch view to plain text mode 

WindowTitleClock.pro
Qt Code:
  1. QT += core gui
  2.  
  3. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  4.  
  5. CONFIG += c++11
  6.  
  7. # The following define makes your compiler emit warnings if you use
  8. # any Qt feature that has been marked deprecated (the exact warnings
  9. # depend on your compiler). Please consult the documentation of the
  10. # deprecated API in order to know how to port your code away from it.
  11. DEFINES += QT_DEPRECATED_WARNINGS
  12.  
  13. # You can also make your code fail to compile if it uses deprecated APIs.
  14. # In order to do so, uncomment the following line.
  15. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  16. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  17.  
  18. INCLUDEPATH += include
  19. SOURCES += \
  20. src/main.cpp \
  21. src/windowtitleclock.cpp
  22.  
  23. HEADERS += \
  24. include/windowtitleclock.h
  25.  
  26. FORMS += \
  27. resources/windowtitleclock.ui
  28.  
  29. TRANSLATIONS += \
  30. resources/WindowTitleClock_en_US.ts
  31.  
  32. # Default rules for deployment.
  33. qnx: target.path = /tmp/$${TARGET}/bin
  34. else: unix:!android: target.path = /opt/$${TARGET}/bin
  35. !isEmpty(target.path): INSTALLS += target
To copy to clipboard, switch view to plain text mode