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. QStandardItem pointer loses value after leaving function
Forum Updated to NodeBB v4.3 + New Features

QStandardItem pointer loses value after leaving function

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 909 Views 2 Watching
  • 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.
  • G Offline
    G Offline
    gsephelec
    wrote on last edited by
    #1

    Hi,

    I'm having an issue updating a QStandardItem pointer with a function, it updates correctly within that function but as soon as we leave the function, the value hasn't been updated in the parent function.

    I've managed to isolate the issue to a really small piece of code:

    Header:

    class testClass: public QUndoCommand
    {
    public:
        testClass(QStandardItemModel *passItemModelPtr,
                  QUndoCommand *parent = nullptr);
    
        void undo() override;
        void redo() override;
    
    private:
        QStandardItemModel *itemModelPtr;
    
        void testFunction(QStandardItem *testItem);
    };
    

    Source:

    testClass::testClass(QStandardItemModel *passItemModelPtr,
                         QUndoCommand *parent)
        : QUndoCommand(parent),
          itemModelPtr(passItemModelPtr)
    {
    }
    
    void testClass::testFunction(QStandardItem *testItem)
    {
        testItem = itemModelPtr->item(0);
    }
    
    void testClass::redo()
    {
        QStandardItem *randomItem;
    
        /* doesn't work */
        testFunction(randomItem);
    
        /* works */
        randomItem = itemModelPtr->item(0);
    }
    
    void testClass::undo()
    {
    }
    

    Called from main.cpp:

    globalUndoStack->push(new testClass(dataItemModel));
    

    The issue occurs in the testClass::redo() function, *randomItem isn't set correctly using the testFunction() function, but it works just fine on the next line: randomItem = itemModelPtr->item(0);

    Note that the QStandardItemModel I pass to the class does exist and has an item at index 0.

    If I debug the application and set a breakpoint on the line testItem = itemModelPtr->item(0); in testClass::testFunction(), I can clear see that testItem is set correctly, but once I return to redo() it hasn't updated.

    Also I checked the declration of QStandardItem *randomItem, it doesn't matter if I initialise it or not, all 3 have the same effect:

    QStandardItem *randomItem;
    QStandardItem *randomItem = nullptr;
    QStandardItem *randomItem = new QStandardItem;
    

    Any ideas what I'm doing wrong here?

    Thanks.

    Christian EhrlicherC 1 Reply Last reply
    0
    • G gsephelec

      Hi,

      I'm having an issue updating a QStandardItem pointer with a function, it updates correctly within that function but as soon as we leave the function, the value hasn't been updated in the parent function.

      I've managed to isolate the issue to a really small piece of code:

      Header:

      class testClass: public QUndoCommand
      {
      public:
          testClass(QStandardItemModel *passItemModelPtr,
                    QUndoCommand *parent = nullptr);
      
          void undo() override;
          void redo() override;
      
      private:
          QStandardItemModel *itemModelPtr;
      
          void testFunction(QStandardItem *testItem);
      };
      

      Source:

      testClass::testClass(QStandardItemModel *passItemModelPtr,
                           QUndoCommand *parent)
          : QUndoCommand(parent),
            itemModelPtr(passItemModelPtr)
      {
      }
      
      void testClass::testFunction(QStandardItem *testItem)
      {
          testItem = itemModelPtr->item(0);
      }
      
      void testClass::redo()
      {
          QStandardItem *randomItem;
      
          /* doesn't work */
          testFunction(randomItem);
      
          /* works */
          randomItem = itemModelPtr->item(0);
      }
      
      void testClass::undo()
      {
      }
      

      Called from main.cpp:

      globalUndoStack->push(new testClass(dataItemModel));
      

      The issue occurs in the testClass::redo() function, *randomItem isn't set correctly using the testFunction() function, but it works just fine on the next line: randomItem = itemModelPtr->item(0);

      Note that the QStandardItemModel I pass to the class does exist and has an item at index 0.

      If I debug the application and set a breakpoint on the line testItem = itemModelPtr->item(0); in testClass::testFunction(), I can clear see that testItem is set correctly, but once I return to redo() it hasn't updated.

      Also I checked the declration of QStandardItem *randomItem, it doesn't matter if I initialise it or not, all 3 have the same effect:

      QStandardItem *randomItem;
      QStandardItem *randomItem = nullptr;
      QStandardItem *randomItem = new QStandardItem;
      

      Any ideas what I'm doing wrong here?

      Thanks.

      Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by Christian Ehrlicher
      #2

      this is basic c++ stuff - you're passing a pointer and modifying the pointer inside this function, nothing more. It's the same as

      void func1(int a)
      {
        a = 42;
      }
      
      void func2()
      {
        int a = 4711;
        func1(a);
        // a is still 4711 here
      }
      

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

      G 1 Reply Last reply
      0
      • Christian EhrlicherC Christian Ehrlicher

        this is basic c++ stuff - you're passing a pointer and modifying the pointer inside this function, nothing more. It's the same as

        void func1(int a)
        {
          a = 42;
        }
        
        void func2()
        {
          int a = 4711;
          func1(a);
          // a is still 4711 here
        }
        
        G Offline
        G Offline
        gsephelec
        wrote on last edited by
        #3

        @Christian-Ehrlicher
        Please could you advise what I need to do to correct this? It's confusing the hell out of me.

        Bare in mind, I'm not looking to copy the value of itemModelPtr->item(0); into *randomItem;, I'm looking for *randomItem; to point to the QStandardItem * at itemModelPtr->item(0);

        Thanks.

        Christian EhrlicherC 1 Reply Last reply
        0
        • G gsephelec

          @Christian-Ehrlicher
          Please could you advise what I need to do to correct this? It's confusing the hell out of me.

          Bare in mind, I'm not looking to copy the value of itemModelPtr->item(0); into *randomItem;, I'm looking for *randomItem; to point to the QStandardItem * at itemModelPtr->item(0);

          Thanks.

          Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          Either pass a pointer or a reference of the parameter you want to modify. I would go for a reference.

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

          G 1 Reply Last reply
          3
          • Christian EhrlicherC Christian Ehrlicher

            Either pass a pointer or a reference of the parameter you want to modify. I would go for a reference.

            G Offline
            G Offline
            gsephelec
            wrote on last edited by
            #5

            @Christian-Ehrlicher
            I'm really sorry but I just can't wrap my head around this. I looked at how to pass references and changed my code to:

            void testClass::testFunction(QStandardItem &testItem)
            {
                testItem = *itemModelPtr->item(0);
            }
            

            But now it's saying

            'operator=' is a protected member of 'QStandardItem'

            Christian EhrlicherC 1 Reply Last reply
            0
            • G gsephelec

              @Christian-Ehrlicher
              I'm really sorry but I just can't wrap my head around this. I looked at how to pass references and changed my code to:

              void testClass::testFunction(QStandardItem &testItem)
              {
                  testItem = *itemModelPtr->item(0);
              }
              

              But now it's saying

              'operator=' is a protected member of 'QStandardItem'

              Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Since you want to modify a pointer than you have to pass a reference to a pointer. Basic c++ stuff and nothing Qt specific.

              void testClass::testFunction(QStandardItem *&testItem)
              {
              testitem = new QStandardItem()
              }

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

              M G 2 Replies Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                Since you want to modify a pointer than you have to pass a reference to a pointer. Basic c++ stuff and nothing Qt specific.

                void testClass::testFunction(QStandardItem *&testItem)
                {
                testitem = new QStandardItem()
                }

                M Offline
                M Offline
                mpergand
                wrote on last edited by
                #7

                Or a double pointer:

                void testClass::testFunction(QStandardItem **testItem)
                {
                    *testItem = itemModelPtr->item(0);
                }
                
                void testClass::redo()
                {
                    QStandardItem *randomItem;
                    testFunction(&randomItem);
                
                JonBJ 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  Since you want to modify a pointer than you have to pass a reference to a pointer. Basic c++ stuff and nothing Qt specific.

                  void testClass::testFunction(QStandardItem *&testItem)
                  {
                  testitem = new QStandardItem()
                  }

                  G Offline
                  G Offline
                  gsephelec
                  wrote on last edited by
                  #8

                  @Christian-Ehrlicher
                  Thank you for this, changed my code to:

                  void testClass::testFunction(QStandardItem *&testItem)
                  {
                      testItem = itemModelPtr->item(0);
                  }
                  

                  and it all works now.

                  I appreciate this might be "basic c++" but I just couldn't get my head around what I was missing.

                  Thanks for your help.

                  S 1 Reply Last reply
                  0
                  • G gsephelec has marked this topic as solved on
                  • M mpergand

                    Or a double pointer:

                    void testClass::testFunction(QStandardItem **testItem)
                    {
                        *testItem = itemModelPtr->item(0);
                    }
                    
                    void testClass::redo()
                    {
                        QStandardItem *randomItem;
                        testFunction(&randomItem);
                    
                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by JonB
                    #9

                    @mpergand said in QStandardItem pointer loses value after leaving function:

                    Or a double pointer:

                    Indeed, but now that C++ has references that is always safer, so I don't see why one would choose ** here any longer?

                    1 Reply Last reply
                    0
                    • G gsephelec

                      @Christian-Ehrlicher
                      Thank you for this, changed my code to:

                      void testClass::testFunction(QStandardItem *&testItem)
                      {
                          testItem = itemModelPtr->item(0);
                      }
                      

                      and it all works now.

                      I appreciate this might be "basic c++" but I just couldn't get my head around what I was missing.

                      Thanks for your help.

                      S Offline
                      S Offline
                      SimonSchroeder
                      wrote on last edited by
                      #10

                      @gsephelec said in QStandardItem pointer loses value after leaving function:

                      I just couldn't get my head around what I was missing.

                      Well, this is a very rare case and will likely slip through for many C++ programmers. Here is one way to think about this: QStandardItem * passes a copy of the pointer to the function. This is why you change only the copy of the pointer to point to a different object. Hence why others have suggested to use a reference or pointer to the pointer you want to modify.

                      1 Reply Last reply
                      0

                      • Login

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