Results 1 to 2 of 2

Thread: unable to update label from another thread

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Question unable to update label from another thread

    Iam new to Qt programming.Qt 4.7.3 is using for the development.
    We are developing a program to read from the multiple file(initially started with two) and display in to the Gui.File reading is doing from one thread and the read value is passing to the label in Gui thread.
    Here the code.
    fileread.cpp

    Qt Code:
    1. #include "fileread.h"
    2. #include "ui_fileread.h"
    3. #include "filethread.h"
    4. #include<QThread>
    5. #include <QtGui>
    6. #include<QLabel>
    7. fileread::fileread(QWidget *parent) :
    8. QWidget(parent),
    9. ui(new Ui::fileread)
    10. {
    11. ui->setupUi(this);
    12.  
    13. }
    14.  
    15. fileread::~fileread()
    16. {
    17. delete ui;
    18. }
    19.  
    20.  
    21. void fileread::on_pushButton_clicked()
    22. {
    23.  
    24. QString fname1="E:/QT/test1.txt";
    25. QString fname2="E:/QT/test3.txt";
    26.  
    27.  
    28. filethread fthread1(fname1),fthread2(fname2);
    29.  
    30. QObject::connect(&fthread1, SIGNAL(setlabel(QString)),ui->label_2, SLOT(setText(QString)), Qt::QueuedConnection);
    31. QObject::connect(&fthread2, SIGNAL(setlabel(QString)),ui->label_3 , SLOT(setText(QString)), Qt::QueuedConnection);
    32.  
    33. fthread1.start();
    34. fthread2.start();
    35.  
    36. fthread1.wait();
    37. fthread2.wait();
    38.  
    39. }
    40.  
    41.  
    42. filethread.cpp
    43.  
    44. filethread::filethread(QString text) : QThread()
    45. {
    46.  
    47. fname=text;
    48. }
    49.  
    50.  
    51. QMutex mutex;
    52. void filethread::run()
    53. {
    54. QFile fp(fname);
    55. if(fp.open(QIODevice::ReadOnly))
    56. {
    57. QDataStream in(&fp);
    58. }
    59. QTextStream in(&fp);
    60.  
    61. QString line = in.readLine();
    62.  
    63. while (!line.isNull())
    64. {
    65.  
    66. mutex.lock();
    67. emit setlabel(line);
    68. sleep(1);
    69. qDebug()<<"Content from the file:"<<line;
    70. line = in.readLine();
    71. mutex.unlock();
    72.  
    73. }
    74. }
    To copy to clipboard, switch view to plain text mode 

    Here what problem Iam facing is only last line from the file is displaying in the label.Other text from the file is not comming in the label.All line from the file should come in the label.File is reading properly,that I understood from qDebug function.I think Gui thread is get blocked because of that it is not comming for all line from file.
    Iam in this problem for about three weeks...
    Please help me..
    Last edited by wysota; 5th October 2012 at 00:01. Reason: missing [code] tags

Similar Threads

  1. Replies: 15
    Last Post: 31st January 2020, 18:25
  2. Qwt: Unable to draw the label from qwtplotmarker?
    By sonulohani in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2012, 13:55
  3. Unable to get my spinbox to display (all that shows is the associated label)
    By bschumacher in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 17th May 2012, 02:32
  4. Replies: 2
    Last Post: 8th April 2010, 16:16
  5. Unable to update QTableWidget which is on Different QWidget from MainWindow
    By sagar.mangalwedhekar in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2010, 07:07

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.