Nominate our 2022 Qt Champions!

Update QlineEdit

  • I'm writing to ask for advice on how best to implement my code with QT. I have a class called Action that every one second retrieves the PC time (with gettimeofday), this value shall be displayed in the GUI. So I have a class widget that defines all the widgets necessary for the GUI. The value (expressed in seconds) will be displayed with a QLineEdit. So my question is, how I have to implement Signal and slot to update the value in QLineEdit? Should I emit a signal every time the function retreiveTimetoSend is called?


    class Action: public object


    Qtimer timer;
    unisgned int timetosend;

    void retreiveTimetoSend()


    public slots:
    void doWork();


    void retreiveTimetoSend(){
    struct timeval Now;
    unsigned int Sec;
    gettimeofday(&Now, NULL);
    Sec = Now.tv_sec;
    time.value =Sec;

    void Action::Action()
    connect(&timer, SIGNAL(timeout()), this, SLOT (doWork()));

    void Action::doWork(){


    class widgets: public QWidget
    QLineEdit *displayTime;

    widget(action *w_test);


    widgets::widgets(action *w_test)
    displayTime= new QLineEdit();
    displayTC->setStyleSheet("color: blue; background-color: red");



    int main(int argc, char *argv[]) {

    QApplication app(argc, argv);

    Action *test = new Action;
    Thread *threadtest = new QThread;

    test ->moveToThread(threadtest);
    QObject::connect(threadtest, SIGNAL(started()), test ,SLOT(doWork()));

    widget *mainwindows = new widget(test);

    threadtest ->start();
    return app.exec();

  • I'm having a bit of a hard time understanding your code.

    What exactly do you want to achieve? Do you want to update a QLineEdit every second, to display the current time? In that case I don't think you need anything besides QTimer, set a to a 1000 msecs delay. Connecting that timer's timeout() signal to a slot where you update the text. Another thread is not necessary here.

  • Moderators

    If you don't want the time to be editable, just use QLabel (you can update the text using setText() function. It's actually the same for QLineEdit).

    You do not need any QThreads or gettimeofday(), that is way too complicated for such a simple task. Just use a QTimer in your main thread, and get the time using "QTime::currentTime":

  • OK thanks for your reply....

    I post only a part of code..which is more complicated and for many reasons the code is based on different therads..
    I have a thread that implements different actions and every 1 second get a time ( I use gettimeofday() because returns second and microsecond) that have to be desplayed.

  • Moderators

    Then, add a signal to your thread, connect it to the main window using Qt::QueuedConnection. Emit the signal in every time you have the new time.

Log in to reply