Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Update QlineEdit
Forum Updated to NodeBB v4.3 + New Features

Update QlineEdit

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lalyperu
    wrote on last edited by
    #1

    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();
    }@

    1 Reply Last reply
    0
    • F Offline
      F Offline
      frankiefrank
      wrote on last edited by
      #2

      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.

      "Roads? Where we're going, we don't need roads."

      1 Reply Last reply
      0
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        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.

        (Z(:^

        1 Reply Last reply
        0
        • L Offline
          L Offline
          lalyperu
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • sierdzioS Offline
            sierdzioS Offline
            sierdzio
            Moderators
            wrote on last edited by
            #5

            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.

            (Z(:^

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved