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. A Clock isn't show hours,who can tell me what has happened? it's show like this format MM:SS
Forum Updated to NodeBB v4.3 + New Features

A Clock isn't show hours,who can tell me what has happened? it's show like this format MM:SS

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 3 Posters 2.3k Views 2 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.
  • K Offline
    K Offline
    konalo
    wrote on last edited by
    #1

    A Clock isn't show hour,who can tell me what has happened?
    i just want it show HH:MM:SS

    //digiclock.h
    #ifndef DIGICLOCK_H
    #define DIGICLOCK_H
    #include<QLCDNumber>
    
    class DigiClock : public QLCDNumber
    {
        Q_OBJECT
    public:
        DigiClock(QWidget * parent=0);
        void mousePressEvent(QMouseEvent *);
        void mouseMoveEvent(QMouseEvent *);
    private slots:
        void showTime();//显示当前时间
    private:
        QPoint dragPosition;//保存鼠标点相对电子时钟窗体左上角的偏移值
        bool showColon;//用于显示时间是否显示" : "
    
    };
    
    #endif // DIGICLOCK_H
    
    //digiclock.cpp
    #include "digiclock.h"
    #include<QTimer>
    #include<QTime>
    #include<QMouseEvent>
    #include<QPalette>
    DigiClock::DigiClock(QWidget * parent):
        QLCDNumber(parent)
    {
        /*设置时钟背景*/
        QPalette p=palette();
        p.setColor(QPalette::Window,Qt::yellow);
        setPalette(p);
        setWindowFlags(Qt::FramelessWindowHint);//?//设置窗体标示,此处设置为一个没有面板边框和标题栏的窗体
        setWindowTitle(tr("电子时钟"));
        setWindowOpacity(0.5);//设置窗体透明度
        QTimer *timer=new QTimer(this);
        connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
        timer->start(1000);
        showTime();
        resize(350,60);
        showColon=true;
    }
    
    void DigiClock::mousePressEvent(QMouseEvent * event)
    {
        if(event->button()==Qt::LeftButton)
        {
            dragPosition=event->globalPos()-frameGeometry().topLeft();
            event->accept();
        }
        if(event->button()==Qt::RightButton)
        {
            close();
        }
    }
    
    void DigiClock::mouseMoveEvent(QMouseEvent *event)
    {
        if(event->buttons()&Qt::LeftButton)
        {
            move(event->globalPos()-dragPosition);
            event->accept();
        }
    }
    
    void DigiClock::showTime()
    {
        QTime time=QTime::currentTime();
        QString text=time.toString("hh:mm:ss");
        if(showColon)
        {
            text[2]=':';
    
            showColon=false;
        }
        else
        {
            text[2]=' ';
    
            showColon=true;
        }
        display(text);
    }
    
    
    raven-worxR K 2 Replies Last reply
    0
    • K konalo

      A Clock isn't show hour,who can tell me what has happened?
      i just want it show HH:MM:SS

      //digiclock.h
      #ifndef DIGICLOCK_H
      #define DIGICLOCK_H
      #include<QLCDNumber>
      
      class DigiClock : public QLCDNumber
      {
          Q_OBJECT
      public:
          DigiClock(QWidget * parent=0);
          void mousePressEvent(QMouseEvent *);
          void mouseMoveEvent(QMouseEvent *);
      private slots:
          void showTime();//显示当前时间
      private:
          QPoint dragPosition;//保存鼠标点相对电子时钟窗体左上角的偏移值
          bool showColon;//用于显示时间是否显示" : "
      
      };
      
      #endif // DIGICLOCK_H
      
      //digiclock.cpp
      #include "digiclock.h"
      #include<QTimer>
      #include<QTime>
      #include<QMouseEvent>
      #include<QPalette>
      DigiClock::DigiClock(QWidget * parent):
          QLCDNumber(parent)
      {
          /*设置时钟背景*/
          QPalette p=palette();
          p.setColor(QPalette::Window,Qt::yellow);
          setPalette(p);
          setWindowFlags(Qt::FramelessWindowHint);//?//设置窗体标示,此处设置为一个没有面板边框和标题栏的窗体
          setWindowTitle(tr("电子时钟"));
          setWindowOpacity(0.5);//设置窗体透明度
          QTimer *timer=new QTimer(this);
          connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
          timer->start(1000);
          showTime();
          resize(350,60);
          showColon=true;
      }
      
      void DigiClock::mousePressEvent(QMouseEvent * event)
      {
          if(event->button()==Qt::LeftButton)
          {
              dragPosition=event->globalPos()-frameGeometry().topLeft();
              event->accept();
          }
          if(event->button()==Qt::RightButton)
          {
              close();
          }
      }
      
      void DigiClock::mouseMoveEvent(QMouseEvent *event)
      {
          if(event->buttons()&Qt::LeftButton)
          {
              move(event->globalPos()-dragPosition);
              event->accept();
          }
      }
      
      void DigiClock::showTime()
      {
          QTime time=QTime::currentTime();
          QString text=time.toString("hh:mm:ss");
          if(showColon)
          {
              text[2]=':';
      
              showColon=false;
          }
          else
          {
              text[2]=' ';
      
              showColon=true;
          }
          display(text);
      }
      
      
      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @konalo
      see the docs
      The QLCDNumber widget just displays integers. Thus no space and no colon allowed

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      K 1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @raven-worx You are looking at the wrong function doc. See here. Column is one of the authorised char.

        @konalo You should start from the Digital clock example without modification and then build from it.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        K raven-worxR 2 Replies Last reply
        0
        • SGaistS SGaist

          @raven-worx You are looking at the wrong function doc. See here. Column is one of the authorised char.

          @konalo You should start from the Digital clock example without modification and then build from it.

          K Offline
          K Offline
          konalo
          wrote on last edited by
          #4

          @SGaist what's your mean? ??_??

          1 Reply Last reply
          0
          • SGaistS SGaist

            @raven-worx You are looking at the wrong function doc. See here. Column is one of the authorised char.

            @konalo You should start from the Digital clock example without modification and then build from it.

            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by raven-worx
            #5

            @SGaist said in A Clock isn't show hours,who can tell me what has happened? it's show like this format MM:SS:

            @raven-worx You are looking at the wrong function doc. See here. Column is one of the authorised char.

            oh i was looking at the 4.8 docs... haven noticed.

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            0
            • raven-worxR raven-worx

              @konalo
              see the docs
              The QLCDNumber widget just displays integers. Thus no space and no colon allowed

              K Offline
              K Offline
              konalo
              wrote on last edited by
              #6

              @raven-worx hum, but i got hh:mm or mm:ss ,when i set the format hh:mm and mm:ss
              ,but when i set the text format hh:mm:ss , it's show me mm:ss

              1 Reply Last reply
              0
              • K konalo

                A Clock isn't show hour,who can tell me what has happened?
                i just want it show HH:MM:SS

                //digiclock.h
                #ifndef DIGICLOCK_H
                #define DIGICLOCK_H
                #include<QLCDNumber>
                
                class DigiClock : public QLCDNumber
                {
                    Q_OBJECT
                public:
                    DigiClock(QWidget * parent=0);
                    void mousePressEvent(QMouseEvent *);
                    void mouseMoveEvent(QMouseEvent *);
                private slots:
                    void showTime();//显示当前时间
                private:
                    QPoint dragPosition;//保存鼠标点相对电子时钟窗体左上角的偏移值
                    bool showColon;//用于显示时间是否显示" : "
                
                };
                
                #endif // DIGICLOCK_H
                
                //digiclock.cpp
                #include "digiclock.h"
                #include<QTimer>
                #include<QTime>
                #include<QMouseEvent>
                #include<QPalette>
                DigiClock::DigiClock(QWidget * parent):
                    QLCDNumber(parent)
                {
                    /*设置时钟背景*/
                    QPalette p=palette();
                    p.setColor(QPalette::Window,Qt::yellow);
                    setPalette(p);
                    setWindowFlags(Qt::FramelessWindowHint);//?//设置窗体标示,此处设置为一个没有面板边框和标题栏的窗体
                    setWindowTitle(tr("电子时钟"));
                    setWindowOpacity(0.5);//设置窗体透明度
                    QTimer *timer=new QTimer(this);
                    connect(timer,SIGNAL(timeout()),this,SLOT(showTime()));
                    timer->start(1000);
                    showTime();
                    resize(350,60);
                    showColon=true;
                }
                
                void DigiClock::mousePressEvent(QMouseEvent * event)
                {
                    if(event->button()==Qt::LeftButton)
                    {
                        dragPosition=event->globalPos()-frameGeometry().topLeft();
                        event->accept();
                    }
                    if(event->button()==Qt::RightButton)
                    {
                        close();
                    }
                }
                
                void DigiClock::mouseMoveEvent(QMouseEvent *event)
                {
                    if(event->buttons()&Qt::LeftButton)
                    {
                        move(event->globalPos()-dragPosition);
                        event->accept();
                    }
                }
                
                void DigiClock::showTime()
                {
                    QTime time=QTime::currentTime();
                    QString text=time.toString("hh:mm:ss");
                    if(showColon)
                    {
                        text[2]=':';
                
                        showColon=false;
                    }
                    else
                    {
                        text[2]=' ';
                
                        showColon=true;
                    }
                    display(text);
                }
                
                
                K Offline
                K Offline
                konalo
                wrote on last edited by
                #7

                @konalo @raven-worx @SGaist
                got it , in doc detailed description of QLCDNumber "The QLCDNumber widget displays a number with LCD-like digits.
                It can display a number in just about any size. It can display decimal, hexadecimal, octal or binary numbers. It is easy to connect to data sources using the display() slot, which is overloaded to take any of five argument types." it only show five by default , add

                DigiClock::setDigitCount(8);
                

                i got what i want,just HH:MM:SS

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @raven-worx, it's really just the wrong overload you were looking at ;)

                  @konalo Great ! Thanks for sharing your solution.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  raven-worxR 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    @raven-worx, it's really just the wrong overload you were looking at ;)

                    @konalo Great ! Thanks for sharing your solution.

                    raven-worxR Offline
                    raven-worxR Offline
                    raven-worx
                    Moderators
                    wrote on last edited by
                    #9

                    @SGaist said in A Clock isn't show hours,who can tell me what has happened? it's show like this format MM:SS:

                    @raven-worx, it's really just the wrong overload you were looking at ;)

                    no, compare the Qt 4.8 docs and the Qt 5 docs of QLCDNumber's display(QString) method. They have changed semantically.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      My bad, it was in the detailed description for Qt 4.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      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