Emit Problems



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



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



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



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


  • Qt Champions 2017

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



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


Log in to reply
 

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