Unable to Display both time and date together using QLCDnumber



  • @#include<lcdtd.h>
    #include<QtGui>

    lcdtd::lcdtd(QLCDNumber *parent):QLCDNumber(parent)
    {
    setWindowTitle("Time And Date");
    QTimer *timer = new QTimer(this);
    connect(timer,SIGNAL(timeout()),this,SLOT(showDate()));
    QTimer *timex = new QTimer(this);
    connect(timex,SIGNAL(timeout()),this,SLOT(showTime()));
    timex->start(1000);

    resize(200,200);
    setDigitCount(10);
    

    }

    void lcdtd::showDate()
    {
    QDate date = QDate::currentDate();

    QString tex= date.toString("dd.MM.yyyy");
    this->display(tex);
    

    }
    void lcdtd::showTime()
    {
    QTime time = QTime::currentTime();

    QString text= time.toString("hh:mm:ss ap");
    this->display(text);
    

    }
    @



  • bq. this->display(tex);

    I guess "display()" overrides the previous input in the QLCDNumber and what will be shown will be the last one. Why not use QDateTime, instead of separate QDate and QTime?



  • Your solution is very right. But is there any way to use both QDate and QTime and display both of them in a same window??


  • Moderators

    [quote author="rajatgupta431" date="1359198118"]@#include<lcdtd.h>
    #include<QtGui>

    lcdtd::lcdtd(QLCDNumber *parent):QLCDNumber(parent)
    {
    setWindowTitle("Time And Date");
    // QTimer *timer = new QTimer(this);
    // connect(timer,SIGNAL(timeout()),this,SLOT(showDate()));
    QTimer *timex = new QTimer(this);
    connect(timex,SIGNAL(timeout()),this,SLOT(showTime()));
    timex->start(1000);

    resize(200,200);
    setDigitCount(10);
    

    }

    void lcdtd::showTime()
    {
    QDate date = QDate::currentDate();

    QString tex= date.toString("dd.MM.yyyy");
    QTime time = QTime::currentTime();
    
    QString text= tex + " " + time.toString("hh:mm:ss ap");
    this->display(text);
    

    }
    @[/quote]
    something crude and very simple would be this above.

    More sophisticated when you are holding somewhere the converted date and reuse.

    Also you can use "QDateTime":http://qt-project.org/doc/qt-5.0/qtcore/qdatetime.html#currentDateTime there is also "toString":http://qt-project.org/doc/qt-5.0/qtcore/qdatetime.html#toString available


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.