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. Increasing usage of references for non-value objects?
QtWS25 Last Chance

Increasing usage of references for non-value objects?

Scheduled Pinned Locked Moved Unsolved General and Desktop
data modelsreferencesvalue objectscopyingsoftware design
30 Posts 5 Posters 6.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.
  • E Offline
    E Offline
    elfring
    wrote on 30 Sept 2018, 11:39 last edited by
    #1

    There is a data structure category which is known as “value object”. Copying of contents should often be avoided for other data types.
    A lot of functions return objects as values while the usage of references would occasionally be preferred then. Can this software situation be improved anyhow also for the Qt class library?

    J 1 Reply Last reply 1 Oct 2018, 04:15
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 30 Sept 2018, 12:02 last edited by
      #2

      Since most Qt objects are using implicit sharing I don't see that big problem here... http://doc.qt.io/qt-5/implicit-sharing.html

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      E 1 Reply Last reply 30 Sept 2018, 12:44
      4
      • C Christian Ehrlicher
        30 Sept 2018, 12:02

        Since most Qt objects are using implicit sharing I don't see that big problem here... http://doc.qt.io/qt-5/implicit-sharing.html

        E Offline
        E Offline
        elfring
        wrote on 30 Sept 2018, 12:44 last edited by
        #3

        I don't see that big problem here...

        There are also parts of data structures to consider which are not “implicitly shared”.

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 30 Sept 2018, 16:51 last edited by
          #4

          @elfring said in Increasing usage of references for non-value objects?:

          There are also parts of data structures to consider which are not “implicitly shared”.

          I can't follow you - when the object is implicit shared it doesn't matter if one of it's elements is implicit shared or not...

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          E 1 Reply Last reply 30 Sept 2018, 17:23
          1
          • C Christian Ehrlicher
            30 Sept 2018, 16:51

            @elfring said in Increasing usage of references for non-value objects?:

            There are also parts of data structures to consider which are not “implicitly shared”.

            I can't follow you - when the object is implicit shared it doesn't matter if one of it's elements is implicit shared or not...

            E Offline
            E Offline
            elfring
            wrote on 30 Sept 2018, 17:23 last edited by
            #5

            I can't follow you

            I suggest to take another look according to growing data structure sizes.

            • when the object is implicit shared it doesn't matter if one of it's elements is implicit shared or not...
            • An object can contain further elements which are sharable.
            • Custom data structure members might need special treatment, don't they?
            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 30 Sept 2018, 17:30 last edited by
              #6

              An object can contain further elements which are sharable.

              As i said above - as long as the object is implicit shared it does not matter at all.

              @elfring said in Increasing usage of references for non-value objects?:

              Custom data structure members might need special treatment

              There you have full control over the data structure so you can do what you want with it e.g. returning it as const ref in getters or whatever.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              2
              • E elfring
                30 Sept 2018, 11:39

                There is a data structure category which is known as “value object”. Copying of contents should often be avoided for other data types.
                A lot of functions return objects as values while the usage of references would occasionally be preferred then. Can this software situation be improved anyhow also for the Qt class library?

                J Offline
                J Offline
                JKSH
                Moderators
                wrote on 1 Oct 2018, 04:15 last edited by
                #7

                @elfring said in Increasing usage of references for non-value objects?:

                There is a data structure category which is known as “value object”. Copying of contents should often be avoided for other data types.
                A lot of functions return objects as values while the usage of references would occasionally be preferred then. Can this software situation be improved anyhow also for the Qt class library?

                Can you give a few examples of problematic functions in Qt?

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

                E 1 Reply Last reply 1 Oct 2018, 13:15
                0
                • J JKSH
                  1 Oct 2018, 04:15

                  @elfring said in Increasing usage of references for non-value objects?:

                  There is a data structure category which is known as “value object”. Copying of contents should often be avoided for other data types.
                  A lot of functions return objects as values while the usage of references would occasionally be preferred then. Can this software situation be improved anyhow also for the Qt class library?

                  Can you give a few examples of problematic functions in Qt?

                  E Offline
                  E Offline
                  elfring
                  wrote on 1 Oct 2018, 13:15 last edited by
                  #8

                  Can you give a few examples of problematic functions in Qt?

                  The Qt documentation provides the following information.
                  “…
                  Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.
                  …”

                  Pointers belong to the category “value object”.
                  But the situation can become more interesting if you would need direct access for the data structures they refer to, can't it?

                  Under which circumstances would you like to fiddle with C++ references any more?

                  J 1 Reply Last reply 1 Oct 2018, 15:07
                  0
                  • E elfring
                    1 Oct 2018, 13:15

                    Can you give a few examples of problematic functions in Qt?

                    The Qt documentation provides the following information.
                    “…
                    Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.
                    …”

                    Pointers belong to the category “value object”.
                    But the situation can become more interesting if you would need direct access for the data structures they refer to, can't it?

                    Under which circumstances would you like to fiddle with C++ references any more?

                    J Offline
                    J Offline
                    JKSH
                    Moderators
                    wrote on 1 Oct 2018, 15:07 last edited by JKSH 10 Jan 2018, 15:07
                    #9

                    @elfring said in Increasing usage of references for non-value objects?:

                    Can you give a few examples of problematic functions in Qt?

                    The Qt documentation provides the following information.
                    “…
                    Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.
                    …”

                    Pointers belong to the category “value object”.
                    But the situation can become more interesting if you would need direct access for the data structures they refer to, can't it?

                    Under which circumstances would you like to fiddle with C++ references any more?

                    I'm sorry, I can't understand you.

                    Let's start from the beginning. You said "A lot of functions return objects as values while the usage of references would occasionally be preferred then."

                    Now, please do these 2 steps:

                    1. Name one function that returns objects as values.
                    2. Describe how you can improve that function by using references.

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

                    E 1 Reply Last reply 1 Oct 2018, 17:08
                    1
                    • J JKSH
                      1 Oct 2018, 15:07

                      @elfring said in Increasing usage of references for non-value objects?:

                      Can you give a few examples of problematic functions in Qt?

                      The Qt documentation provides the following information.
                      “…
                      Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e., copy-on-write.
                      …”

                      Pointers belong to the category “value object”.
                      But the situation can become more interesting if you would need direct access for the data structures they refer to, can't it?

                      Under which circumstances would you like to fiddle with C++ references any more?

                      I'm sorry, I can't understand you.

                      Let's start from the beginning. You said "A lot of functions return objects as values while the usage of references would occasionally be preferred then."

                      Now, please do these 2 steps:

                      1. Name one function that returns objects as values.
                      2. Describe how you can improve that function by using references.
                      E Offline
                      E Offline
                      elfring
                      wrote on 1 Oct 2018, 17:08 last edited by
                      #10

                      Let's start from the beginning.

                      • Do you work with any data structures that are not implicitly shared?
                      • How do you think about the following use case?
                        1. Call a function like “QStandardItem::data” so that you get a QVariant object.
                        2. This object can eventually be converted to an instance of a custom data type by the function “QVariant::value”.
                          Would you like to avoid extra data copies there occasionally (when you try to modify the custom object in-place instead)?
                      1 Reply Last reply
                      0
                      • J Offline
                        J Offline
                        JKSH
                        Moderators
                        wrote on 2 Oct 2018, 03:30 last edited by JKSH 10 Feb 2018, 03:31
                        #11

                        @elfring said in Increasing usage of references for non-value objects?:

                        • Do you work with any data structures that are not implicitly shared?

                        Yes I do. When I implement a custom value object or data structure, I usually don't make it implicitly shared. Instead, I tend to store them as raw pointers or shared_ptr.

                        • How do you think about the following use case?
                          1. Call a function like “QStandardItem::data” so that you get a QVariant object.
                          2. This object can eventually be converted to an instance of a custom data type by the function “QVariant::value”.
                            Would you like to avoid extra data copies there occasionally (when you try to modify the custom object in-place instead)?

                        Thanks, I think I've got a better understanding of what you want now. Your goal is to modify the data inside a QStandardItem without copying anything, is that right?

                        So far, you're trying to achieving your goal by changing QVariant and/or changing QStandardItem

                        Let me propose an alternative solution: Subclass QStandardItem: https://forum.qt.io/topic/95037/support-for-constructing-qstandarditem-objects-from-qvariant-references/16

                        If you're willing to go further, I even recommend subclassing QAbstractItemModel or QAbstractTableModel. I don't think QStandardItemModel is well-suited for handling custom, complex data types.

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

                        E 1 Reply Last reply 2 Oct 2018, 07:10
                        2
                        • J JKSH
                          2 Oct 2018, 03:30

                          @elfring said in Increasing usage of references for non-value objects?:

                          • Do you work with any data structures that are not implicitly shared?

                          Yes I do. When I implement a custom value object or data structure, I usually don't make it implicitly shared. Instead, I tend to store them as raw pointers or shared_ptr.

                          • How do you think about the following use case?
                            1. Call a function like “QStandardItem::data” so that you get a QVariant object.
                            2. This object can eventually be converted to an instance of a custom data type by the function “QVariant::value”.
                              Would you like to avoid extra data copies there occasionally (when you try to modify the custom object in-place instead)?

                          Thanks, I think I've got a better understanding of what you want now. Your goal is to modify the data inside a QStandardItem without copying anything, is that right?

                          So far, you're trying to achieving your goal by changing QVariant and/or changing QStandardItem

                          Let me propose an alternative solution: Subclass QStandardItem: https://forum.qt.io/topic/95037/support-for-constructing-qstandarditem-objects-from-qvariant-references/16

                          If you're willing to go further, I even recommend subclassing QAbstractItemModel or QAbstractTableModel. I don't think QStandardItemModel is well-suited for handling custom, complex data types.

                          E Offline
                          E Offline
                          elfring
                          wrote on 2 Oct 2018, 07:10 last edited by
                          #12

                          Your goal is to modify the data inside a QStandardItem without copying anything, is that right?

                          Yes. - This is a software area where I am also looking for corresponding solutions.

                          I don't think QStandardItemModel is well-suited for handling custom, complex data types.

                          • It is another useful programming interface, isn't it?
                          • Will any extensions make software development easier here?
                          J 1 Reply Last reply 2 Oct 2018, 08:16
                          0
                          • E elfring
                            2 Oct 2018, 07:10

                            Your goal is to modify the data inside a QStandardItem without copying anything, is that right?

                            Yes. - This is a software area where I am also looking for corresponding solutions.

                            I don't think QStandardItemModel is well-suited for handling custom, complex data types.

                            • It is another useful programming interface, isn't it?
                            • Will any extensions make software development easier here?
                            J Offline
                            J Offline
                            JKSH
                            Moderators
                            wrote on 2 Oct 2018, 08:16 last edited by JKSH 10 Feb 2018, 08:38
                            #13

                            @elfring said in Increasing usage of references for non-value objects?:

                            I don't think QStandardItemModel is well-suited for handling custom, complex data types.

                            • It is another useful programming interface, isn't it?

                            Yes, it is a useful tool for certain use-cases.

                            Based on your requirements though, (custom data type + clean software design + no converting/copying data), other tools are better for your use-case.

                            • Will any extensions make software development easier here?

                            I don't think so.

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

                            E 1 Reply Last reply 2 Oct 2018, 08:28
                            0
                            • J JKSH
                              2 Oct 2018, 08:16

                              @elfring said in Increasing usage of references for non-value objects?:

                              I don't think QStandardItemModel is well-suited for handling custom, complex data types.

                              • It is another useful programming interface, isn't it?

                              Yes, it is a useful tool for certain use-cases.

                              Based on your requirements though, (custom data type + clean software design + no converting/copying data), other tools are better for your use-case.

                              • Will any extensions make software development easier here?

                              I don't think so.

                              E Offline
                              E Offline
                              elfring
                              wrote on 2 Oct 2018, 08:28 last edited by
                              #14

                              Will any extensions make software development easier here?

                              I don't think so.

                              I am curious on how needed extensions can eventually move into Qt software libraries.

                              J 1 Reply Last reply 2 Oct 2018, 08:36
                              0
                              • E elfring
                                2 Oct 2018, 08:28

                                Will any extensions make software development easier here?

                                I don't think so.

                                I am curious on how needed extensions can eventually move into Qt software libraries.

                                J Offline
                                J Offline
                                JKSH
                                Moderators
                                wrote on 2 Oct 2018, 08:36 last edited by
                                #15

                                @elfring said in Increasing usage of references for non-value objects?:

                                I am curious on how needed extensions can eventually move into Qt software libraries.

                                What extensions do you want? Adding a constructor (QStandardItem::QStandardItem(const QVariant&, int)) is discussed at https://forum.qt.io/topic/95037/support-for-constructing-qstandarditem-objects-from-qvariant-references -- do you have any others in mind?

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

                                J E 2 Replies Last reply 2 Oct 2018, 08:43
                                0
                                • J JKSH
                                  2 Oct 2018, 08:36

                                  @elfring said in Increasing usage of references for non-value objects?:

                                  I am curious on how needed extensions can eventually move into Qt software libraries.

                                  What extensions do you want? Adding a constructor (QStandardItem::QStandardItem(const QVariant&, int)) is discussed at https://forum.qt.io/topic/95037/support-for-constructing-qstandarditem-objects-from-qvariant-references -- do you have any others in mind?

                                  J Offline
                                  J Offline
                                  JKSH
                                  Moderators
                                  wrote on 2 Oct 2018, 08:43 last edited by
                                  #16

                                  Back to the original topic of this post (Accessing internal data of a QStandardItem):

                                  Imagine that an extension lets you get a reference/pointer to the internal data of QStandardItemModel. When you edit the data in-place, how will you notify the view that you have edited the data?

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

                                  E 1 Reply Last reply 2 Oct 2018, 09:31
                                  0
                                  • J JKSH
                                    2 Oct 2018, 08:36

                                    @elfring said in Increasing usage of references for non-value objects?:

                                    I am curious on how needed extensions can eventually move into Qt software libraries.

                                    What extensions do you want? Adding a constructor (QStandardItem::QStandardItem(const QVariant&, int)) is discussed at https://forum.qt.io/topic/95037/support-for-constructing-qstandarditem-objects-from-qvariant-references -- do you have any others in mind?

                                    E Offline
                                    E Offline
                                    elfring
                                    wrote on 2 Oct 2018, 09:23 last edited by
                                    #17

                                    do you have any others in mind?

                                    Yes, of course.

                                    • Programming interfaces which return pointers can be encapsulated so that null pointer checks will be performed at fewer source code places.
                                    • Model adaptors
                                    • Occasional Qt object construction without default parameters
                                    kshegunovK 1 Reply Last reply 2 Oct 2018, 15:10
                                    0
                                    • J JKSH
                                      2 Oct 2018, 08:43

                                      Back to the original topic of this post (Accessing internal data of a QStandardItem):

                                      Imagine that an extension lets you get a reference/pointer to the internal data of QStandardItemModel. When you edit the data in-place, how will you notify the view that you have edited the data?

                                      E Offline
                                      E Offline
                                      elfring
                                      wrote on 2 Oct 2018, 09:31 last edited by
                                      #18

                                      When you edit the data in-place, how will you notify the view that you have edited the data?

                                      The signal “itemChanged” should be emitted, shouldn't it?

                                      J 1 Reply Last reply 2 Oct 2018, 14:04
                                      0
                                      • E elfring
                                        2 Oct 2018, 09:31

                                        When you edit the data in-place, how will you notify the view that you have edited the data?

                                        The signal “itemChanged” should be emitted, shouldn't it?

                                        J Offline
                                        J Offline
                                        JKSH
                                        Moderators
                                        wrote on 2 Oct 2018, 14:04 last edited by
                                        #19

                                        @elfring said in Increasing usage of references for non-value objects?:

                                        When you edit the data in-place, how will you notify the view that you have edited the data?

                                        The signal “itemChanged” should be emitted, shouldn't it?

                                        Good! You're right, the object needs to emit a signal to notify the view.

                                        Next thing to think about: If you get a pointer/reference to the data inside a QStandardItem and you modify the object directly... how can you emit the "itemChanged()" signal?

                                        @elfring said in Increasing usage of references for non-value objects?:

                                        • Programming interfaces which return pointers can be encapsulated so that null pointer checks will be performed at fewer source code places.
                                        • Model adaptors
                                        • Occasional Qt object construction without default parameters

                                        I'll have a look through these ideas and reply later.

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

                                        E 1 Reply Last reply 2 Oct 2018, 14:30
                                        1
                                        • J JKSH
                                          2 Oct 2018, 14:04

                                          @elfring said in Increasing usage of references for non-value objects?:

                                          When you edit the data in-place, how will you notify the view that you have edited the data?

                                          The signal “itemChanged” should be emitted, shouldn't it?

                                          Good! You're right, the object needs to emit a signal to notify the view.

                                          Next thing to think about: If you get a pointer/reference to the data inside a QStandardItem and you modify the object directly... how can you emit the "itemChanged()" signal?

                                          @elfring said in Increasing usage of references for non-value objects?:

                                          • Programming interfaces which return pointers can be encapsulated so that null pointer checks will be performed at fewer source code places.
                                          • Model adaptors
                                          • Occasional Qt object construction without default parameters

                                          I'll have a look through these ideas and reply later.

                                          E Offline
                                          E Offline
                                          elfring
                                          wrote on 2 Oct 2018, 14:30 last edited by
                                          #20

                                          how can you emit the "itemChanged()" signal?

                                          I would use a simple function call for this purpose.
                                          Are you looking for any other answer?

                                          J 1 Reply Last reply 2 Oct 2018, 14:48
                                          0

                                          7/30

                                          1 Oct 2018, 04:15

                                          23 unread
                                          • Login

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