-1

I am writing to ask for advice on how best to implement my code using QT library. I have a class called Action class that every one second retrieve 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();
}
p.i.g.
  • 2,815
  • 2
  • 24
  • 41

1 Answers1

4

Instead using gettimeofday use QTime::currentTime then convert it to string (chose the format) and emit result. This signal should be connected to slot QLineEdit::setText.

Using thread is completely obsolete here.

Marek R
  • 32,568
  • 6
  • 55
  • 140
  • I'm using gettimeofday because returns seconds and microsecond which I need...The code I post is only a little part of the entire code that for many reason will be implemented with this architecture so I have a thread. So i post the code to know which is the better way to update QLineEdit. – user3276825 Mar 25 '14 at 16:06
  • 1
    having in line edit (UI) with such time precision is pointless. If you take into account it is editing field and it is updated live then it looks like very bad UI design. – Marek R Mar 25 '14 at 16:24