Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How can I create the base class for two singleton instance?



  • Base class 1:

    clude "qcoreapplication.h"
    #include "qobject.h"
    
    class Uploader: virtual public QObject {
    	Q_OBJECT
    signals:
    	void Started();
    	void ProgressChanged(int);
    	void BootloaderFound();
    	void Finished(bool);
    	
    public:
    	Uploader();
    	virtual ~Uploader();
    	virtual void upLoading() = 0;
    	virtual void SetHexFile(const QString&) = 0;
    };
    

    derived class 1:

    #pragma once
    #include "UpdateFirmware.h"
    #include "qprocess.h"
    
    
    class TeensyLoader : public Uploader
    {
    	TeensyLoader();
    	TeensyLoader(const TeensyLoader &);
    
    	QList<QMetaObject::Connection> connections;
    	QProcess process;
    	QString fileName;
    public:
    	static Uploader* Instance();
    	void upLoading();
    	void SetHexFile(const QString&);
    	~TeensyLoader();
    };
    

    Derived Class 2:

    #pragma once
    
    #include <QThread>
    #include <QEventLoop>
    
    #include <HexLoader.h>
    #include <BootloaderOperator.h>
    #include "UpdateFirmware.h"
    
    class BootloaderThread :public QThread, public Uploader {
    	Q_OBJECT
    
    	BootloaderThread();
    
    public:
    	static Uploader* Instance();
    
    	void run();
    	void upLoading();
    	void SetHexFile(const QString&);
    
    //signals:
    //	void ProgressChanged(int);
    //	//void Notification(const QString&);
    //	void Finished(bool);
    //	void Started();
    //	void BootloaderFound();
    
    private:
    	QString SearchForHidPath();
    	bool EraseFlash();
    	bool ProgramFlash(const QByteArray&);
    	bool CheckCrc();
    	void JumpToApplication();
    
    	HexRecords fw;
    	BootloaderOperator *bootOp;
    };
    

    Here I would like to declare signals in Base class and use into derived class.
    void Started(); void ProgressChanged(int); void BootloaderFound(); void Finished(bool);

    both derived class are created as single instance.
    one among two subclass is derived from QThread. So that, it is create diamond problem, and I am getting the error like below

    0_1556152541970_ea0ba279-4ec1-46f0-9450-889050463fdd-image.png

    how can I solve this error?

    I am trying to remove duplicate of code.
    Here my duplicated code.

    Uploader* bootloaderThread;
    	if (it->info.hwVer.hwModel < PLUS_2_0) {
    		bootloaderThread = TeensyLoader::Instance();
    	}
    	else {
    		bootloaderThread = BootloaderThread::Instance();
    	}
    
    	bootloaderThread->SetHexFile(hexFileName);
    
    	static bool firstTime = true;
    
    	if (firstTime) {
    		connect(bootloaderThread, &Uploader::ProgressChanged, this, &MainWindow::BootloaderSetProgress, Qt::QueuedConnection);
    		connect(bootloaderThread, &Uploader::Finished, this, &MainWindow::BootloaderFinished, Qt::QueuedConnection);
    		connect(bootloaderThread, &Uploader::Started, this, &MainWindow::BootloaderStarted, Qt::QueuedConnection);
    		connect(bootloaderThread, &Uploader::BootloaderFound, this, &MainWindow::BootloaderFound, Qt::QueuedConnection);
    		firstTime = false;
    	}
    
    	bootloaderThread->upLoading();
    

    any suggestion for removing duplicating code?


  • Qt Champions 2017

    Issue is coming because twice you are inheriting from QOobject. Once is coming from QThread & Other is UpLoader. Remove this & it should work.



  • @dheerendra

    I know that is Issue but How can I define the Signal in another class and used in class BootloaderThread. Is it any alternative way?


  • Lifetime Qt Champion

    Hi,

    Do you really need that QThread subclass or would the worker object approach be more adequate ?



  • @SGaist said in How can I create the base class for two singleton instance?:

    Do you really need that QThread subclass

    class BootloaderThread : public QThread is written by someone else, and It is very big class. so I don't know, I am able to modify or not.

    I am interested to know which requirement is force to developer for use subclass of QThread instead of worker Object approach.

    However, is it fine if I will do like below and do not use the run function in TeensyLoader class. or is it make problem.

    class Uploader: public QThread
    class TeensyLoader: public Uploader
    class BootloaderThread : public Uploader
    

  • Lifetime Qt Champion

    The QThread documentation shows both approaches.

    You should check what your classes are supposed to do and then define whether subclassing QThread is really needed. Most of the time, it's not the case.



  • Or just inject/create the uploader as an object/property of BootloaderThread.


  • Qt Champions 2017

    @Yash001 You can three classes & inheritance mechanism as you mentioned. I'm keeping aside the topic of why not to inherit from QThread.