QTextEdit 的内存问题

发布于 2024-12-16 21:53:28 字数 2412 浏览 4 评论 0原文

我正在尝试使用 QTextEdit 输出 QstringList

例如,

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();
}

但列表的大小可能非常巨大,最多可达 1700 行。大约 400 左右的附加后,我收到此错误消息。

ipodGuiLoaderQT.exe 中 0x65154715 处未处理的异常:0xC0000005: 访问冲突读取位置 0xfdfdfe11。

这通常表明存在内存溢出,但我无法控制文本浏览器的内存,或者我是否走在完全错误的轨道上?

编辑

我做了一个小更改,我现在将列表元素添加到名为 string 的 Qstring 中,然后在循环末尾设置文本。

例如

textBrowser->setText(string);

我仍然遇到同样的问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风为裳 2024-12-23 21:53:28

您确定尝试附加到 QTextEdit 的第 i 个元素是有效的列表元素吗?使用 [] 运算符时要小心:

返回索引位置 i 处的项目作为可修改的引用。我必须
是列表中的有效索引位置(即,0 <= i < size())。

如果 i 大于或等于 list.count() 那么这可能是崩溃的根源。

为了检查QTextEdit没有问题,您可以尝试以下操作:

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

如果上述代码导致异常(我发现这是不可能的),请告诉我们。否则列表本身应该有问题,或更可能是处理列表的代码有问题。

编辑:有关您的代码的一些问题

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

如果i==0 list[-1] 无效并且可能导致崩溃

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

同样如果< code>i==list.count()-1 i+1 不是有效索引,因此异常的另一个原因

循环遍历 QStringList

为了得到名为 listQStringList 的所有元素并将它们显示在 QTextEdit 上,您可以执行以下操作:

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

这会正常工作。

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.

白色秋天 2024-12-23 21:53:28

我在这里打开一个答案,以免评论列表超载:)
如果您确定问题不是来自 list 的内容,那么它可能与 standardItems 数组有关。你能告诉我它是如何初始化的吗?也许您尝试越界访问它

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文