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. How can I create the base class for two singleton instance?

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
refactorc++ threadqobjectqthreadqmetaobject
8 Posts 4 Posters 1.9k 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.
  • Y Offline
    Y Offline
    Yash001
    wrote on 25 Apr 2019, 00:39 last edited by Yash001
    #1

    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?

    1 Reply Last reply
    0
    • D Offline
      D Offline
      dheerendra
      Qt Champions 2022
      wrote on 25 Apr 2019, 01:36 last edited by
      #2

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

      Dheerendra
      @Community Service
      Certified Qt Specialist
      http://www.pthinks.com

      Y 1 Reply Last reply 25 Apr 2019, 16:54
      4
      • D dheerendra
        25 Apr 2019, 01:36

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

        Y Offline
        Y Offline
        Yash001
        wrote on 25 Apr 2019, 16:54 last edited by
        #3

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

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Apr 2019, 18:52 last edited by SGaist
          #4

          Hi,

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

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          Y 1 Reply Last reply 25 Apr 2019, 19:49
          1
          • S SGaist
            25 Apr 2019, 18:52

            Hi,

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

            Y Offline
            Y Offline
            Yash001
            wrote on 25 Apr 2019, 19:49 last edited by Yash001
            #5

            @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
            
            D 1 Reply Last reply 26 Apr 2019, 02:02
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 25 Apr 2019, 19:53 last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              1
              • fcarneyF Offline
                fcarneyF Offline
                fcarney
                wrote on 25 Apr 2019, 22:46 last edited by
                #7

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

                C++ is a perfectly valid school of magic.

                1 Reply Last reply
                0
                • Y Yash001
                  25 Apr 2019, 19:49

                  @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
                  
                  D Offline
                  D Offline
                  dheerendra
                  Qt Champions 2022
                  wrote on 26 Apr 2019, 02:02 last edited by
                  #8

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

                  Dheerendra
                  @Community Service
                  Certified Qt Specialist
                  http://www.pthinks.com

                  1 Reply Last reply
                  0

                  1/8

                  25 Apr 2019, 00:39

                  • Login

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