Results 1 to 20 of 27

Thread: QThread run() makes MainWindow get stuck

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    45
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QThread run() makes MainWindow get stuck

    hi,
    i have an application out of which i want to start a thread wich reads a big textfile into a QString but after having started the thread my mainwindow out of which the thread got started gets stuck (means its stuck during thread execution and i dont know why):

    here is some code parts:

    gui mainwindow (created with designer):
    Qt Code:
    1. // //////////////////////////////////////////
    2. class MainWindow : public QMainWindow
    3. // //////////////////////////////////////////
    4. {
    5. Q_OBJECT
    6.  
    7. // //////////////////////
    8. public:
    9. // //////////////////////
    10. explicit MainWindow(QWidget *parent = 0);
    11. ~MainWindow();
    12.  
    13. public slots:
    14. void AppendTextEdit(QString);
    15.  
    16. // //////////////////////
    17. private:
    18. // //////////////////////
    19. Ui::MainWindow *ui;
    20. FileLoader *fileloader;
    21.  
    22. // //////////////////////
    23. private slots:
    24. // //////////////////////
    25. void on_pushButton_2_clicked();
    26. void on_pushButton_clicked();
    27. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. // //////////////////////////////////
    2. MainWindow::MainWindow(QWidget *parent) :
    3. QMainWindow(parent),
    4. ui(new Ui::MainWindow)
    5. // //////////////////////////////////
    6. {
    7. ui->setupUi(this);
    8. fileloader = new FileLoader(this);
    9.  
    10. QObject::connect(fileloader, SIGNAL(HaveNextChunk(QString)), this, SLOT(AppendTextEdit(QString)));
    11.  
    12. ui->textEdit->setCursorWidth(4);
    13. ui->textEdit->setLineWrapMode(QTextEdit::NoWrap);
    14.  
    15. }
    16.  
    17. // //////////////////////////////////
    18. MainWindow::~MainWindow()
    19. // //////////////////////////////////
    20. {
    21. delete ui;
    22. delete fileloader;
    23. }
    24.  
    25. // //////////////////////////////////
    26. void MainWindow::AppendTextEdit(QString str)
    27. // //////////////////////////////////
    28. {
    29. QTextCursor tc = ui->textEdit->textCursor();
    30. tc.movePosition(QTextCursor::End);
    31. ui->textEdit->setTextCursor(tc);
    32. ui->textEdit->insertPlainText(str);
    33. }
    34.  
    35.  
    36. // //////////////////////////////////
    37. void MainWindow::on_pushButton_clicked()
    38. // //////////////////////////////////
    39. {
    40. fileloader->start();
    41. }
    42.  
    43. // //////////////////////////////////
    44. void MainWindow::on_pushButton_2_clicked()
    45. // //////////////////////////////////
    46. {
    47. fileloader->quit();
    48. }
    To copy to clipboard, switch view to plain text mode 

    now the thread-class
    Qt Code:
    1. #include <QThread>
    2.  
    3.  
    4. // //////////////////////////////////////////
    5. class FileLoader : public QThread
    6. // //////////////////////////////////////////
    7. {
    8. Q_OBJECT
    9.  
    10. // //////////////////////////////
    11. public:
    12. // //////////////////////////////
    13. FileLoader(QObject *parent=NULL);
    14. ~FileLoader();
    15. virtual void run();
    16.  
    17. // //////////////////////////////
    18. signals:
    19. // // //////////////////////////////
    20. void HaveNextChunk(QString);
    21.  
    22. // //////////////////////////////
    23. private:
    24. // //////////////////////////////
    25. CLogFile logfile;
    26. QString chunk;
    27.  
    28. };
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. //// //////////////////////////////////
    2. FileLoader::FileLoader(QObject *parent):QThread(parent)
    3. // //////////////////////////////////
    4. {
    5. }
    6.  
    7. //// //////////////////////////////////
    8. FileLoader::~FileLoader()
    9. // //////////////////////////////////
    10. {
    11. }
    12.  
    13.  
    14. // //////////////////////////////////
    15. void FileLoader::run()
    16. // //////////////////////////////////
    17. {
    18. logfile.Open("big.log");
    19. if( !logfile.IsOpen() )
    20. return;
    21.  
    22. // ulong filesize = logfile.GetFileSize();
    23. // ulong bytesRead = 0;
    24.  
    25. chunk.clear();
    26. string token;
    27. while( logfile.GetNextLines(token, 10000) )
    28. {
    29. chunk += token.c_str();
    30. }
    31. emit HaveNextChunk(chunk);
    32. logfile.Close();
    33. }
    To copy to clipboard, switch view to plain text mode 

    u can ignore the CLogFile class stuff (it just reads bunch of lines out of textfile and works correct).

    the problem is whenever the thread is started out of my mainwindow the whole mainwindow gets stuck until thread has finished work .. why !?!?

    thnx


    EDIT: ok it seems the insertPlainText(str) call is the bottle-neck --> i guess the string that has to be put into the textedit is quite big (although the file being read is only half-MB of size)

    any ideas as alternative approaches to reading file (maybe chunkwise) via thread and updating the textedit as soon as one chunk is read !?!?
    Last edited by kerim; 24th March 2011 at 13:17.

Similar Threads

  1. Changing MainWindow UI from another QThread.
    By EdgeLuxe in forum Newbie
    Replies: 1
    Last Post: 4th September 2010, 15:35
  2. QPushButton gets stuck
    By mhoover in forum Qt Programming
    Replies: 1
    Last Post: 22nd May 2010, 23:22
  3. Control MainWindow from QThread
    By lixo1 in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2009, 10:33
  4. Help plz! I’m stuck with these delegates
    By codeaddict in forum Qt Programming
    Replies: 7
    Last Post: 19th August 2008, 21:33
  5. comboboxes get stuck
    By illuzioner in forum Qt Programming
    Replies: 2
    Last Post: 30th March 2006, 01:03

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.