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. Working with 2 Threads
Forum Updated to NodeBB v4.3 + New Features

Working with 2 Threads

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 412 Views 2 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.
  • D Offline
    D Offline
    deleted286
    wrote on last edited by
    #1

    Hi everyone. Im working with threads. In the following code, i read datas from txt and run it on my thread. I want to use another thread for writing the datas another txt file. How can i do it?

    #include "mythread.h"
    #include <QtCore>
    #include <QDebug>
    #include <QFile>
    MyThread::MyThread()
    {
    
    }
    
    void MyThread::run()  //Reading file from txt with thread1
    {
        QFile file("C:/Users/ilknu/Documents/untitled1/deneme.txt");
    
        if (file.open(QIODevice::ReadOnly))
        {
            QTextStream in (&file);
            while (!in.atEnd())
            {
    
                QString line = in.readLine();
                QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
                for(const QString &entry : list)
                {
                    double num = entry.toDouble();
                    qDebug()<<num;
                    queue.enqueue(num);
    
                } // for
            } // while
        } // if
    
        file.close();
    }
    
    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Based on your other questions with regard to your application, please do not start with threading now. It's a really complex and involved topic that you do not need most of the time.

      Stay single threaded until you have more experience with C++ and also with the asynchronous nature of Qt.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      4
      • SGaistS SGaist

        Hi,

        Based on your other questions with regard to your application, please do not start with threading now. It's a really complex and involved topic that you do not need most of the time.

        Stay single threaded until you have more experience with C++ and also with the asynchronous nature of Qt.

        D Offline
        D Offline
        deleted286
        wrote on last edited by
        #3

        @SGaist It sounds nice and make sense but i must work in any case

        SGaistS 1 Reply Last reply
        0
        • D deleted286

          @SGaist It sounds nice and make sense but i must work in any case

          SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @suslucoder said in Working with 2 Threads:

          It sounds nice and make sense but i must work in any case

          What do you mean by any case ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Jonas KvingeJ Offline
            Jonas KvingeJ Offline
            Jonas Kvinge
            wrote on last edited by
            #5

            There is a helpful video on youtube for learning QThread.
            https://www.youtube.com/watch?v=JaGqGhRW5Ks&t=299s&ab_channel=VoidRealms
            I usually do not do it like that though.
            I create an object and the thread as separate objects, then I use QObject::moveToThread, then QThread::start, and connect signal slots to the object.

            Alternatively you can use QtConcurrent::run which is easier to use.
            https://doc.qt.io/qt-5/qtconcurrent.html
            Call the function in a QtConcurrent::run, you can connect signal/slots to the returned QFuture to a function that will execute once finished.

            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