Results 1 to 2 of 2

Thread: unable to update label from another thread

  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 01:01. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2011
    Location
    Hyderabad, India
    Posts
    1,882
    Thanks
    3
    Thanked 452 Times in 435 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    15

    Default Re: unable to update label from another thread

    You are emiting setlabel() signal for every line, and it is connected to setText() which will replace the complete label text (in your case the previous line will be erased). One solution would be implement a custom slot where in you read the current text and append with new line and then setText() on the label. One other option you can use QPlainTextEdit (instead of QLabel) and connect to appendPlainText() slot
    When you know how to do it then you may do it wrong.
    When you don't know how to do it then it is not that you may do it wrong but you may not do it right.

Similar Threads

  1. Replies: 15
    Last Post: 31st January 2020, 19:25
  2. Qwt: Unable to draw the label from qwtplotmarker?
    By sonulohani in forum Qt Programming
    Replies: 1
    Last Post: 1st June 2012, 14: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, 03:32
  4. Replies: 2
    Last Post: 8th April 2010, 17: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, 08: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.