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. Cannot create QImage using smart pointers

Cannot create QImage using smart pointers

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimageqpointer
16 Posts 6 Posters 2.4k 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.
  • S SGaist
    16 Dec 2020, 19:01

    Hi,

    As stated in the QPointer documentation, the type must derive from QObject. QImage does not.

    C Offline
    C Offline
    chessking5544
    wrote on 16 Dec 2020, 19:07 last edited by
    #6

    @SGaist that makes sense and I should have seen that in the docs. thanks. Will c++ smart pointers work, or is there just no way to do this? I'm looking for a way to get auto deletion for all QImages created.

    C S 2 Replies Last reply 16 Dec 2020, 19:13
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 16 Dec 2020, 19:09 last edited by
      #7

      Can you explain exactly how many QImage do you need to generate ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      C 1 Reply Last reply 16 Dec 2020, 19:18
      0
      • C chessking5544
        16 Dec 2020, 19:07

        @SGaist that makes sense and I should have seen that in the docs. thanks. Will c++ smart pointers work, or is there just no way to do this? I'm looking for a way to get auto deletion for all QImages created.

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 16 Dec 2020, 19:13 last edited by Christian Ehrlicher
        #8

        @chessking5544 said in Cannot create QImage using smart pointers:

        I'm looking for a way to get auto deletion for all QImages created.

        Don't create it on the stack but on the heap. Don't create it on the heap but on the stack. QImage will allocate it's internal image data on the stack anyway.

        /edit: mixed up stack and heap for unknown reasons - fixed :)

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

        J 1 Reply Last reply 16 Dec 2020, 21:44
        1
        • S SGaist
          16 Dec 2020, 19:09

          Can you explain exactly how many QImage do you need to generate ?

          C Offline
          C Offline
          chessking5544
          wrote on 16 Dec 2020, 19:18 last edited by
          #9

          @SGaist most likely a few per thread and maybe a dozen threads. I am coming from the C# world and trying to avoid potential memory leaks by using smart pointers for all dynamic allocation.

          1 Reply Last reply
          0
          • S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 16 Dec 2020, 19:25 last edited by
            #10

            As @Christian-Ehrlicher suggests: no need for heap allocation in the case of QImage. That will also free you from having to delete them manually.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            C 1 Reply Last reply 16 Dec 2020, 20:40
            0
            • S SGaist
              16 Dec 2020, 19:25

              As @Christian-Ehrlicher suggests: no need for heap allocation in the case of QImage. That will also free you from having to delete them manually.

              C Offline
              C Offline
              chessking5544
              wrote on 16 Dec 2020, 20:40 last edited by
              #11

              ok. thanks

              1 Reply Last reply
              0
              • C Christian Ehrlicher
                16 Dec 2020, 19:13

                @chessking5544 said in Cannot create QImage using smart pointers:

                I'm looking for a way to get auto deletion for all QImages created.

                Don't create it on the stack but on the heap. Don't create it on the heap but on the stack. QImage will allocate it's internal image data on the stack anyway.

                /edit: mixed up stack and heap for unknown reasons - fixed :)

                J Offline
                J Offline
                JonB
                wrote on 16 Dec 2020, 21:44 last edited by
                #12

                @Christian-Ehrlicher said in Cannot create QImage using smart pointers:

                Don't create it on the stack but on the heap.

                Do you want to rephrase this? :)

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 17 Dec 2020, 06:04 last edited by
                  #13

                  @JonB said in Cannot create QImage using smart pointers:

                  Do you want to rephrase this? :)

                  What should be rephrased here? It's basic ++ stuff. You can create an object either on the heap with new or directly on the stack.

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

                  J 1 Reply Last reply 17 Dec 2020, 06:12
                  0
                  • C Christian Ehrlicher
                    17 Dec 2020, 06:04

                    @JonB said in Cannot create QImage using smart pointers:

                    Do you want to rephrase this? :)

                    What should be rephrased here? It's basic ++ stuff. You can create an object either on the heap with new or directly on the stack.

                    J Offline
                    J Offline
                    jsulm
                    Lifetime Qt Champion
                    wrote on 17 Dec 2020, 06:12 last edited by
                    #14

                    @Christian-Ehrlicher "Don't create it on the stack but on the heap." - you should exchange stack and heap :-)

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

                    C 1 Reply Last reply 17 Dec 2020, 06:17
                    1
                    • J jsulm
                      17 Dec 2020, 06:12

                      @Christian-Ehrlicher "Don't create it on the stack but on the heap." - you should exchange stack and heap :-)

                      C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 17 Dec 2020, 06:17 last edited by
                      #15

                      @jsulm Thx, fixed

                      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
                      0
                      • C chessking5544
                        16 Dec 2020, 19:07

                        @SGaist that makes sense and I should have seen that in the docs. thanks. Will c++ smart pointers work, or is there just no way to do this? I'm looking for a way to get auto deletion for all QImages created.

                        S Offline
                        S Offline
                        SimonSchroeder
                        wrote on 18 Dec 2020, 07:59 last edited by
                        #16

                        @chessking5544 said in Cannot create QImage using smart pointers:

                        Will c++ smart pointers work, or is there just no way to do this?

                        Just for completeness I will answer this. But first I have to agree with the others that you should allocate QImage on the stack. Good C++ style avoids pointers wherever possible. The best lifetime management is variables on the stack. Containers - and in this case I will count QImage as a container - will internally allocate memory on the heap. This is a good thing as your stack is quite small. I don't know C# well (but assume it has some similarity to Java within this respect), but objects do not need to be allocated using new in C++. If you want polymorphism you can use references instead of pointers. Rarely do you actually need pointers in C++.

                        Now to the answer: C++ smart pointers work with every class. Just use it like this:

                        std::shared_ptr<QImage> image = std::make_shared<QImage>(QSize(1920,1080), QImage::Format_RGB32);
                        

                        Note the use of make_shared here. This will automatically handle out-of-memory errors. Internally it will call new and hand down its parameters to the constructor of QImage. It is advised to use make_shared<QImage>(...) over shared_ptr<QImage>(new QImage(...)) (I can't remember all the reasons).

                        1 Reply Last reply
                        2

                        15/16

                        17 Dec 2020, 06:17

                        • Login

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