Hi to all!
I've read the docs about QList but I did not find any answers regarding my question. So, after QList method http://doc.trolltech.com/4.3/qlist.html#append, the record is not added to QList. What are possible reasons?
Hi to all!
I've read the docs about QList but I did not find any answers regarding my question. So, after QList method http://doc.trolltech.com/4.3/qlist.html#append, the record is not added to QList. What are possible reasons?
Qt 5.3 Opensource & Creator 3.1.2
Can you show a code example?
Post part of its code, but you can also use the following:
Qt Code:
QList<QString> list; list << "One" << "Two" << "Three" << "Four";To copy to clipboard, switch view to plain text mode
Marcelo E. Geyer
This is my code:Qt Code:
m_pShoppingCartView->order()->orders().append(tmpOrder); // adds order to internal qlist int iOrderSize=m_pShoppingCartView->order()->orders().size(); // calcs size Q_ASSERT_X(iOrderSize>0, "COperationWindow.cpp", "m_pShoppingCartView->order()->orders().size()<0"); // checks sizeTo copy to clipboard, switch view to plain text mode
The application aborts on Q_ASSERT_X.
Qt 5.3 Opensource & Creator 3.1.2
Doesn't by any chance orders() return a copy of the list and not a reference?
Well, inline function order returns pointer:m_pOrder is declared as:Qt Code:
inline CMerchandizeOrder* order() { return m_pOrder; };To copy to clipboard, switch view to plain text modeand then inline member function orders() returns:Qt Code:
CMerchandizeOrder* m_pOrder;To copy to clipboard, switch view to plain text modeand m_Orders are defined as:Qt Code:
inline QList<structOrder> orders() { return m_Orders; };To copy to clipboard, switch view to plain text modeQt Code:
private: QList<structOrder> m_Orders;To copy to clipboard, switch view to plain text mode
Qt 5.3 Opensource & Creator 3.1.2
Well, how do I recode this code so it will work? I do not know how to make a pointer to QList
Qt 5.3 Opensource & Creator 3.1.2
Instead of
Qt Code:
private: QList<structOrder> m_Orders;To copy to clipboard, switch view to plain text mode
use
Qt Code:
private: QList<structOrder>* m_Orders;To copy to clipboard, switch view to plain text mode
The are certain key things you need to be aware of with C++.
One very important thing is that when you call return at the end of the function, a COPY of the type you're returning is created to hold the data. This is because any variable declared inside a function is a STACK variable for the scope of that function. So if you declare a non pointer variable then return it, in your case in the call to orders(), you've essentially chained your call to .append(tmpOrder) on a copy of the variable that is lost as soon as the next line is called.
Now by declaring a QList<structOrder>*, at the end of your function you'll be returning a COPY still, but it will be a copy of a POINTER. The actual instantiated QList is on the heap so isn't cleaned up at the end of the function, therefore copied QList*'s data you'll be appending to is valid and won't get lost in transit.
In terms of architecture, you might want to re-consider this kind of thing anyway and use a wrapper function. A sideline of hiding the QList also is that you could schtick with your non pointer version:
Qt Code:
void ShoppingCartView::AddOrder(structOrder order) { m_Orders.append( order ); }To copy to clipboard, switch view to plain text mode
Last edited by stevey; 8th May 2008 at 23:20.
Actually he doesn't need a pointer to the list. He needs to return a reference (or pointer) to the list:
Qt Code:
inline QList<structOrder> &orders() const { return m_Orders; };To copy to clipboard, switch view to plain text mode
or
Qt Code:
inline QList<structOrder> *orders() const { return &m_Orders; };To copy to clipboard, switch view to plain text mode
Of course hiding the implementation detail and exposing a method for adding items to the list as suggested is a much better idea.
But how do I create a pointer to QList???
The codereturns an compile error.Qt Code:
Q_CHECK_PTR(m_pOrders); // checks creationTo copy to clipboard, switch view to plain text mode
And I need this info urgently please, since I have a demo of application and the app won't compile neither run because i cannot add items to a non created list!!!!![]()
Qt 5.3 Opensource & Creator 3.1.2
Bookmarks