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. Qt event emit inside thread not working properly
Forum Updated to NodeBB v4.3 + New Features

Qt event emit inside thread not working properly

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 2 Posters 537 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
    sujith D
    wrote on last edited by
    #1
    void loaddepth::run(){
       
     
        QString file_name=this->filename;
        int day=this->day_no;
        int depth=this->deepth_no;
     
        QFile *file=new QFile(file_name);
        QTextStream in(file);
        file->open(QIODevice::ReadOnly | QIODevice::Text);
        const auto size = file->size();
    
         QStringList strList;
         bool flag=false;
    
    
            QStringList list;
            int line=0;
            int day_count=0;
    
            while(!in.atEnd()&&day_count<=day)
            {
                QString str=in.readLine().simplified();
                QStringList list=str.split(' ');
    
                if(list.count()==3){
    
                    if(flag){
                        **emit readStatusChanged(this->file_no,list[0],list[1],list[2].toDouble());**
    
                    }
     
    
     
                } else if(list.count()==1){
    
                    if(list[0].toInt()!=depth&&flag==true){
                        qDebug()<<"b4 senting";
               }
    
                    if(list[0].toInt()==depth){
    
                            flag=true;
    
                    }
    
                }
    
       line++;
            }
    
    
    }
     ******************************
    connect(fileData1,SIGNAL(readStatusChanged(int,QString,QString,double)),this,SLOT(onReadStatusChanged(int,QString,QString,double)));
    
    **event emits is not triggering inside the thread loop.**
    
    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Did you check if the connect is successful and if you really emit the signal? Please show us where you connect the signal/slot connection.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      S 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        Did you check if the connect is successful and if you really emit the signal? Please show us where you connect the signal/slot connection.

        S Offline
        S Offline
        sujith D
        wrote on last edited by sujith D
        #3

        @Christian-Ehrlicher ya emit works only outside while loop. it doesn't work inside the loop.

        connect(fileData1,SIGNAL(readStatusChanged(int,QString,QString,double)),this,SLOT(onReadStatusChanged(int,QString,QString,double)));

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @sujith-D said in Qt event emit inside thread not working properly:

          ya emit works only outside while loop. it doesn't work inside the loop.

          Again: how did you check that the signal is emitted? Did you added a breakpoint? Or a debug output?

          And you still did not check the return value of connect() and did not show us the code around the connect on how you instantiate the class etc.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          S 1 Reply Last reply
          0
          • Christian EhrlicherC Christian Ehrlicher

            @sujith-D said in Qt event emit inside thread not working properly:

            ya emit works only outside while loop. it doesn't work inside the loop.

            Again: how did you check that the signal is emitted? Did you added a breakpoint? Or a debug output?

            And you still did not check the return value of connect() and did not show us the code around the connect on how you instantiate the class etc.

            S Offline
            S Offline
            sujith D
            wrote on last edited by
            #5

            @Christian-Ehrlicher

            I do qDebug(), and its output.

            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @sujith-D said in Qt event emit inside thread not working properly:

              I do qDebug(), and its output.

              Again: please show us more code, best a minimal example so we can try to reproduce it.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              S 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @sujith-D said in Qt event emit inside thread not working properly:

                I do qDebug(), and its output.

                Again: please show us more code, best a minimal example so we can try to reproduce it.

                S Offline
                S Offline
                sujith D
                wrote on last edited by sujith D
                #7

                @Christian-Ehrlicher
                Calling thread from module2. Declared signals and slots in corresponding header files.
                I don't know why the same event not working inside the loop during the file read.

                Module2::Module2(QWidget *parent) :
                    QWidget(parent),
                    rect(0),
                    ui(new Ui::Module2),
                    scaleFactor(1),
                    mapDim(1500)
                {
                    fileData1=new loaddepth(this);
                 fileData1->filename=QCoreApplication::applicationDirPath()+"/../project3DUnderWater/0.txt";
                fileData1->deepth_no=1;
                connect(fileData1,SIGNAL(readStatusChanged(int,QString,QString,double)),this,SLOT(onReadStatusChanged(int,QString,QString,double)));
                    connect(fileData1,SIGNAL(ReadFileCompleted(int,QJsonObject)),this,SLOT(onReadFileCompleted(int,QJsonObject)));
                fileData1->start();
                
                }
                
                void Module2::onReadStatusChanged(int file_no,QString y,QString x,double z ){
                    qDebug()<<"file_no"<<file_no;
                
                }
                
                void Module2::onReadFileCompleted(int file_no,QJsonObject json){
                
                 qDebug() <<json;
                
                }
                

                module2 header file

                #include<QProgressDialog>
                namespace Ui {
                class Module2;
                }
                
                class Module2 : public QWidget
                {
                    Q_OBJECT
                
                public:
                    explicit Module2(QWidget *parent = nullptr);
                    ~Module2();
                 
                    loaddepth *fileData1;
                
                private slots:
                    void onReadStatusChanged(int,QString,QString,double);
                    void onReadFileCompleted(int,QJsonObject);
                };
                

                Thread header file

                #include<QtCore>
                class loaddepth:public QThread
                {
                   Q_OBJECT
                public:
                    loaddepth(QObject *parent=0);
                    void run() override;
                    bool stop;
                   QString  filename; int day_no;
                   int deepth_no;
                      QJsonObject obj;
                int file_no;
                
                signals:
                
                        void readStatusChanged(int,QString,QString,double);
                        void ReadFileCompleted(int,QJsonObject);
                
                 
                };
                
                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  This is working fine for me:

                  #include <QtCore>
                  
                  class MyThread : public QThread
                  {
                    Q_OBJECT
                  public:
                    using QThread::QThread;
                  
                    void run() override
                    {
                      while (true) {
                        emit emitSignal();
                        QThread::sleep(2);
                      }
                    }
                  
                  Q_SIGNALS:
                    void emitSignal();
                  };
                  
                  class MyObject : public QObject
                  {
                    Q_OBJECT
                  public:
                    MyObject(QObject *parent = nullptr)
                      : QObject(parent)
                    {
                      MyThread *mt = new MyThread(this);
                      connect(mt, &MyThread::emitSignal, this, &MyObject::mySlot);
                      mt->start();
                    }
                  private Q_SLOTS:
                    void mySlot() { qDebug() << "Got signal!"; }
                  };
                  
                  int main(int argc, char *argv[])
                  {
                      QCoreApplication app(argc, argv);
                  
                      MyObject obj;
                      return app.exec();
                  }
                  
                  #include "main.moc"
                  

                  Btw: no need to create (and leak) a QFile object on the heap.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  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