Results 1 to 3 of 3

Thread: When I start a thread second time my app is freezing

  1. #1
    Join Date
    Jul 2019
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default When I start a thread second time my app is freezing

    Hello! I'm trying to get title of a YouTube video and display it on main window. This operation is blocking my app second time when I press the button from main window so I created a class that represents a QObject and I moved it to another thread. But the same happens, when I start the thread second time my app is not responding.

    My code:
    title.h
    Qt Code:
    1. #ifndef TITLE_H
    2. #define TITLE_H
    3.  
    4. #include <QUrl>
    5. #include <QWidget>
    6. #include <QTextDocument>
    7. #include <QFile>
    8.  
    9. class Title : public QObject
    10. {
    11. Q_OBJECT
    12. public:
    13. Title(QWidget *parent = nullptr);
    14. ~Title();
    15. void searchTile(const QUrl &);
    16. void htmlGet(const QUrl &);
    17.  
    18. signals:
    19. void titleReady(QString);
    20. void True(QString);
    21.  
    22. private:
    23. QFile file = QFile("data.json");
    24. bool find(QJsonArray, QString, const QUrl &);
    25. };
    26.  
    27. #endif // TITLE_H
    To copy to clipboard, switch view to plain text mode 

    title.cpp
    Qt Code:
    1. #include <title.h>
    2. #include <QScopedPointer>
    3. #include <QNetworkAccessManager>
    4. #include <QNetworkReply>
    5. #include <QRegularExpression>
    6. #include <QJsonDocument>
    7. #include <QJsonObject>
    8. #include <QJsonArray>
    9.  
    10. Title::Title(QWidget *parent) : QObject(parent){
    11.  
    12. }
    13.  
    14. Title::~Title(){
    15.  
    16. }
    17.  
    18. void Title::searchTile(const QUrl &url){
    19. if(file.exists() && file.size() != 0){
    20. file.open(QIODevice::ReadOnly | QIODevice::Text); //I have a json file where I save data like title and url of a video
    21. QString val = file.readAll();
    22. file.close();
    23. QJsonDocument d = QJsonDocument::fromJson(val.toUtf8());
    24. QJsonArray list = d.array();
    25. bool checkUrl = this->find(list, "url", url);
    26. if(checkUrl)
    27. emit True("URL is already in download list!"); //this is for showing an error message box
    28. else
    29. this->htmlGet(url);
    30. }
    31. else
    32. this->htmlGet(url);
    33. }
    34.  
    35. void Title::htmlGet(const QUrl &url) {
    36. QScopedPointer<QNetworkAccessManager> manager(new QNetworkAccessManager);
    37. QNetworkReply *response = manager->get(QNetworkRequest(url));
    38. qDebug() << "titluuuu";
    39. connect(response, &QNetworkReply::finished, [response, this]{
    40. response->deleteLater();
    41. response->manager()->deleteLater();
    42. if (response->error() != QNetworkReply::NoError) return;
    43. auto const contentType =
    44. response->header(QNetworkRequest::ContentTypeHeader).toString();
    45. static QRegularExpression re("charset=([!-~]+)");
    46. auto const match = re.match(contentType);
    47. if (!match.hasMatch() || 0 != match.captured(1).compare("utf-8", Qt::CaseInsensitive)) {
    48. qWarning() << "Content charsets other than utf-8 are not implemented yet:" << contentType;
    49. return;
    50. }
    51. auto const html = QString::fromUtf8(response->readAll());
    52. QRegularExpression rg("eow-title.*>");
    53. auto const m = rg.match(html);
    54. auto s = m.captured(0);
    55. QRegularExpression n("title=.*");
    56. auto a = n.match(s);
    57. h.setHtml(a.captured(0).split("\"")[1]);
    58. emit titleReady(QString(QChar(0x200E)) + h.toPlainText());
    59. }) && manager.take();
    60. }
    61.  
    62. bool Title::find(QJsonArray arr, QString key, const QUrl &url){
    63. //this function is for searching if a url is already in the json file
    64. for(const auto obj : arr){
    65. if(obj.toObject().value(key) == url.toString().split("&")[0])
    66. return true;
    67. }
    68. return false;
    69.  
    70. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.h
    Qt Code:
    1. #include <QWidget>
    2. #include <QThread>
    3. #include <QJsonArray>
    4.  
    5. class mainWindow : public QWidget
    6. {
    7. Q_OBJECT
    8.  
    9. signals:
    10. void titleReady(QUrl);
    11.  
    12. private:
    13. int i = 0;
    14. QThread th;
    15. QJsonArray json;
    16. void displayTitle(QString);
    17. void startWork();
    18. void buttonPushed();
    19. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "title.h"
    3.  
    4. mainWindow::mainWindow(QWidget *parent) :
    5. QWidget(parent)
    6. {
    7. title = new Title();
    8. title->moveToThread(&th);
    9. connect(title, &Title::True, this, &mainWindow::errorDialog);
    10. connect(&th, &QThread::finished, title, [this] {qDebug() << "finished"; });
    11. connect(this, &mainWindow::titleReady, title, &Title::searchTile);
    12. connect(&th, &QThread::started, this, &mainWindow::startWork);
    13. connect(title, &Title::titleReady, this, &mainWindow::displayTitle);
    14. }
    15.  
    16. void mainWindow::buttonPushed(){
    17. th.start()
    18. }
    19.  
    20. void mainWindow::startWork(){
    21. qDebug() << "Started";
    22. emit titleReady(QUrl(txt->text()));
    23. }
    24.  
    25. void mainWindow::displayTitle(QString html){
    26. QJsonObject info;
    27.  
    28. //I have a QComboBox modified with a QTreeView
    29. QModelIndex index_type = type_box->currentView()->rootIndex();
    30. QVariant data_type = type_box->currentView()->model()->data(index_type);
    31. info.insert("type", data_type.toString());
    32.  
    33. QModelIndex index_ext = type_box->currentView()->currentIndex();
    34. QVariant data_ext = type_box->currentView()->model()->data(index_ext);
    35. info.insert("ext", data_ext.toString());
    36.  
    37. info.insert("loc", loc_txt->text());
    38. info.insert("title", QJsonValue(html));
    39. info.insert("url", txt->text().split("&")[0]);
    40.  
    41. json.push_back(info);
    42. QJsonDocument doc(json);
    43. file.open(QIODevice::WriteOnly | QIODevice::Text);
    44. QTextStream text(&file);
    45. text << doc.toJson();
    46. file.close();
    47.  
    48. //On main window I have a table widget where I want to display some data in it
    49. QTableWidgetItem *item1 = new QTableWidgetItem(QString("%1(%2)").arg(data_type.toString(), data_ext.toString()));
    50. QTableWidgetItem *item4 = new QTableWidgetItem(QString("Waiting..."));
    51.  
    52. item1->setTextAlignment(Qt::AlignCenter);
    53. item2->setTextAlignment(Qt::AlignCenter);
    54. item3->setTextAlignment(Qt::AlignCenter);
    55. tem4->setTextAlignment(Qt::AlignCenter);
    56.  
    57. table->setItem(i, 0, item);
    58. table->setItem(i, 1, item1);
    59. table->setItem(i, 2, item2);
    60. table->setItem(i, 3, item3);
    61. table->setItem(i, 4, item4);
    62.  
    63. i++;
    64. title->thread()->quit();
    65. }
    To copy to clipboard, switch view to plain text mode 

    How can I solve this? Only the second time I start the thread, the GUI is freezing and I'm wondering why this happens. I also tried displayTitle() like this:
    Qt Code:
    1. void mainWindow:: displayTitle (){
    2. qDebug << "test";
    3. title->thread()->quit();
    4. }
    To copy to clipboard, switch view to plain text mode 
    but it's the same.

    I'm glad if you give me advice for optimization of my code if it's something wrong. Thank you!
    Last edited by INeedADollar; 19th October 2019 at 22:26.

  2. #2
    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: When I start a thread second time my app is freezing

    QTextDocument is a GUI class which should imo not be used inside a thread. I don't see a reason why a thread is needed here at all. Starting a thread a second time is also imo not supported (and also not needed for your task).

  3. #3
    Join Date
    Jul 2019
    Posts
    32
    Thanks
    5
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: When I start a thread second time my app is freezing

    Quote Originally Posted by ChristianEhrlicher View Post
    QTextDocument is a GUI class which should imo not be used inside a thread. I don't see a reason why a thread is needed here at all. Starting a thread a second time is also imo not supported (and also not needed for your task).
    I agree with you but what can I do to make my gui not freezing because I also tried without another thread and when I clicked the button second time after starting the application always my gui freezed?

Similar Threads

  1. Replies: 1
    Last Post: 6th December 2015, 17:12
  2. Start a thread automatic at start
    By ralphot in forum Qt Programming
    Replies: 3
    Last Post: 10th July 2013, 16:54
  3. Thread freezing
    By porterneon in forum Qt Programming
    Replies: 0
    Last Post: 3rd November 2011, 11:38
  4. Replies: 10
    Last Post: 10th March 2008, 13:28
  5. Thread freezing GUI
    By TheGrimace in forum Qt Programming
    Replies: 26
    Last Post: 27th June 2007, 16:57

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.