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 fromQThread
. So that, it is create diamond problem, and I am getting the error like belowhow 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?
-
Issue is coming because twice you are inheriting from QOobject. Once is coming from QThread & Other is UpLoader. Remove this & it should work.
-
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? -
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 ofworker 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
-
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.
-
@Yash001 You can three classes & inheritance mechanism as you mentioned. I'm keeping aside the topic of why not to inherit from QThread.