瀏覽代碼

Add "returnValue" property to qml GrpcSubscription

The property allows to propagate the value changed event to any object
which is bound to a property of the return value: this way there is no
need to update manually the values through the updated callback
Giulio Girardi 4 年之前
父節點
當前提交
f704927929
共有 2 個文件被更改,包括 12 次插入0 次删除
  1. 3 0
      src/grpc/quick/qquickgrpcsubscription.cpp
  2. 9 0
      src/grpc/quick/qquickgrpcsubscription_p.h

+ 3 - 0
src/grpc/quick/qquickgrpcsubscription.cpp

@@ -54,6 +54,7 @@ void QQuickGrpcSubscription::updateSubscription()
     if (m_returnValue != nullptr) {
         m_returnValue->deleteLater(); //TODO: probably need to take care about return value cleanup other way. It's just reminder about weak memory management.
         m_returnValue = nullptr;
+        returnValueChanged();
     }
 
     if (m_client.isNull() || m_method.isEmpty() || !m_enabled || m_argument.isNull()) {
@@ -144,6 +145,8 @@ bool QQuickGrpcSubscription::subscribe()
         return false;
     }
 
+    returnValueChanged();
+
     QGrpcSubscription *subscription = nullptr;
     bool ok = method.invoke(m_client, Qt::DirectConnection,
                                       QGenericReturnArgument("QtProtobuf::QGrpcSubscription*", static_cast<void *>(&subscription)),

+ 9 - 0
src/grpc/quick/qquickgrpcsubscription_p.h

@@ -55,6 +55,9 @@
  * \subsubsection argument QObject *argument
  * \details Pointer to argument that will be used for subscription.
  *
+ * \subsubsection returnValue QObject *returnValue
+ * \details Value returned by the subscription (Note that it is the same "return" object passed by the "updated" signal)
+ *
  * \subsection Signals
  * \subsubsection updated updated(ReturnType value)
  * \details The signal notifies about received update for subscription. It provides "return" value ready for use in QML.
@@ -97,6 +100,7 @@ class QQuickGrpcSubscription : public QObject
     Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
     Q_PROPERTY(QString method READ method WRITE setMethod NOTIFY methodChanged)
     Q_PROPERTY(QObject *argument READ argument WRITE setArgument NOTIFY argumentChanged)
+    Q_PROPERTY(QObject *returnValue READ returnValue NOTIFY returnValueChanged)
 
 public:
     QQuickGrpcSubscription(QObject *parent = nullptr);
@@ -118,6 +122,10 @@ public:
         return m_argument;
     }
 
+    QObject *returnValue() const {
+        return m_returnValue;
+    }
+
     void setClient(QAbstractGrpcClient *client) {
         if (m_client == client) {
             return;
@@ -166,6 +174,7 @@ signals:
     void methodChanged();
     void enabledChanged();
     void argumentChanged();
+    void returnValueChanged();
 
 private:
     void updateSubscription();