Results 1 to 19 of 19

Thread: QList question

  1. #1
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Angry QList question

    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

  2. #2
    Join Date
    Apr 2008
    Posts
    104
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 7 Times in 7 Posts

    Default Re: QList question

    Can you show a code example?

  3. #3
    Join Date
    Jan 2008
    Location
    Brasil
    Posts
    131
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    18
    Thanked 3 Times in 3 Posts

    Lightbulb Re: QList question

    Post part of its code, but you can also use the following:

    Qt Code:
    1. QList<QString> list;
    2. list << "One" << "Two" << "Three" << "Four";
    To copy to clipboard, switch view to plain text mode 

    Marcelo E. Geyer

  4. #4
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    This is my code:
    Qt Code:
    1. m_pShoppingCartView->order()->orders().append(tmpOrder); // adds order to internal qlist
    2. int iOrderSize=m_pShoppingCartView->order()->orders().size(); // calcs size
    3. Q_ASSERT_X(iOrderSize>0, "COperationWindow.cpp", "m_pShoppingCartView->order()->orders().size()<0"); // checks size
    To copy to clipboard, switch view to plain text mode 

    The application aborts on Q_ASSERT_X.
    Qt 5.3 Opensource & Creator 3.1.2

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QList question

    Doesn't by any chance orders() return a copy of the list and not a reference?

  6. #6
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    Well, inline function order returns pointer:
    Qt Code:
    1. inline CMerchandizeOrder* order()
    2. { return m_pOrder; };
    To copy to clipboard, switch view to plain text mode 
    m_pOrder is declared as:
    Qt Code:
    1. CMerchandizeOrder* m_pOrder;
    To copy to clipboard, switch view to plain text mode 
    and then inline member function orders() returns:
    Qt Code:
    1. inline QList<structOrder> orders()
    2. { return m_Orders; };
    To copy to clipboard, switch view to plain text mode 
    and m_Orders are defined as:
    Qt Code:
    1. private:
    2. QList<structOrder> m_Orders;
    To copy to clipboard, switch view to plain text mode 
    Qt 5.3 Opensource & Creator 3.1.2

  7. #7
    Join Date
    Jan 2006
    Location
    Bremen, Germany
    Posts
    554
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanked 86 Times in 81 Posts

    Default Re: QList question

    Quote Originally Posted by MarkoSan View Post
    Qt Code:
    1. inline QList<structOrder> orders()
    2. { return m_Orders; };
    To copy to clipboard, switch view to plain text mode 
    Here you return a copy of the List.

  8. #8
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    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

  9. #9
    Join Date
    Mar 2006
    Posts
    140
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    8
    Thanked 4 Times in 3 Posts

    Default Re: QList question

    Instead of
    Qt Code:
    1. private:
    2. QList<structOrder> m_Orders;
    To copy to clipboard, switch view to plain text mode 

    use
    Qt Code:
    1. private:
    2. 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:
    1. void ShoppingCartView::AddOrder(structOrder order)
    2. {
    3. m_Orders.append( order );
    4. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by stevey; 8th May 2008 at 23:20.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QList question

    Actually he doesn't need a pointer to the list. He needs to return a reference (or pointer) to the list:

    Qt Code:
    1. inline QList<structOrder> &orders() const { return m_Orders; };
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. 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.

  11. #11
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    But how do I create a pointer to QList???

    The code
    Qt Code:
    1. m_pOrders=new QList(); // creates new qlist
    2. Q_CHECK_PTR(m_pOrders); // checks creation
    To copy to clipboard, switch view to plain text mode 
    returns an compile error.

    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

  12. #12
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    Ok, I've managed to do it. I forgot to pass the type of qlist. Now let me check some things I will report the progress. Thanks for all!
    Qt 5.3 Opensource & Creator 3.1.2

  13. #13
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: QList question

    You don't need to create the list on the heap. Simply return a pointer to the list by using the ampersand.

  14. #14
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    Hello, let me continue this thread of mine after 3 years!

    I am trying to implement wrapper around QList and when trying to compile following code:
    Qt Code:
    1. #ifndef BSPITEM_H
    2. #define BSPITEM_H
    3.  
    4. #include <QObject>
    5. #include <QtCore/QList>
    6.  
    7. #include "BSPItemProperty.h"
    8.  
    9. class BSPItem : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. private:
    14. QList<BSPItemProperty> m_lsProperties;
    15.  
    16. public:
    17. explicit BSPItem(QObject *parent = 0);
    18.  
    19. inline QList<BSPItemProperty>& properties()
    20. { return this->m_lsProperties; }
    21.  
    22. void addProperty(const QString& name,
    23. const QVariant& value);
    24. void deleteProperty(const QString& name);
    25. };
    26.  
    27. #endif // BSPITEM_H
    To copy to clipboard, switch view to plain text mode 
    and its implementation - .cpp file:
    Qt Code:
    1. #include "bspitem.h"
    2.  
    3. BSPItem::BSPItem(QObject *parent) :
    4. QObject(parent)
    5. {
    6. this->properties().clear();
    7. }
    8.  
    9. void BSPItem::addProperty(const QString& name,
    10. const QVariant& value)
    11. {
    12. BSPItemProperty tempProperty(this,
    13. name,
    14. value);
    15.  
    16.  
    17. this->properties().append(tempProperty);
    18. }
    19.  
    20. void BSPItem::deleteProperty(const QString& name)
    21. {
    22. for(quint64 iIndex=0; iIndex<(quint64)this->properties().size(); iIndex++)
    23. if(this->properties().at(iIndex).propertyName()==name)
    24. this->deleteProperty(name);
    25. }
    To copy to clipboard, switch view to plain text mode 
    I get following errors:
    Running build steps for project QtBSPPlayer...
    Configuration unchanged, skipping qmake step.
    Starting: "C:\QtSDK\mingw\bin\mingw32-make.exe" -w
    mingw32-make: Entering directory `C:/Users/HP2/Desktop/QtBSPPlayer-build-desktop'
    C:/QtSDK/mingw/bin/mingw32-make -f Makefile.Debug
    mingw32-make[1]: Entering directory `C:/Users/HP2/Desktop/QtBSPPlayer-build-desktop'
    g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_HAVE_MMX -DQT_HAVE_3DNOW -DQT_HAVE_SSE -DQT_HAVE_MMXEXT -DQT_HAVE_SSE2 -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include \QtCore" -I"..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include \QtGui" -I"..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include " -I"..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include \ActiveQt" -I"debug" -I"..\QtBSPPlayer" -I"." -I"..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\mkspecs \win32-g++" -o debug\bspitem.o ..\QtBSPPlayer\bspitem.cpp
    mingw32-make[1]: Leaving directory `C:/Users/HP2/Desktop/QtBSPPlayer-build-desktop'
    mingw32-make: Leaving directory `C:/Users/HP2/Desktop/QtBSPPlayer-build-desktop'
    In file included from ..\QtBSPPlayer\/bspitem.h:7,
    from ..\QtBSPPlayer\bspitem.cpp:1:
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/qobject.h: In copy constructor 'BSPItemProperty::BSPItemProperty(const BSPItemProperty&)':
    ..\QtBSPPlayer\/BSPItemProperty.h:9: instantiated from 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = BSPItemProperty]'
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include/QtCore/qlist.h:499: instantiated from 'void QList<T>::append(const T&) [with T = BSPItemProperty]'
    ..\QtBSPPlayer\bspitem.cpp:17: instantiated from here
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/qobject.h:309: error: 'QObject::QObject(const QObject&)' is private
    ..\QtBSPPlayer\/BSPItemProperty.h:9: error: within this context
    In file included from ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/qobject.h:50,
    from ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/QObject:1,
    from ..\QtBSPPlayer\/bspitem.h:4,
    from ..\QtBSPPlayer\bspitem.cpp:1:
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include/QtCore/qlist.h: In member function 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = BSPItemProperty]':
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include/QtCore/qlist.h:359: note: synthesized method 'BSPItemProperty::BSPItemProperty(const BSPItemProperty&)' first required here
    In file included from ..\QtBSPPlayer\/bspitem.h:7,
    from ..\QtBSPPlayer\bspitem.cpp:1:
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/qobject.h: In member function 'BSPItemProperty& BSPItemProperty::operator=(const BSPItemProperty&)':
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/qobject.h:309: error: 'QObject& QObject::operator=(const QObject&)' is private
    ..\QtBSPPlayer\/BSPItemProperty.h:9: error: within this context
    In file included from ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/qobject.h:50,
    from ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/QObject:1,
    from ..\QtBSPPlayer\/bspitem.h:4,
    from ..\QtBSPPlayer\bspitem.cpp:1:
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include/QtCore/qlist.h: In member function 'void QList<T>::node_construct(QList<T>::Node*, const T&) [with T = BSPItemProperty]':
    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include/QtCore/qlist.h:365: note: synthesized method 'BSPItemProperty& BSPItemProperty::operator=(const BSPItemProperty&)' first required here
    mingw32-make[1]: *** [debug/bspitem.o] Error 1
    mingw32-make: *** [debug] Error 2
    The process "C:\QtSDK\mingw\bin\mingw32-make.exe" exited with code 2.
    Error while building project QtBSPPlayer (target: Desktop)
    When executing build step 'Make'
    What is wrong??
    Qt 5.3 Opensource & Creator 3.1.2

  15. #15
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    11
    Thanked 16 Times in 16 Posts

    Default Re: QList question

    ..\..\..\..\QtSDK\Desktop\Qt\4.7.1\mingw\include\Q tCore/qobject.h:309: error: 'QObject::QObject(const QObject&)' is private
    ..\QtBSPPlayer\/BSPItemProperty.h:9: error: within this context
    Are you trying to make a copy of a qobject?

  16. #16
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    Quote Originally Posted by nightghost View Post
    Are you trying to make a copy of a qobject?
    I do not think so, but if I do, then I do not know where. If I comment the core of methods addProperty() - append method generates errors and in constructor, clear method generates errors and I simply do not know why. Does these two methods use copy constructor internally??
    Qt 5.3 Opensource & Creator 3.1.2

  17. #17
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    131
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    11
    Thanked 16 Times in 16 Posts

    Default Re: QList question

    I assume that append creates a copy of the appened object, as well as functions like value

  18. #18
    Join Date
    Jan 2006
    Location
    Ljubljana
    Posts
    687
    Qt products
    Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Android
    Thanks
    111
    Thanked 4 Times in 4 Posts

    Default Re: QList question

    Quote Originally Posted by nightghost View Post
    I assume that append creates a copy of the appened object, as well as functions like value
    Well, so what do I do now to get rid of these errors
    Qt 5.3 Opensource & Creator 3.1.2

  19. #19
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: QList question

    define a copy constructor in you BP item class

Similar Threads

  1. Sorting using qSort(), - if QList contains POINTERS
    By joseph in forum Qt Programming
    Replies: 13
    Last Post: 18th August 2013, 18:55
  2. Interface methods question
    By sincnarf in forum Qt Programming
    Replies: 3
    Last Post: 5th October 2007, 10:20
  3. QList
    By dragon in forum Qt Programming
    Replies: 11
    Last Post: 9th May 2007, 20:15
  4. QList question
    By aamer4yu in forum Qt Programming
    Replies: 4
    Last Post: 2nd April 2007, 13:43
  5. Accessing QList Objects
    By magikalpnoi in forum Qt Programming
    Replies: 7
    Last Post: 21st September 2006, 20:43

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.