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. Emit Problems
QtWS25 Last Chance

Emit Problems

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 828 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.
  • D Offline
    D Offline
    deleted61
    wrote on last edited by deleted61
    #1

    Hey, I was testing Qt out and was trying to do multi-threading with a simple test application to see how it would work. Well, I fast ran into the problem that either the Reader thread I created won't emit or the main application doesn't receive the signal. I've tried Googling my problem but didn't find anything that helped and I already spent a day trying to get this to work, so if anyone knows what I'm doing wrong I would appreciate the insight...

    Qt Version: Qt 5.11.1 MSVC2017 64bit
    Source: Gist
    MyApp.cpp

    #include "MyApp.h"
    
    MyApp::MyApp(QWidget *parent)
    	: QMainWindow(parent)
    {
    	ui.setupUi(this);
    
    	connect(&reader, SIGNAL(Done()), this, SLOT(done()));
        
    	AllocConsole();
    	freopen("CONOUT$", "w", stdout);
    
    	reader.start_reader();
    }
    
    void MyApp::done()
    {
    	std::cout << "Reader done!" << std::endl;
    }
    

    MyApp.h

    #pragma once
    
    #include <iostream>
    
    #include <QtWidgets/QMainWindow>
    #include "ui_MyApp.h"
    
    #include "Reader.h"
    
    class MyApp : public QMainWindow
    {
    	Q_OBJECT
    
    
    public:
    	MyApp(QWidget *parent = 0);
    
    public slots:
    	void done();
    
    private:
    	Ui::MyAppClass ui;
    	Reader reader;
    };
    

    Reader.cpp

    #include "Reader.h"
    
    Reader::Reader(QObject* parent)
    {
    	abort = false;
    	restart = false;
    }
    
    Reader::~Reader()
    {
    	mutex.lock();
    	abort = true;
    	condition.wakeOne();
    	mutex.unlock();
    
    	wait();
    }
    
    void Reader::start_reader()
    {
    	QMutexLocker locker(&mutex);
    
    	if (isRunning())
    	{
    		start();
    	}
    	else
    	{
    		restart = true;
    		condition.wakeOne();
    	}
    
    }
    
    void Reader::run()
    {
      emit Done();
    }
    

    Reader.h

    #pragma once
    
    #include <iostream>
    
    #include <QThread>
    #include <QMutex>
    #include <QWaitCondition>
    
    class Reader : public QThread
    {
    	Q_OBJECT
    
    public:
    	Reader(QObject *parent = 0);
    	~Reader();
    
    	void start_reader();
    
    signals:
    	void Done();
    
    protected:
    	void run() override;
    
    private:
    	QMutex mutex;
    	QWaitCondition condition;
    	bool abort, restart;
    };
    

    PS: Sorry if this isn't the correct forum...

    K kshegunovK 2 Replies Last reply
    0
    • D deleted61

      Hey, I was testing Qt out and was trying to do multi-threading with a simple test application to see how it would work. Well, I fast ran into the problem that either the Reader thread I created won't emit or the main application doesn't receive the signal. I've tried Googling my problem but didn't find anything that helped and I already spent a day trying to get this to work, so if anyone knows what I'm doing wrong I would appreciate the insight...

      Qt Version: Qt 5.11.1 MSVC2017 64bit
      Source: Gist
      MyApp.cpp

      #include "MyApp.h"
      
      MyApp::MyApp(QWidget *parent)
      	: QMainWindow(parent)
      {
      	ui.setupUi(this);
      
      	connect(&reader, SIGNAL(Done()), this, SLOT(done()));
          
      	AllocConsole();
      	freopen("CONOUT$", "w", stdout);
      
      	reader.start_reader();
      }
      
      void MyApp::done()
      {
      	std::cout << "Reader done!" << std::endl;
      }
      

      MyApp.h

      #pragma once
      
      #include <iostream>
      
      #include <QtWidgets/QMainWindow>
      #include "ui_MyApp.h"
      
      #include "Reader.h"
      
      class MyApp : public QMainWindow
      {
      	Q_OBJECT
      
      
      public:
      	MyApp(QWidget *parent = 0);
      
      public slots:
      	void done();
      
      private:
      	Ui::MyAppClass ui;
      	Reader reader;
      };
      

      Reader.cpp

      #include "Reader.h"
      
      Reader::Reader(QObject* parent)
      {
      	abort = false;
      	restart = false;
      }
      
      Reader::~Reader()
      {
      	mutex.lock();
      	abort = true;
      	condition.wakeOne();
      	mutex.unlock();
      
      	wait();
      }
      
      void Reader::start_reader()
      {
      	QMutexLocker locker(&mutex);
      
      	if (isRunning())
      	{
      		start();
      	}
      	else
      	{
      		restart = true;
      		condition.wakeOne();
      	}
      
      }
      
      void Reader::run()
      {
        emit Done();
      }
      

      Reader.h

      #pragma once
      
      #include <iostream>
      
      #include <QThread>
      #include <QMutex>
      #include <QWaitCondition>
      
      class Reader : public QThread
      {
      	Q_OBJECT
      
      public:
      	Reader(QObject *parent = 0);
      	~Reader();
      
      	void start_reader();
      
      signals:
      	void Done();
      
      protected:
      	void run() override;
      
      private:
      	QMutex mutex;
      	QWaitCondition condition;
      	bool abort, restart;
      };
      

      PS: Sorry if this isn't the correct forum...

      K Offline
      K Offline
      kenchan
      wrote on last edited by
      #2

      @Jeep
      I suggest you post some of your code or no one will be able to suggest what you are doing wrong!

      1 Reply Last reply
      0
      • D Offline
        D Offline
        deleted61
        wrote on last edited by
        #3

        @kenchan Well, it was there the whole time under "Source: Gist" but I added it to code blocks as well.

        K 1 Reply Last reply
        0
        • D deleted61

          @kenchan Well, it was there the whole time under "Source: Gist" but I added it to code blocks as well.

          K Offline
          K Offline
          kenchan
          wrote on last edited by
          #4

          @Jeep Well, blow we down so it was :-P

          1 Reply Last reply
          0
          • D deleted61

            Hey, I was testing Qt out and was trying to do multi-threading with a simple test application to see how it would work. Well, I fast ran into the problem that either the Reader thread I created won't emit or the main application doesn't receive the signal. I've tried Googling my problem but didn't find anything that helped and I already spent a day trying to get this to work, so if anyone knows what I'm doing wrong I would appreciate the insight...

            Qt Version: Qt 5.11.1 MSVC2017 64bit
            Source: Gist
            MyApp.cpp

            #include "MyApp.h"
            
            MyApp::MyApp(QWidget *parent)
            	: QMainWindow(parent)
            {
            	ui.setupUi(this);
            
            	connect(&reader, SIGNAL(Done()), this, SLOT(done()));
                
            	AllocConsole();
            	freopen("CONOUT$", "w", stdout);
            
            	reader.start_reader();
            }
            
            void MyApp::done()
            {
            	std::cout << "Reader done!" << std::endl;
            }
            

            MyApp.h

            #pragma once
            
            #include <iostream>
            
            #include <QtWidgets/QMainWindow>
            #include "ui_MyApp.h"
            
            #include "Reader.h"
            
            class MyApp : public QMainWindow
            {
            	Q_OBJECT
            
            
            public:
            	MyApp(QWidget *parent = 0);
            
            public slots:
            	void done();
            
            private:
            	Ui::MyAppClass ui;
            	Reader reader;
            };
            

            Reader.cpp

            #include "Reader.h"
            
            Reader::Reader(QObject* parent)
            {
            	abort = false;
            	restart = false;
            }
            
            Reader::~Reader()
            {
            	mutex.lock();
            	abort = true;
            	condition.wakeOne();
            	mutex.unlock();
            
            	wait();
            }
            
            void Reader::start_reader()
            {
            	QMutexLocker locker(&mutex);
            
            	if (isRunning())
            	{
            		start();
            	}
            	else
            	{
            		restart = true;
            		condition.wakeOne();
            	}
            
            }
            
            void Reader::run()
            {
              emit Done();
            }
            

            Reader.h

            #pragma once
            
            #include <iostream>
            
            #include <QThread>
            #include <QMutex>
            #include <QWaitCondition>
            
            class Reader : public QThread
            {
            	Q_OBJECT
            
            public:
            	Reader(QObject *parent = 0);
            	~Reader();
            
            	void start_reader();
            
            signals:
            	void Done();
            
            protected:
            	void run() override;
            
            private:
            	QMutex mutex;
            	QWaitCondition condition;
            	bool abort, restart;
            };
            

            PS: Sorry if this isn't the correct forum...

            kshegunovK Offline
            kshegunovK Offline
            kshegunov
            Moderators
            wrote on last edited by
            #5

            @Jeep said in Emit Problems:

            if anyone knows what I'm doing wrong I would appreciate the insight...

            Not yet. Remove the mutex and the condition and simplify. Call wait() in the main window's destructor not in the thread's destructor, and instead of calling start_reader() just call start(). Most important of all make sure that the signal-slot connection is successful - use the pointer to member syntax there (Qt5 syntax) and check the return value.

            Read and abide by the Qt Code of Conduct

            1 Reply Last reply
            4
            • D Offline
              D Offline
              deleted61
              wrote on last edited by
              #6

              @kshegunov
              Thanks, seems to work now after the changes you suggested! :)
              I also updated the gist with the working code.

              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