如何在基类中使用来自qt中所有继承类的信号?

How use signal in base class from all inherited class in qt?

本文关键字:继承 qt 信号 基类      更新时间:2023-10-16

我有一个qt quick 2应用程序,我的c ++类有基类

class ManagerEngine : public QObject
{
Q_OBJECT
public:
explicit ManagerEngine(QObject *parent = nullptr);
bool isProcessing();
void setIsProcssing(bool isProcessing);
signals:    
void isProcessingChanged(bool isProcessing);
private:
bool m_IsProccessing;
};

我有很多从上面的类继承的类

class BookManager : public ManagerEngine
{
};
void BookManager::getList(bool refreshList){
setIsProcssing(true);
//get data from server
setIsProcssing(false);
}

现在在 thease 类中,我想BusyIndicator显示该方法何时从服务器获取一些数据。

BookPage.qml

BookManager{
id: bm
onIsProcessingChanged: {
busyIndicator.visible=isProcessing; // busyIndicator is in main.qml
}
}

类别页面.qml

CategoryManager{
id: cm
onIsProcessingChanged: {
busyIndicator.visible=isProcessing;
}
}

问题页面.qml

QuestionManager{
id: qm
onIsProcessingChanged: {
busyIndicator.visible=isProcessing;
}
}
//I have many class like : login signup and ... inherited from base class

主.qml

BusyIndicator{
id:busyIndicator        
}

BookPage.qml和...(上图(并在页面中显示繁忙指示器。但我想按时完成。

我试过这个:我使用基类ManagerEngine进行显示BusyIndicator

我想显示如下所示的繁忙指示器。(i 在主页中声明了基类。现在如果我打开BookPage.qml显示书籍列表,现在忙碌指示器必须可见

主.qml

ManagerEngine{
id: me
onIsProcessingChanged: {
progressIndicator.visible=isProcessing;
}
}
BusyIndicator{
id:busyIndicator        
}

但它不起作用。有没有另一种方法可以完成这项工作(例如我可以使用静态关键字(

这永远不会起作用,因为这些类似乎在主线程中工作,并且在处理完成时,不会进行任何用户界面更新,并且您的应用程序被冻结。

在所有情况下,解决方案都必须从使繁忙指示器成为适当的属性开始 - 这将神奇地解决您的 Qt Quick 问题,您可以简单地绑定到该属性,而根本不需要显式使用信号。这就是属性通知的用途,QML会为您处理它们。您也不希望资源库是公共的 - 这是内部的只读状态。您还需要恒定正确的签名。

解决所有问题后,我们得到:

class ManagerEngine : public QObject {
Q_OBJECT
Q_PROPERTY(bool isProcessing READ isProcessing NOTIFY isProcessingChanged)
public:
using QObject::QObject; //for C++14 & up
bool isProcessing() const;
Q_SIGNAL void isProcessingChanged(bool);
protected:
void setIsProcessing(bool);
private:
bool m_isProccessing = false;
};
/// This method is not thread-safe
void ManagerEngine::setIsProcessing(bool p) {
if (p == m_isProcessing) return;
m_isProcessing = p; // precondition of the signal
return emit isProcessingChanged(m_isProcessing);
}
/// This method is not thread-safe
bool ManagerEngine::isProcessing() const {
return m_isProcessing;
}

如果希望处理是管理器引擎的所有实例共享的状态,请使其成为类的属性,而不是对象的属性。在C++中,static成员声明意味着它是类成员,而不是对象成员。

接口

class ManagerEngine : public QObject {
Q_OBJECT
Q_PROPERTY(bool isProcessing READ isProcessing NOTIFY isProcessingChanged)
public:
using QObject::QObject; //for C++14 & up
~ManagerEngine() override;
bool isProcessing() const;
Q_SIGNAL void isProcessingChanged(bool);
protected:
void setIsProcessing(bool);
private:
bool m_isProcessing = false; // per-object
static QAtomicInt m_processingCount; // per-class
};

实现

QAtomicInt ManagerEngine::m_processingCount;
ManagerEngine::~MangerEngine() {
setIsProcessing(false);
// Perhaps it'd be more appropriate instead to simply
// Q_ASSERT(!m_isProcessing) if the engine should not be destroyed
// during processing.
}
// This method is not thread-safe, but multiple engines can
// reside in different threads.
void ManagerEngine::setIsProcessing(bool p) {
if (p == m_isProcessing) return;
int const delta = p ? (+1) : (-1);
auto count = m_processingCount.load();
Q_ASSERT(count >= 0);
Q_ASSERT(p || count > 0);
m_isProcessing = p;
while (!m_processingCount.testAndSetOrdered(count, count+delta)) {
count = m_processingCount.load();
}
// The signal will be emitted only when the global state changes,
// and exactly once per global state change.
if ((count > 0) != ((count+delta) > 0))
emit isProcessingChanged(count > 0);
return;
}
// Note: Due to data races, it is not guaranteed that the result of
// this method is the same as the parameter in the isProcessingChanged
// signal. Only the signal's parameter is guaranteed to change state
// in a race-free fashion, i.e. always toggle (alternate) and never repeat.
bool ManagerEngine::isProcessing() const {
auto const count = m_processingCount.load();
Q_ASSERT(count >= 0);
Q_ASSERT(!m_isProcessing || count > 0);
return count > 0;
}