QStandardItem pointer loses value after leaving function
-
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,*randomItemisn't set correctly using thetestFunction()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);intestClass::testFunction(), I can clear see thattestItemis set correctly, but once I return toredo()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.
-
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,*randomItemisn't set correctly using thetestFunction()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);intestClass::testFunction(), I can clear see thattestItemis set correctly, but once I return toredo()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.
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 } -
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 }@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 theQStandardItem *atitemModelPtr->item(0);Thanks.
-
@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 theQStandardItem *atitemModelPtr->item(0);Thanks.
Either pass a pointer or a reference of the parameter you want to modify. I would go for a reference.
-
Either pass a pointer or a reference of the parameter you want to modify. I would go for a reference.
@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-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'
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()
} -
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()
} -
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()
}@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.
-
G gsephelec has marked this topic as solved on
-
Or a double pointer:
void testClass::testFunction(QStandardItem **testItem) { *testItem = itemModelPtr->item(0); } void testClass::redo() { QStandardItem *randomItem; testFunction(&randomItem); -
@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.
@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.