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?

    @action.h

    class Action: public object
    {

    Q_OBJECT

    private:
    Qtimer timer;
    unisgned int timetosend;

    private:
    void retreiveTimetoSend()

    public:
    Action();
    ~Action();

    public slots:
    void doWork();
    }

    action.cpp

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

    void Action::Action()
    {
    timer.setInterval(1000);
    connect(&timer, SIGNAL(timeout()), this, SLOT (doWork()));
    timer.start();
    }

    void Action::doWork(){
    retreiveTimetoSend()
    }

    widget.h

    class widgets: public QWidget
    {
    Q_OBJECT
    private:
    QLineEdit *displayTime;

    public:
    widget(action *w_test);
    }

    widget.cpp

    widgets::widgets(action *w_test)
    {
    displayTime= new QLineEdit();
    displayTime->setText(QString::number(w_test->timetosend,10));
    displayTC->setStyleSheet("color: blue; background-color: red");

    }
    

    main.cpp

    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);
    mywindow->show();

    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":http://qt-project.org/doc/qt-5/qtime.html#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
 

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