如何在基类中使用来自qt中所有继承类的信号?
How use signal in base class from all inherited class in qt?
我有一个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;
}
相关文章:
- 如何在QT Creator上将QWidget声明为继承类的对象
- Qt Quick - 如何在 qml 接口中使用继承自 QQuickPaintedItem 的 c++ 类?
- 具有 QObject 继承的单例 - Qt
- 类 QLayout 和类 QMainWindow Qt c++ 之间的继承
- 如何在基类中使用来自qt中所有继承类的信号?
- Qt:如何访问继承的小部件?
- 如何列出从Qt中Qobject继承的类的所有成员变量
- QDialog 继承类的 Qt 调用成员函数
- QT:私人会员而不是继承?原因是什么?这是一个具体的概念吗?
- Qt 无法继承属性
- 如何在Qt Creator中从继承的类自动创建虚拟方法
- 在构造函数 QT 创建器(继承类)中传递默认参数
- Qt 中的 c++ 继承基础
- QT:继承导致模棱两可
- qt qsharedDatapointer多重继承钻石图案
- Qt 访问继承类的 D 指针
- 使用CMake构建Qt项目并从QMainWindow继承会导致未引用的vtable错误
- 我应该采取什么方式使用 qt:all 类继承自 QObject 或删除 新后手动删除
- LNK尝试继承时Qt中出现错误
- 类和继承qt