Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Returning C++ references from more programming interfaces?

Returning C++ references from more programming interfaces?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qstandarditemqvectorreferencesapisoftware design
27 Posts 4 Posters 7.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J JKSH
    21 Oct 2018, 20:27

    @elfring said in Returning C++ references from more programming interfaces?:

    I find it interesting then that the QVector class contains some member functions which return references to an object which was stored at a specific position in such a container. Can this functionality make sense also for other classes like QStandardItem?

    No, it does not make sense.

    If you add this functionality to QStandardItem, you will break encapsulation which is bad for object-oriented programming.

    E Offline
    E Offline
    elfring
    wrote on 21 Oct 2018, 20:40 last edited by
    #3

    …, you will break encapsulation which is bad for object-oriented programming.

    Do you find the “encapsulation” already broken for the QVector class then?

    Will software design extensions become feasible?

    J 1 Reply Last reply 21 Oct 2018, 20:41
    0
    • E elfring
      21 Oct 2018, 20:40

      …, you will break encapsulation which is bad for object-oriented programming.

      Do you find the “encapsulation” already broken for the QVector class then?

      Will software design extensions become feasible?

      J Offline
      J Offline
      JKSH
      Moderators
      wrote on 21 Oct 2018, 20:41 last edited by
      #4

      @elfring said in Returning C++ references from more programming interfaces?:

      Do you find the “encapsulation” already broken for the QVector class then?

      No, because QVector is a generic container. It is designed to allow full access to every element.

      QStandardItem is not a generic container.

      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

      E 1 Reply Last reply 21 Oct 2018, 20:44
      2
      • J JKSH
        21 Oct 2018, 20:41

        @elfring said in Returning C++ references from more programming interfaces?:

        Do you find the “encapsulation” already broken for the QVector class then?

        No, because QVector is a generic container. It is designed to allow full access to every element.

        QStandardItem is not a generic container.

        E Offline
        E Offline
        elfring
        wrote on 21 Oct 2018, 20:44 last edited by
        #5

        QStandardItem is not a generic container.

        Can any more generic functionality be combined also with this class?

        J 1 Reply Last reply 21 Oct 2018, 20:55
        0
        • E elfring
          21 Oct 2018, 20:44

          QStandardItem is not a generic container.

          Can any more generic functionality be combined also with this class?

          J Offline
          J Offline
          JKSH
          Moderators
          wrote on 21 Oct 2018, 20:55 last edited by
          #6

          @elfring said in Returning C++ references from more programming interfaces?:

          Can any more generic functionality be combined also with this class?

          No. QStandardItem is a very specific class so you should not add generic functionality to it.

          If you want generic functionality, use a different class.

          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

          1 Reply Last reply
          2
          • V Offline
            V Offline
            VRonin
            wrote on 22 Oct 2018, 07:58 last edited by VRonin
            #7

            Say you return a reference (to the data, I assume). And you change said data. How is the model supposed to know that the data changed. In other words, who takes care of emitting dataChanged()?
            The reference user can't be an acceptable answer as it would mean the developer has to remember to call a function every time and that's not something the current C++ evolution likes.

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            E 1 Reply Last reply 22 Oct 2018, 10:32
            3
            • V VRonin
              22 Oct 2018, 07:58

              Say you return a reference (to the data, I assume). And you change said data. How is the model supposed to know that the data changed. In other words, who takes care of emitting dataChanged()?
              The reference user can't be an acceptable answer as it would mean the developer has to remember to call a function every time and that's not something the current C++ evolution likes.

              E Offline
              E Offline
              elfring
              wrote on 22 Oct 2018, 10:32 last edited by
              #8

              In other words, who takes care of emitting dataChanged()?

              The responsibility is the same to notify others about data changes, isn't it?

              Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

              J 1 Reply Last reply 23 Oct 2018, 19:42
              0
              • E elfring
                22 Oct 2018, 10:32

                In other words, who takes care of emitting dataChanged()?

                The responsibility is the same to notify others about data changes, isn't it?

                Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

                J Offline
                J Offline
                JKSH
                Moderators
                wrote on 23 Oct 2018, 19:42 last edited by JKSH
                #9

                @elfring said in Returning C++ references from more programming interfaces?:

                In other words, who takes care of emitting dataChanged()?

                The responsibility is the same to notify others about data changes, isn't it?

                Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

                QStandardItemModel is responsible for emitting dataChanged(). Every time you call QStandardItem::setValue(), QStandardItemModel will automatically emit dataChanged().

                If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted. This is why we MUST NOT provide a reference to a QStandardItem's data!

                Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                E 1 Reply Last reply 24 Oct 2018, 08:56
                5
                • J JKSH
                  23 Oct 2018, 19:42

                  @elfring said in Returning C++ references from more programming interfaces?:

                  In other words, who takes care of emitting dataChanged()?

                  The responsibility is the same to notify others about data changes, isn't it?

                  Have you got the impression that this aspect would be different because of the reachability of specific information by pointers or references?

                  QStandardItemModel is responsible for emitting dataChanged(). Every time you call QStandardItem::setValue(), QStandardItemModel will automatically emit dataChanged().

                  If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted. This is why we MUST NOT provide a reference to a QStandardItem's data!

                  E Offline
                  E Offline
                  elfring
                  wrote on 24 Oct 2018, 08:56 last edited by
                  #10

                  Every time you call QStandardItem::setValue(), …

                  Would you like to refer to an other class (or member function) here?

                  If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted.

                  How do you think about to take additional solutions into account for this software development concern?

                  Can such customised functions trigger also the desired change notification?

                  V 1 Reply Last reply 24 Oct 2018, 11:04
                  0
                  • E elfring
                    24 Oct 2018, 08:56

                    Every time you call QStandardItem::setValue(), …

                    Would you like to refer to an other class (or member function) here?

                    If you get a reference to the QStandardItem's data and then modify the data, the dataChanged() signal will not be emitted.

                    How do you think about to take additional solutions into account for this software development concern?

                    Can such customised functions trigger also the desired change notification?

                    V Offline
                    V Offline
                    VRonin
                    wrote on 24 Oct 2018, 11:04 last edited by
                    #11

                    @elfring said in Returning C++ references from more programming interfaces?:

                    Would you like to refer to an other class (or member function) here?

                    It's setData, just a typo

                    Can such customised functions trigger also the desired change notification?

                    I can't think of how but I'm happy to see an example implementation of what would work

                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                    ~Napoleon Bonaparte

                    On a crusade to banish setIndexWidget() from the holy land of Qt

                    E 1 Reply Last reply 24 Oct 2018, 11:45
                    3
                    • V VRonin
                      24 Oct 2018, 11:04

                      @elfring said in Returning C++ references from more programming interfaces?:

                      Would you like to refer to an other class (or member function) here?

                      It's setData, just a typo

                      Can such customised functions trigger also the desired change notification?

                      I can't think of how but I'm happy to see an example implementation of what would work

                      E Offline
                      E Offline
                      elfring
                      wrote on 24 Oct 2018, 11:45 last edited by
                      #12

                      I can't think of how but I'm happy to see an example implementation of what would work

                      I am curious on how the corresponding change acceptance will evolve for possible extensions around the class “QStandardItem”.

                      J 1 Reply Last reply 24 Oct 2018, 11:49
                      0
                      • E elfring
                        24 Oct 2018, 11:45

                        I can't think of how but I'm happy to see an example implementation of what would work

                        I am curious on how the corresponding change acceptance will evolve for possible extensions around the class “QStandardItem”.

                        J Online
                        J Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on 24 Oct 2018, 11:49 last edited by
                        #13

                        @elfring I think @VRonin asked for an example, you do not have to submit a patch to Qt

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        E 1 Reply Last reply 24 Oct 2018, 12:00
                        2
                        • J jsulm
                          24 Oct 2018, 11:49

                          @elfring I think @VRonin asked for an example, you do not have to submit a patch to Qt

                          E Offline
                          E Offline
                          elfring
                          wrote on 24 Oct 2018, 12:00 last edited by
                          #14

                          … , you do not have to submit a patch to Qt

                          • Can the work with elements from a QStandardItem object become as convenient as the access for simple arrays?
                          • Are you used to the programming with template functions and overloaded operators?
                          1 Reply Last reply
                          0
                          • V Offline
                            V Offline
                            VRonin
                            wrote on 24 Oct 2018, 12:00 last edited by VRonin
                            #15

                            To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

                            class Example : public QObject{
                                Q_OBJECT
                            public:
                                explicit Example(QObject* parent = nullptr) 
                                    : QObject(parent), m_value(0)
                                {}
                                const int& value() const {return m_value;}
                                void setValue(const int& val){
                                    if(m_value==val)
                                        return;
                                    m_value=val;
                                    valueChanged(m_value);
                                }
                            signals:
                                void valueChanged(int val);
                            private:
                                int m_value;
                            };
                            

                            How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                            ~Napoleon Bonaparte

                            On a crusade to banish setIndexWidget() from the holy land of Qt

                            E V 2 Replies Last reply 24 Oct 2018, 12:38
                            1
                            • V VRonin
                              24 Oct 2018, 12:00

                              To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

                              class Example : public QObject{
                                  Q_OBJECT
                              public:
                                  explicit Example(QObject* parent = nullptr) 
                                      : QObject(parent), m_value(0)
                                  {}
                                  const int& value() const {return m_value;}
                                  void setValue(const int& val){
                                      if(m_value==val)
                                          return;
                                      m_value=val;
                                      valueChanged(m_value);
                                  }
                              signals:
                                  void valueChanged(int val);
                              private:
                                  int m_value;
                              };
                              

                              How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                              E Offline
                              E Offline
                              elfring
                              wrote on 24 Oct 2018, 12:38 last edited by
                              #16

                              How would you structure something like …

                              I would expect that the member variable will be changed only if no C++ exceptions were thrown.
                              Thus I would interpret the following approach as an useful software design option.

                              void modify(my_data const & md)
                              {
                              m_value = md;
                              emit valueChanged();
                              }
                              

                              Would you like to extend such an example with function objects or the application of lambdas?

                              1 Reply Last reply
                              0
                              • V Offline
                                V Offline
                                VRonin
                                wrote on 24 Oct 2018, 13:03 last edited by VRonin
                                #17

                                m_value = md;

                                So what's the advantage of using a reference if you still call the assignment operator (that would trigger a copy)? and what's the difference between your modify and my setValue?

                                "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                ~Napoleon Bonaparte

                                On a crusade to banish setIndexWidget() from the holy land of Qt

                                E 1 Reply Last reply 24 Oct 2018, 14:42
                                0
                                • V VRonin
                                  24 Oct 2018, 13:03

                                  m_value = md;

                                  So what's the advantage of using a reference if you still call the assignment operator (that would trigger a copy)? and what's the difference between your modify and my setValue?

                                  E Offline
                                  E Offline
                                  elfring
                                  wrote on 24 Oct 2018, 14:42 last edited by
                                  #18

                                  … and what's the difference between your modify and my setValue?

                                  I omitted an equality check in my example.
                                  But I guess that this implementation detail distracts from the original issue of my feature request here.

                                  Would you like to adjust programming interfaces around container class variants any further?

                                  1 Reply Last reply
                                  0
                                  • V VRonin
                                    24 Oct 2018, 12:00

                                    To put it into prospective, let's assume we have something like this and let's assume int is not super cheap to copy, just for argument sake:

                                    class Example : public QObject{
                                        Q_OBJECT
                                    public:
                                        explicit Example(QObject* parent = nullptr) 
                                            : QObject(parent), m_value(0)
                                        {}
                                        const int& value() const {return m_value;}
                                        void setValue(const int& val){
                                            if(m_value==val)
                                                return;
                                            m_value=val;
                                            valueChanged(m_value);
                                        }
                                    signals:
                                        void valueChanged(int val);
                                    private:
                                        int m_value;
                                    };
                                    

                                    How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                    V Offline
                                    V Offline
                                    VRonin
                                    wrote on 24 Oct 2018, 15:09 last edited by
                                    #19

                                    You didn't answer

                                    @VRonin said in Returning C++ references from more programming interfaces?:

                                    How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                    "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                    ~Napoleon Bonaparte

                                    On a crusade to banish setIndexWidget() from the holy land of Qt

                                    E 1 Reply Last reply 24 Oct 2018, 16:11
                                    0
                                    • V VRonin
                                      24 Oct 2018, 15:09

                                      You didn't answer

                                      @VRonin said in Returning C++ references from more programming interfaces?:

                                      How would you structure something like int& value() {return m_value;} that assures you the signal valueChanged is emitted in case the reference is changed?

                                      E Offline
                                      E Offline
                                      elfring
                                      wrote on 24 Oct 2018, 16:11 last edited by
                                      #20

                                      You didn't answer

                                      I suggest to distinguish the update scope and the actor which should trigger the desired change notification (by a specific function call).
                                      Another software design option would be the use of a corresponding class, wouldn't it?

                                      Example demo1;
                                      
                                      struct notifier
                                      {
                                       Example& ex;
                                       
                                       notifier(Example& target, int input)
                                       : ex(target)
                                       { ex[0] = input; }
                                       
                                       ~notifier()
                                       { ex.valueChanged(); }
                                      } demo2(demo1, 123);
                                      
                                      J 1 Reply Last reply 24 Oct 2018, 22:40
                                      0
                                      • E elfring
                                        24 Oct 2018, 16:11

                                        You didn't answer

                                        I suggest to distinguish the update scope and the actor which should trigger the desired change notification (by a specific function call).
                                        Another software design option would be the use of a corresponding class, wouldn't it?

                                        Example demo1;
                                        
                                        struct notifier
                                        {
                                         Example& ex;
                                         
                                         notifier(Example& target, int input)
                                         : ex(target)
                                         { ex[0] = input; }
                                         
                                         ~notifier()
                                         { ex.valueChanged(); }
                                        } demo2(demo1, 123);
                                        
                                        J Offline
                                        J Offline
                                        JKSH
                                        Moderators
                                        wrote on 24 Oct 2018, 22:40 last edited by JKSH
                                        #21

                                        @elfring, so you want programmers to replace Code 1 with Code 2; have I understood you correctly?

                                        //======
                                        // Code 1
                                        //======
                                        Example demo1;
                                        demo1.setData(123); // Automatically emits valueChanged() immediately
                                        
                                        //======
                                        // Code 2
                                        //======
                                        Example demo1;
                                        notifier demo2(demo1, 123);
                                        
                                        // valueChanged() is emitted when demo2 is destroyed
                                        

                                        I must say that Code 1 is a much more elegant and intuitive than Code 2.

                                        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                                        E 1 Reply Last reply 25 Oct 2018, 05:20
                                        2
                                        • J JKSH
                                          24 Oct 2018, 22:40

                                          @elfring, so you want programmers to replace Code 1 with Code 2; have I understood you correctly?

                                          //======
                                          // Code 1
                                          //======
                                          Example demo1;
                                          demo1.setData(123); // Automatically emits valueChanged() immediately
                                          
                                          //======
                                          // Code 2
                                          //======
                                          Example demo1;
                                          notifier demo2(demo1, 123);
                                          
                                          // valueChanged() is emitted when demo2 is destroyed
                                          

                                          I must say that Code 1 is a much more elegant and intuitive than Code 2.

                                          E Offline
                                          E Offline
                                          elfring
                                          wrote on 25 Oct 2018, 05:20 last edited by
                                          #22

                                          so you want programmers to replace Code 1 with Code 2; …

                                          Not really. - I suggest to choose between available software design options.

                                          The standard behaviour of the function “QStandardItem::setData” is generally fine.
                                          The software situaton might look different if more reference-returning functions from a container class like QVector will be taken into account.
                                          A need can evolve to call the function “valueChanged” (or “dataChanged”) in a C++ destructor, can't it?

                                          1 Reply Last reply
                                          0

                                          12/27

                                          24 Oct 2018, 11:45

                                          • Login

                                          • Login or register to search.
                                          12 out of 27
                                          • First post
                                            12/27
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • Users
                                          • Groups
                                          • Search
                                          • Get Qt Extensions
                                          • Unsolved