Results 1 to 4 of 4

Thread: Read with one thread, send datas to another thread

  1. #1
    Join Date
    Jan 2021
    Posts
    1
    Qt products
    Qt4 Qt5
    Platforms
    Windows

    Default Read with one thread, send datas to another thread

    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?

    mythread.cpp


    Qt Code:
    1. #include "mythread.h"
    2. #include <QtCore>
    3. #include <QDebug>
    4. #include <QFile>
    5. MyThread::MyThread()
    6. {
    7.  
    8. }
    9.  
    10. void MyThread::run() //Reading file from txt with thread1
    11. {
    12. QFile file("C:/Users/ilknu/Documents/untitled1/deneme.txt");
    13.  
    14. if (file.open(QIODevice::ReadOnly))
    15. {
    16. QTextStream in (&file);
    17. while (!in.atEnd())
    18. {
    19.  
    20. QString line = in.readLine();
    21. QStringList list = line.split(QLatin1Char(' '), Qt::SkipEmptyParts);
    22. for(const QString &entry : list)
    23. {
    24. double num = entry.toDouble();
    25. qDebug()<<num;
    26. queue.enqueue(num);
    27.  
    28. } // for
    29. } // while
    30. } // if
    31.  
    32. file.close();
    33. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read with one thread, send datas to another thread

    There is no reason to use threads for this. Open both files, read from one file, compute your sum, and write it to the second file. When the reading file is at the end, close both files.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Read with one thread, send datas to another thread

    Quote Originally Posted by d_stranz View Post
    There is no reason to use threads for this.
    We try to explain him this for a while - now he tries it in another forum

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Read with one thread, send datas to another thread

    Maybe his toolbox is filled only with hammers, so every problem becomes a nail.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. [QStandardItemModel] read from a different thread
    By mentalmushroom in forum Qt Programming
    Replies: 4
    Last Post: 10th November 2011, 10:33
  2. Thread started from main does send signal back.
    By edxxgardo in forum Qt Programming
    Replies: 3
    Last Post: 20th July 2011, 15:36
  3. Replies: 7
    Last Post: 27th January 2011, 09:48
  4. Can you send a signal to a thread?
    By Dumbledore in forum Qt Programming
    Replies: 1
    Last Post: 9th November 2007, 21:31
  5. Thread+send event
    By Fastman in forum Qt Programming
    Replies: 20
    Last Post: 1st August 2007, 15:09

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.