如何使用qt中所有继承类的基类中的信号? 我有Qt快2应用程序,我的C++类有基础类< /p> 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; };

如何使用qt中所有继承类的基类中的信号? 我有Qt快2应用程序,我的C++类有基础类< /p> 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; };,c++,qt,qt-quick,qtquickcontrols2,busyindicator,C++,Qt,Qt Quick,Qtquickcontrols2,Busyindicator,我有许多类是从上面的类继承而来的 class BookManager : public ManagerEngine { }; void BookManager::getList(bool refreshList){ setIsProcssing(true); //get data from server setIsProcssing(false); } 现在,在ase类中,我想在方法从服务器获取数据时显示BusyIndicator BookPage.qml BookM

我有许多类是从上面的类继承而来的

class BookManager : public ManagerEngine
{
};

void BookManager::getList(bool refreshList){
    setIsProcssing(true);
    //get data from server
    setIsProcssing(false);
}
现在,在ase类中,我想在方法从服务器获取数据时显示
BusyIndicator

BookPage.qml

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

//I have many class like : login signup and ... inherited from base class
BusyIndicator{
    id:busyIndicator        
}
ManagerEngine{
    id: me
    onIsProcessingChanged: {
        progressIndicator.visible=isProcessing;
    }
}
BusyIndicator{
    id:busyIndicator        
}
类别页.qml

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

//I have many class like : login signup and ... inherited from base class
BusyIndicator{
    id:busyIndicator        
}
ManagerEngine{
    id: me
    onIsProcessingChanged: {
        progressIndicator.visible=isProcessing;
    }
}
BusyIndicator{
    id:busyIndicator        
}
问题页面.qml

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

//I have many class like : login signup and ... inherited from base class
BusyIndicator{
    id:busyIndicator        
}
ManagerEngine{
    id: me
    onIsProcessingChanged: {
        progressIndicator.visible=isProcessing;
    }
}
BusyIndicator{
    id:busyIndicator        
}
main.qml

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

//I have many class like : login signup and ... inherited from base class
BusyIndicator{
    id:busyIndicator        
}
ManagerEngine{
    id: me
    onIsProcessingChanged: {
        progressIndicator.visible=isProcessing;
    }
}
BusyIndicator{
    id:busyIndicator        
}
BookPage.qml
和。。。(上图)并在应用程序页面中显示busyIndicator。但我希望按时完成此操作

我尝试过这样做:我使用基类
ManagerEngine
来显示
BusyIndicator

我想显示如下所示的busyIndicator。(我在主页中声明了基类。现在,如果我打开BookPage.qml以显示图书列表,busyIndicator必须可见。)

main.qml

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

//I have many class like : login signup and ... inherited from base class
BusyIndicator{
    id:busyIndicator        
}
ManagerEngine{
    id: me
    onIsProcessingChanged: {
        progressIndicator.visible=isProcessing;
    }
}
BusyIndicator{
    id:busyIndicator        
}

但是它不起作用。有没有其他方法来完成这项工作(例如,我可以使用static关键字)

这永远不会起作用,因为这些类似乎在主线程中工作,并且当处理完成时,没有用户界面更新,应用程序被冻结

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

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

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 < /COD>成员声明意味着它是类成员,而不是对象成员。

接口 实施
就我个人而言,这个问题并不清楚。我只想说清楚,当你说
BusyIndicator
时,你的意思是
progressIndicator.visible
等于
isProcessing
?另外,请提及你所面临的错误,或者什么没有按预期工作。我不理解你,请更好地解释一下。你指出了吗派生类中的实现起作用,但基类中的实现不起作用,这与您的标题相矛盾。@EylanESC当我在继承类(BookManager)中使用时在BookPage.qml中,将显示busyindicator。但我希望调用BookPage.qml中的getlist,main.qml中的busyindicator将显示visible@mohsen是否希望如果继承自ManagerEngine的类中的任何对象更改了SetIsProcessing的状态,ManagerEngine类的对象也会得到通知?@mohsen我建议您编辑你的问题,并至少以个性化的方式添加我的评论,因为其他用户不理解你。问题是另一个问题,提问者的问题不是很清楚,但在评论中,他有一个名为ManagerEngine的类,假设有一个名为manager1和manager2的类的对象具有诸如BookManager之类的派生类,假设存在对象bookmanager1、bookmanager2,因此询问者要求,如果您更改bookmanagerX中的isProcessing属性,您也将收到管理员的通知。@eyllanesc但首先,使用信号的模式是错误的,QML会自动处理这些通知当你绑定到源对象的属性时。无论如何,我回答的是问题,而不是评论。提问者可以自由编辑问题以反映现实。我同意它所表明的,我所说的是,提问者想在他的问题中解释,但他没有正确地做,他把我弄糊涂了,因为他指出它在派生类但不在基类中,所以我说这是矛盾的,你不这样认为吗?这就是为什么我提出我的查询以便更好地理解询问者。