QTextEdit 的内存问题
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定尝试附加到 QTextEdit 的第 i 个元素是有效的列表元素吗?使用
[]
运算符时要小心:如果
i
大于或等于list.count()
那么这可能是崩溃的根源。为了检查
QTextEdit
没有问题,您可以尝试以下操作:如果上述代码导致异常(我发现这是不可能的),请告诉我们。否则列表本身应该有问题,或更可能是处理列表的代码有问题。
编辑:有关您的代码的一些问题
如果
i==0
list[-1]
无效并且可能导致崩溃同样如果< code>i==list.count()-1
i+1
不是有效索引,因此异常的另一个原因循环遍历
QStringList
为了得到名为
list
的QStringList
的所有元素并将它们显示在QTextEdit
上,您可以执行以下操作:这会正常工作。
Are you sure the
ith
element you are trying to append to theQTextEdit
is a valid list element. Be carefuls when using the[]
operator:If
i
is greater or equal tolist.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: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
if
i==0
thelist[-1]
is invalid and a possible cause of crashSimilarly if
i==list.count()-1
thei+1
is not a valid index, so another cause of exceptionLOOPING THOUGH A
QStringList
In order to get all elements of a
QStringList
calledlist
and display them on aQTextEdit
you can do the following:This will work fine.
我在这里打开一个答案,以免评论列表超载:)
如果您确定问题不是来自
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 tostandardItems
array. Could you tell how it's initialized ? Maybe you try to access it out of its bounds