Question

I'm trying to use a QTextEdit to output a QstringList

e.g

void CTextBox::AddText(QStringList list, QStringList animList)
{
    //CGraphics* graphics = CGraphics::GetInst();

    //QStandardItem *baseItem = new StandardItem("Hello");
    //textBrowser = new QTextEdit();
    standardModel->clear();
    rootNode = standardModel->invisibleRootItem();
    treeView->setModel(standardModel);

    QString string;

    //std::string = list[i].

    QDataStream* data = new QDataStream;

    int j = 0;
    int k = 0;

    for (int i = 0; i < (list.size()); i++)
    {
        //string += list[i];
        //string += hierarchyList[i];
        // textBrowser->setText(string);
        string.append(list[i]);
        //textBrowser->append(list[i]);

        if (list[i].contains("Is Parent"))
        {
            standardItems[j] = new QStandardItem(list[i-1]);
            /*for (int k = 0; k < j; k++)
            {
                if (standardItems[j]->contains(st))
                   {
                   }
            }*/
            rootNode->appendRow(standardItems[j]);       
            //k = j;     
            j++;
        }
        else if (list[i].contains("inherits from"))
        {
            standardItems[j] = new QStandardItem(list[i-1]);
            for(k = 0; k < j; k++)
            {
                if (standardItems[k]->text() ==list[i+1])
                {
                    standardItems[k]->appendRow(standardItems[j]);
                    break;
                }
            }
            //standardItems[k]->appendRow(standardItems[j]);
            j++;            
        }
        //textBrowser->setText("Hello");
    }

    for (int i = 0; i < (animList.size()); i++)
    {
        string.append(animList[i]);
        //textBrowser->append(animList[i]);
    }

    textBrowser->setText(string);

    treeView->setModel(standardModel);
    //CGraphics* graphics = CGraphics::GetInst();
    //graphics->Render();
}

But the size of the list can be absolutely enormous up to like 1700 lines. After about 400ish appends or so I get this error message.

Unhandled exception at 0x65154715 in ipodGuiLoaderQT.exe: 0xC0000005: Access violation reading location 0xfdfdfe11.

This would usually indicate that there a memory overflow, but I can't control the memory of a textBrowser, or am I on the completely wrong track?

EDIT

I've made a small change, I'm now adding the list elements to a Qstring called string and then setting the text at the end of the loop.

e.g

textBrowser->setText(string);

I'm still having the same problem.

Was it helpful?

Solution

Are you sure the ith element you are trying to append to the QTextEdit is a valid list element. Be carefuls when using the [] operator:

Returns the item at index position i as a modifiable reference. i must be a valid index position in the list (i.e., 0 <= i < size()).

If i is greater or equal to list.count() then this is probably the root of your crash.

In order to check that there is no problem with the QTextEdit you could try the following:

for (unsigned i=0; i<50000; i++)
    textBrowser->append("Dummy String Row");

If the above code causes exception (I find it impossible) let us know. Otherwise there should be a problem with the list itself or more probably your code that handles the list.

EDIT : SOME PROBLEMS CONCERNING YOUR CODE

standardItems[j] = new QStandardItem(list[i-1]);

if i==0 the list[-1] is invalid and a possible cause of crash

if (standardItems[k]->text() ==list[i+1])

Similarly if i==list.count()-1 the i+1 is not a valid index, so another cause of exception

LOOPING THOUGH A QStringList

In order to get all elements of a QStringList called list and display them on a QTextEdit you can do the following:

for (unsigned i=0; i<list.count(); i++)
    textBrowser->append(list[i]);

This will work fine.

OTHER TIPS

I open an answer here not to overload the comment list :) If you are sure the problem does not come from the contents of list then it may be related to standardItems array. Could you tell how it's initialized ? Maybe you try to access it out of its bounds

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top