Results 1 to 9 of 9

Thread: QList question

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default QList question

    Does the insertion operator make a copy of the value it is inserting?

    For example... Lets say I have the following code...

    Qt Code:
    1. struct someStruct {
    2. int x;
    3. double y;
    4. };
    5.  
    6. QList<someStruct> testList;
    7.  
    8. void className::methodName()
    9. {
    10. someStruct tempStruct;
    11. for (int x = 0; x < 10; x++) {
    12. tempStruct.x = x;
    13. tempStruct.y = 0;
    14. testList <<tempStruct;
    15. }
    16. }
    To copy to clipboard, switch view to plain text mode 

    tempStruct is local to the "methodName" method.
    After this function returns, is testList valid?
    In my test code it is. All works ok. But I'm trying to track down a particularly elusive bug and this is one of my concerns.

    Here's a snippet of my actual code. I'll try to explain the error.

    Qt Code:
    1. LayerDef layer; // LayerDef is the struct definition
    2. if (line.startsWith("Original")) {
    3. while (!line.startsWith(',')) {
    4. line = stream.readLine();
    5. if (line.startsWith(',')){
    6. continue;
    7. }
    8. tokens = line.split(',');
    9. layer.t = tokens[1].toDouble(); // layer is a struct.
    10. layer.dc = tokens[2].toDouble();
    11. layer.dlt = tokens[3].toDouble();
    12. layer.mlt = tokens[5].toDouble();
    13. layer.dcc = tokens[6].toDouble();
    14. layer.rp = tokens[7].toDouble();
    15. layer.name = tokens[8];
    16. layersOriginal <<layer; // layersOriginal is a QList (QList<LayerDef> layersOriginal;)
    17. if (firstPass) {
    18. firstPass = false;
    19. // Update slider values on first pass only.
    20. ui->sldT->setValue(layer.t); // Note this line.
    21. ui->sldDC->setValue(layer.dc);
    22. ui->sldDLT->setValue(layer.dlt);
    23. ui->sldMLT->setValue(layer.mlt);
    24. ui->sldDCC->setValue(layer.dcc);
    25. ui->sldRP->setValue(layer.rp);
    26. }
    27. ui->lstLayers->addItem(layer.name);
    28. }
    To copy to clipboard, switch view to plain text mode 

    This code runs ok. However, after it runs, any attempt to access sldT (a QSlider) causes an assertion failure.
    The failure is "ASSERT failure in QVector<T>:perator[]: "index out of range", file ..\..\Qt\Qt5.3.2\5.3\mingw482_32\include/QtCore/qvector.h, line 385".
    I've went through the code for QSlider and QAbstractSlider. I can't find any reference to a QVector in either of them.

    This is my final step before I start banging my head on my desk.

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

    Default Re: QList question

    A list keeps a copy of your original object.

    As for your error, what is the type of tokens? Isn't it by any chance QVector? What does tokens.size() return?
    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.


  3. #3
    Join Date
    Oct 2014
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList question

    Sorry... I should've mentioned that.

    line is a QString and tokens is a QStringList.

    I'm reading a spreadsheet and splitting the line by the commas.

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

    Default Re: QList question

    Why do you say the problem is with some slider? You are not running this code from a worker thread, are you?
    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
    Oct 2014
    Posts
    7
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: QList question

    No. This code is in the UI thread.

    That's what is confusing me. I can't figure out why the slider is crashing. It works fine before this code runs.
    I've worked on this for two days now. My best guess is that some code or memory is getting corrupted somehow.

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

    Default Re: QList question

    So if you comment out lines #17-26 then some other code stops crashing?
    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.


Similar Threads

  1. QList Question
    By giblit in forum Qt Programming
    Replies: 2
    Last Post: 4th April 2013, 02:41
  2. QList<QList > question
    By SubV in forum Qt Programming
    Replies: 3
    Last Post: 25th July 2008, 16:14
  3. QList question
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 23rd January 2008, 08:51
  4. QList question
    By aamer4yu in forum Qt Programming
    Replies: 4
    Last Post: 2nd April 2007, 14:43
  5. QList question
    By ln\ in forum Newbie
    Replies: 2
    Last Post: 19th December 2006, 14:01

Tags for this Thread

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.