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. Error: working with QThreads
QtWS25 Last Chance

Error: working with QThreads

Scheduled Pinned Locked Moved General and Desktop
qthreadserrorparent
4 Posts 2 Posters 1.7k Views
  • 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.
  • S Offline
    S Offline
    SujaMM
    wrote on last edited by
    #1

    I'm getting this error while ussing QThreads:

    QObject: Cannot create children for a parent that is in a different thread.
    (Parent is QNetworkAccessManager(0x15694420), parent's thread is QThread(0x15670d00), current thread is CaptureThread(0x156f0ea8)

    I'm using an Api to save and get data from the Data Base, but when I try to save some information in the Thread I got that Error.

    Can some help me out wiht this problem.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by mcosta
      #2

      Hi and welcome to devnet,

      as the compiler says you're trying to create an Object passing a parent belonging to a different thread (usually is this).

      Can you show your code??

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • S Offline
        S Offline
        SujaMM
        wrote on last edited by
        #3

        Ok, this it's the header of the QThread.

        #ifndef CAPTURETHREAD_H
        #define CAPTURETHREAD_H
        
        #include <QThread>
        #include <QMutex>
        #include <QWaitCondition>
        #include <QJsonObject>
        #include "api.h"
        class CaptureThread : public QThread
        {
            Q_OBJECT
        public:
               explicit CaptureThread(Api *capi, QObject *parent = 0);
               void run();
               void pause();
               void resume();
               bool flagpause;
               bool Stop;
               int countTime;
               QMutex sync;
               QWaitCondition pauseCond;
               QString url;
               Api* api;
               QJsonObject jsonUser;
               QJsonObject jsonSession;
               int idTask;
        signals:
               void counTimeChange(int countTime);
        };
        
        #endif // CAPTURETHREAD_H
        

        Api it's the class with the methods to access the database through an api.

        The implementation

        #include "capturethread.h"
        #include <QDesktopWidget>
        #include <QApplication>
        #include <QDir>
        #include <QDate>
        
        CaptureThread::CaptureThread(Api *capi, QObject *parent):
            QThread(parent)
        {
            this->countTime = 0;
            this->api = capi;
        }
        
        void CaptureThread::run(){
        
            while (true) {
                   api->createLog(api->jsonUser["id"].toInt(),api->jsonSession["id"].toInt());
                   this->msleep(60000);
                   QDesktopWidget* dw = QApplication::desktop();
                   QPixmap pixmap = QPixmap::grabWindow( dw->winId(), 0, 0, dw->width(), dw->height() );
        
                   QDir mDir(QDir::homePath());
                   QString format = "jpg";
                   QString filename = "/screenshot_" + QString::number(QDate::currentDate().year())+ "_"
                           +QString::number(QDate::currentDate().month())+ "_"
                           +QString::number(QDate::currentDate().day())+ "_"
                           +QString::number(QTime::currentTime().hour())+ "_"
                           +QString::number(QTime::currentTime().minute())+ "_"
                           +QString::number(QTime::currentTime().second());
                   QString mPath = QDir::homePath() + "/user";
                   if (!mDir.exists(mPath))
                        mDir.mkpath(mPath);
                   pixmap.save(mPath+filename+".jpg", format.toLatin1().constData());
                   this->url = mPath+filename+".jpg";
                   api->closeLog(api->jsonUser["id"].toInt(),api->jsonLog["id"].toInt(), url);
                   this->countTime ++;
                   emit counTimeChange(this->countTime);
                   sync.lock();
                   if(flagpause)
                         pauseCond.wait(&sync);
                   sync.unlock();
               }
        }
        
        void CaptureThread::pause() {
                sync.lock();
                flagpause = true;
                sync.unlock();
        }
        
        void CaptureThread::resume() {
                sync.lock();
                flagpause = false;
                sync.unlock();
                pauseCond.wakeAll();
        }
        

        The main thread, the class task

         capThread = new CaptureThread(currentapi,this);
         . . . . 
         void Task::on_btnStart_clicked()
         {
             capThread->flagpause = false;
             capThread->resume();
             capThread->start();
         }
        
        1 Reply Last reply
        0
        • S Offline
          S Offline
          SujaMM
          wrote on last edited by
          #4

          I fix this problem with signals and slots, but I firts create what a call a log, and after one minute a close that log.

          I'm getting this error while closing the log

          QEventLoop::exec: instance 0x8d4418 has already called exec()
          
          1 Reply Last reply
          0

          • Login

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