ASSERT Widget must be created in a GUI Thread



  • 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?


  • Moderators

    @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.



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


  • Qt Champions 2016

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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.