Results 1 to 13 of 13

Thread: QList += operator

  1. #1
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QList += operator

    i got error when try :

    Qt Code:
    1. QList<double> zz;
    2. double r=.05;
    3. for (int k = 0; k < 3; ++k)
    4. {
    5. zz[0] +=r;
    6.  
    7. }
    To copy to clipboard, switch view to plain text mode 
    how to solve this
    thanks

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QList += operator

    In general it would be helpfull if you would actually paste the error message!

    As far as I can see, you have to fill the list first. [] can only be used if that index allready exists.

  3. #3
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList += operator

    you have to fill the list first. []
    yes i did that by :
    zz<<0.0<<0.0<<0.0<<0.0
    but that is idiot

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

    Default Re: QList += operator

    What (who?) is idiot?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList += operator

    There is no need to inquire not my nature abuse literature with one .
    I meant the Behavior of the QList
    that is all

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

    Default Re: QList += operator

    What behaviour of QList? That you can't operate on items you don't have? Every list works this way. It's a list with finite number of elements.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList += operator

    but in c# for example you don't obligate to initialize initial values :
    doble zz=3;
    double[] foo = new double[4];
    for (int k = 0; k <10; ++k)
    {
    foo[0] += zz;


    }
    this is similar to QList and works fluently without any initialization

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

    Default Re: QList += operator

    No, you are wrong. This is the initialization of the array:
    Qt Code:
    1. double[] foo = new double[4];
    To copy to clipboard, switch view to plain text mode 
    You explicitly set the array size to four elements filled with possibly random data.

    The equivalent for Qt (with the difference that each cell is initialized to 0.0) is:
    Qt Code:
    1. QVector<double> foo(4);
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  9. The following user says thank you to wysota for this useful post:

    alrawab (12th January 2013)

  10. #9
    Join Date
    May 2008
    Location
    Tripoli Libya
    Posts
    70
    Thanks
    10
    Thanked 8 Times in 8 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QList += operator

    yes but this not applicable For QList as i know ?
    is there any replacement for zz<<0.0<<0.0<<0.0<<0.0;
    it's looks ugly

  11. #10
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QList += operator

    use QVector
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  12. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QList += operator

    Quote Originally Posted by alrawab View Post
    yes but this not applicable For QList as i know ?
    It's a list. Initializing it like I did with a vector is slow and unnecessary.

    is there any replacement for zz<<0.0<<0.0<<0.0<<0.0;
    e.g.
    Qt Code:
    1. for(int i=0;i<4;++i) zz.append(0);
    To copy to clipboard, switch view to plain text mode 

    By the way, if you know your array of doubles is going to be always of size 4, use a regular C array instead.

    Qt Code:
    1. double zz[4];
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  13. #12
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QList += operator

    If the object is to always have a fixed number of elements then you have better choices than QList as others have pointed out. If your QList must start with a certain number of elements but may grow, and you really find the simple initialisation loop so distasteful, then I guess you can use QVector to initialise your QList:
    Qt Code:
    1. QList<double> stuff = QList<double>::fromVector(QVector<double>(4, 0.0));
    To copy to clipboard, switch view to plain text mode 
    I don't think this is an improvement over the other options. Ultimately expecting C++ and Qt to behave like C# and its library is like tilting at windmills.

  14. The following user says thank you to ChrisW67 for this useful post:

    alrawab (14th January 2013)

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

    Default Re: QList += operator

    With C++11 and Qt 5 one can also use initializer lists.

    Qt Code:
    1. QList<double> xxx = { 0.0, 0.0, 0.0, 0.0 }; // with C++11 enabled
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  16. The following user says thank you to wysota for this useful post:

    alrawab (14th January 2013)

Similar Threads

  1. how to confirm the value of one QList in other QList quickly?
    By weixj2003ld in forum Qt Programming
    Replies: 5
    Last Post: 8th June 2012, 20:48
  2. QList<mypointer *>::contains and operator== issue
    By jamo_ in forum Qt Programming
    Replies: 5
    Last Post: 10th May 2011, 11:44
  3. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  4. Replies: 4
    Last Post: 20th August 2010, 13:54
  5. QList Overloading operator==()
    By josepvr in forum Qt Programming
    Replies: 8
    Last Post: 28th January 2009, 15:28

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.