Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. ASSERT Widget must be created in a GUI Thread
Forum Updated to NodeBB v4.3 + New Features

ASSERT Widget must be created in a GUI Thread

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.9k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Fuel
    wrote on last edited by
    #1

    I have a Problem to seperate a Copy Process in a different Thread. For the Copy Process i have a QProgressDialog, but in the new Thread i cant use the QProgressDialog, the Error says. But especially for that im using movetothread.

    QThread *thread = new QThread();
        DiaryProgress *progress = new DiaryProgress(this);
    
        progress->setAttachments(mAttachments);
        progress->moveToThread(thread);
    
        connect(progress, SIGNAL(errorString(QString)), this, SLOT(errorString(QString)));
        connect(thread, SIGNAL(started()), progress, SLOT(showDialog()));
        connect(progress, SIGNAL(finished()), thread, SLOT(quit()));
        connect(progress, SIGNAL(finished()), progress, SLOT(deleteLater()));
        connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    
        thread->start();
    

    The Error happens, when im using the QProgressDialog in the showDialog() SLOT.

    void DiaryProgress::showDialog()
    {
        QString mAppDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
        mAppDataLocation += QString(QDir::separator()) + "Attachments";
        QDir dir(mAppDataLocation);
        if (!dir.exists())
            dir.mkpath(mAppDataLocation);
    
        QProgressDialog progress("Saving...", "", 0, mAttachments->count());
        progress.setWindowModality(Qt::WindowModal);
        progress.setCancelButton(0);
    
        for (int i = 0; i <= mAttachments->count(); i++)
        {
            if (i < mAttachments->count())
            {
                QString oldPath = mAttachments->at(i).absoluteFilePath();
                QString newPath = mAppDataLocation + QString(QDir::separator()) + mAttachments->at(i).fileName();
    
                while (QFile(newPath).exists())
                {
                    QString suffix = "." + QFileInfo(newPath).suffix();
                    newPath = QFileInfo(newPath).absolutePath() + QString(QDir::separator()) +
                            mAttachments->at(i).baseName() + QString::number(i) + suffix;
                }
    
                QFile(oldPath).copy(oldPath, newPath);
                //QApplication::processEvents();
            }
            else
            {
    //            Database *db;
    //            db = Database::instance();
    
    //            int id = db->insertDiary(ui->diarytextwidget->getText(), ui->wStarRating->getRating(), QDate(ui->lCurrentDate->text()));
    
    //            for (auto item : mAttachments)
    //            {
    //                db->insertDiaryAttachments(id, item.basename(), item.absoluteFilePath());
    //            }
            }
    
            progress.setValue(i);
            QThread::sleep(1);
        }
    
        progress.setValue(mAttachments->count());
        emit finished();
    }
    

    Anyone has an idea, how i can use a QProgressDialog in another Thread?

    raven-worxR 1 Reply Last reply
    0
    • F Fuel

      I have a Problem to seperate a Copy Process in a different Thread. For the Copy Process i have a QProgressDialog, but in the new Thread i cant use the QProgressDialog, the Error says. But especially for that im using movetothread.

      QThread *thread = new QThread();
          DiaryProgress *progress = new DiaryProgress(this);
      
          progress->setAttachments(mAttachments);
          progress->moveToThread(thread);
      
          connect(progress, SIGNAL(errorString(QString)), this, SLOT(errorString(QString)));
          connect(thread, SIGNAL(started()), progress, SLOT(showDialog()));
          connect(progress, SIGNAL(finished()), thread, SLOT(quit()));
          connect(progress, SIGNAL(finished()), progress, SLOT(deleteLater()));
          connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
      
          thread->start();
      

      The Error happens, when im using the QProgressDialog in the showDialog() SLOT.

      void DiaryProgress::showDialog()
      {
          QString mAppDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
          mAppDataLocation += QString(QDir::separator()) + "Attachments";
          QDir dir(mAppDataLocation);
          if (!dir.exists())
              dir.mkpath(mAppDataLocation);
      
          QProgressDialog progress("Saving...", "", 0, mAttachments->count());
          progress.setWindowModality(Qt::WindowModal);
          progress.setCancelButton(0);
      
          for (int i = 0; i <= mAttachments->count(); i++)
          {
              if (i < mAttachments->count())
              {
                  QString oldPath = mAttachments->at(i).absoluteFilePath();
                  QString newPath = mAppDataLocation + QString(QDir::separator()) + mAttachments->at(i).fileName();
      
                  while (QFile(newPath).exists())
                  {
                      QString suffix = "." + QFileInfo(newPath).suffix();
                      newPath = QFileInfo(newPath).absolutePath() + QString(QDir::separator()) +
                              mAttachments->at(i).baseName() + QString::number(i) + suffix;
                  }
      
                  QFile(oldPath).copy(oldPath, newPath);
                  //QApplication::processEvents();
              }
              else
              {
      //            Database *db;
      //            db = Database::instance();
      
      //            int id = db->insertDiary(ui->diarytextwidget->getText(), ui->wStarRating->getRating(), QDate(ui->lCurrentDate->text()));
      
      //            for (auto item : mAttachments)
      //            {
      //                db->insertDiaryAttachments(id, item.basename(), item.absoluteFilePath());
      //            }
              }
      
              progress.setValue(i);
              QThread::sleep(1);
          }
      
          progress.setValue(mAttachments->count());
          emit finished();
      }
      

      Anyone has an idea, how i can use a QProgressDialog in another Thread?

      raven-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on last edited by
      #2

      @Fuel said in ASSERT Widget must be created in a GUI Thread:

      Anyone has an idea, how i can use a QProgressDialog in another Thread?

      as the error says you can't. Rather redesign your code that the custom thread does the work and simply sends the progress via signals (queued connection) to the widget/dialog in the gui/main thread.

      --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
      If you have a question please use the forum so others can benefit from the solution in the future

      1 Reply Last reply
      4
      • F Offline
        F Offline
        Fuel
        wrote on last edited by
        #3

        So its in no way possible? i found another Solution. But im sad that it doesnt work that way.

        mrjjM 1 Reply Last reply
        0
        • F Fuel

          So its in no way possible? i found another Solution. But im sad that it doesnt work that way.

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by mrjj
          #4

          @Fuel
          Hi
          It is not possible to have any QWidget in any
          other thread than then the main thread. ( often called GUI thread)

          1 Reply Last reply
          1

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved