Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:40:07+00:00 2026-06-12T19:40:07+00:00

Using the Qt library, is there a way to automatically update a QDateTimeEdit using

  • 0

Using the Qt library, is there a way to automatically update a QDateTimeEdit using a signal such that the widget shows the current date and time in a similar fashion to a clock?

In the code sample below, dateTimeEditSystem is the QDateTimeEdit object. However, the setDateTime() function only operates once. I would like the QDateTimeEdit object to dynamically update. Is there a reliable way of doing this without using a timer (i.e. with signals and slots)? Or is a timer the only way of doing this?

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    // setup the UI
    ui->setupUi(this);
    // set the current date and time
    ui->dateTimeEditSystem->setDateTime( QDateTime::currentDateTime() );
}

I tried using a timer, but unfortunately the QDateTimeEdit did not update. Here is the complete mainwindow.cpp code. What am I doing wrong here?

#include <QTimer>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    ui->dateTimeEditSystem->setDateTime( QDateTime::currentDateTime() );

    QTimer *timer = new QTimer(this);
    connect(timer, SIGNAL(timeout()), this, SLOT( updateTime() ));
    timer->start(1000);
}

void MainWindow::updateTime()
{
    ui->dateTimeEditSystem->setDateTime( QDateTime::currentDateTime() );
}

MainWindow::~MainWindow()
{
    delete ui;
}

UPDATE:

This is actually very easy to do. The reason why the code was not updating was due to the lack of a slot declaration in the mainwindow.h file. Here is the complete contents of the mainwindow.h header.

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private:
    Ui::MainWindow *ui;

 private slots:
     void updateTime();

};

#endif // MAINWINDOW_H
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:40:08+00:00Added an answer on June 12, 2026 at 7:40 pm

    Automatically updating a QDateTimeEdit or any other clock widget within Qt is easily accomplished with a QTimer. Refer to the following examples for how to accomplish this:

    http://qt-project.org/doc/qt-4.8/widgets-digitalclock.html

    OR

    http://qt-project.org/doc/qt-4.8/widgets-shapedclock.html

    Note that the resolution of a QTimer varies on different operating systems, but for updates that need to occur ~1 second at a time, a QTimer should be fine on virtually all platforms. Refer to the following for more information on the resolution of QTimer:

    http://qt-project.org/doc/qt-4.8/QTimer.html#accuracy-and-timer-resolution

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to temporary use a namespace ? I'm using a library
Is there a way using the Net::SFTP Library in Ruby ( API Link )
We are using a third part library to render 3d. In this library there
Is there any external library using which one can edit and save XML files
Is there anyway to create C# spelling checker without using Office library? I would
I am trying to avoid using Net::SSH::Perl library since there is some problems in
Anyone out there using the FTUtils library for iPhone development? Following the instructions here
There are instructions here to create a C# assembly using the SimMetrics library. The
I am writing my own Firebird database browser using the ibpp library. Is there
I have a library that generates Word documents using the OpenXML SDK, one of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.