Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Singleton QDateTime Issues

Singleton QDateTime Issues

Scheduled Pinned Locked Moved Unsolved General and Desktop
12 Posts 5 Posters 928 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Simof
    wrote on last edited by
    #1

    Is it possible to make singleton class that inherits from QDateTime?

    I would like use the same QDateTime instance insted of currentTime in all my classes.

    For exemple:

    #ifndef CALENDAR_H
    #define CALENDAR_H
    
    #include <QDateTime>
    
    #include <QObject>
    
    class Calendar : public QDateTime
    {
        Q_OBJECT
    
    public:
    
        virtual ~Calendar() {}
    
        static Calendar & GetInstance()
        {
            static Calendar instance;
            return instance;
        }
    
    private:
        explicit Calendar() : QDateTime() {}
    
    };
    
    #endif // CALENDAR_H
    
    

    This code produce a lots of MOQ errors

    CP71C 1 Reply Last reply
    0
    • S Simof

      Is it possible to make singleton class that inherits from QDateTime?

      I would like use the same QDateTime instance insted of currentTime in all my classes.

      For exemple:

      #ifndef CALENDAR_H
      #define CALENDAR_H
      
      #include <QDateTime>
      
      #include <QObject>
      
      class Calendar : public QDateTime
      {
          Q_OBJECT
      
      public:
      
          virtual ~Calendar() {}
      
          static Calendar & GetInstance()
          {
              static Calendar instance;
              return instance;
          }
      
      private:
          explicit Calendar() : QDateTime() {}
      
      };
      
      #endif // CALENDAR_H
      
      

      This code produce a lots of MOQ errors

      CP71C Offline
      CP71C Offline
      CP71
      wrote on last edited by
      #2

      @Simof
      Hi,
      try to remove Q_OBJECT

      S 1 Reply Last reply
      1
      • Christian EhrlicherC Offline
        Christian EhrlicherC Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on last edited by Christian Ehrlicher
        #3

        And what do you think you gain with this class??

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        S 1 Reply Last reply
        4
        • Christian EhrlicherC Christian Ehrlicher

          And what do you think you gain with this class??

          S Offline
          S Offline
          Simof
          wrote on last edited by
          #4

          @Christian-Ehrlicher said in Singleton QDateTime Issues:

          And what do you think you gain with this class??

          I would include this class in all widgets and use this instead of System Clock

          QString dateTimeStr = Calendar::GetInstance().toString("\"yyyy-MM-dd hh:mm\"");
          dateTimeLbl = new QLabel(dateTimeStr);
          
          jsulmJ 2 Replies Last reply
          0
          • S Simof

            @Christian-Ehrlicher said in Singleton QDateTime Issues:

            And what do you think you gain with this class??

            I would include this class in all widgets and use this instead of System Clock

            QString dateTimeStr = Calendar::GetInstance().toString("\"yyyy-MM-dd hh:mm\"");
            dateTimeLbl = new QLabel(dateTimeStr);
            
            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Simof You should read https://doc.qt.io/qt-5/qdatetime.html#QDateTime

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • S Simof

              @Christian-Ehrlicher said in Singleton QDateTime Issues:

              And what do you think you gain with this class??

              I would include this class in all widgets and use this instead of System Clock

              QString dateTimeStr = Calendar::GetInstance().toString("\"yyyy-MM-dd hh:mm\"");
              dateTimeLbl = new QLabel(dateTimeStr);
              
              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @Simof Why don't you simply use static https://doc.qt.io/qt-5/qdatetime.html#currentDateTime instead of introducing a singleton?!

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              S 1 Reply Last reply
              1
              • CP71C CP71

                @Simof
                Hi,
                try to remove Q_OBJECT

                S Offline
                S Offline
                Simof
                wrote on last edited by
                #7

                @CP71 said in Singleton QDateTime Issues:

                @Simof
                Hi,
                try to remove Q_OBJECT

                Now it works correctly, thanks

                1 Reply Last reply
                1
                • jsulmJ jsulm

                  @Simof Why don't you simply use static https://doc.qt.io/qt-5/qdatetime.html#currentDateTime instead of introducing a singleton?!

                  S Offline
                  S Offline
                  Simof
                  wrote on last edited by
                  #8

                  @jsulm said in Singleton QDateTime Issues:

                  @Simof Why don't you simply use static https://doc.qt.io/qt-5/qdatetime.html#currentDateTime instead of introducing a singleton?!

                  Can I define a static QDateTime in main mainwindow and use it in all widgets included inside itself?

                  jsulmJ CP71C 2 Replies Last reply
                  0
                  • S Simof

                    @jsulm said in Singleton QDateTime Issues:

                    @Simof Why don't you simply use static https://doc.qt.io/qt-5/qdatetime.html#currentDateTime instead of introducing a singleton?!

                    Can I define a static QDateTime in main mainwindow and use it in all widgets included inside itself?

                    jsulmJ Offline
                    jsulmJ Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Simof said in Singleton QDateTime Issues:

                    Can I define a static QDateTime in main mainwindow and use it in all widgets included inside itself?

                    Why do you need a static QDateTime? What is your use case?

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • S Simof

                      @jsulm said in Singleton QDateTime Issues:

                      @Simof Why don't you simply use static https://doc.qt.io/qt-5/qdatetime.html#currentDateTime instead of introducing a singleton?!

                      Can I define a static QDateTime in main mainwindow and use it in all widgets included inside itself?

                      CP71C Offline
                      CP71C Offline
                      CP71
                      wrote on last edited by CP71
                      #10

                      @Simof
                      Hi
                      As @jsulm says , you don't need a singleton and static class to do this if you have to do something else that we don't know.
                      To do this you can write QString text = QDateTime::currentDateTime().toString(""yyyy-MM-dd hh:mm""); without declare new class o singleton class.

                      S 1 Reply Last reply
                      0
                      • CP71C CP71

                        @Simof
                        Hi
                        As @jsulm says , you don't need a singleton and static class to do this if you have to do something else that we don't know.
                        To do this you can write QString text = QDateTime::currentDateTime().toString(""yyyy-MM-dd hh:mm""); without declare new class o singleton class.

                        S Offline
                        S Offline
                        Simof
                        wrote on last edited by Simof
                        #11

                        @CP71 said in Singleton QDateTime Issues:

                        @Simof
                        Hi
                        As @jsulm says , you don't need a singleton and static class to do this if you have to do something else that we don't know.
                        To do this you can write QString text = QDateTime::currentDateTime().toString(""yyyy-MM-dd hh:mm""); without declare new class o singleton class.

                        My application works on embedded device and the system time is not reliable so I would use a RTC (Real Time Clock IC) such a main clock.

                        I could use QDateTime::currentDateTime().toString(""yyyy-MM-dd hh:mm"") only if I can set my system time equal to RTC time but I don't know if it's possible

                        JonBJ 1 Reply Last reply
                        0
                        • S Simof

                          @CP71 said in Singleton QDateTime Issues:

                          @Simof
                          Hi
                          As @jsulm says , you don't need a singleton and static class to do this if you have to do something else that we don't know.
                          To do this you can write QString text = QDateTime::currentDateTime().toString(""yyyy-MM-dd hh:mm""); without declare new class o singleton class.

                          My application works on embedded device and the system time is not reliable so I would use a RTC (Real Time Clock IC) such a main clock.

                          I could use QDateTime::currentDateTime().toString(""yyyy-MM-dd hh:mm"") only if I can set my system time equal to RTC time but I don't know if it's possible

                          JonBJ Offline
                          JonBJ Offline
                          JonB
                          wrote on last edited by
                          #12

                          @Simof
                          So don't use raw currentDateTime. If you don't want to repeatedly read RTC (I don't do embedded, maybe that is expensive or inconvenient?), read it once and get currentDateTime() at same time, calculate offset difference, use that adjustment amount to currentDateTime() from then onward?

                          1 Reply Last reply
                          0

                          • Login

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved