保存 QList到一个文件?
我有一个 QList 的 QLineEdit* 示例
QList<QLineEdit*> example;
将包含 100 个 lineEdits 项目。
当我尝试保存或加载文件时,它无法正确保存或加载QList
(如果有的话)。我得到的数据数量远低于预期。
我在 QList
的资源页面上看到 这里有 << 的正确运算符& >>, 但是我似乎无法使用 QDataStream 将它们保存到文件中,
我也尝试将 LineEdits 中的所有“text()”值复制到单独的字符串列表中,但我仍然可以' t 将它们保存到文件中。任何帮助将不胜感激。
编辑:看起来就是这样。这就是我读回它们的方式,是否有更简单的方法,或者我已经涵盖了它?
//memoryAddresses
for(int i = 0; i < 100; i++)
{
QString temp;
loadFile >> temp;
memAddr.at(i)->setText(temp);
}
I've got a QList of QLineEdit*'s
QList<QLineEdit*> example;
Example will hold 100 items of lineEdits.
When I try to save or load to a file, it fails to save or load the QList
properly, if at all. I get a much lower than expected count of data.
I see on QList<T>
's resource page here that there's the correct operator for << & >>,
however I can't seem to get them to save to a file using QDataStream
I've also tried copying all the "text()" values from the LineEdits into a seperate String List but I still can't save them to a file. Any help would be appreciated.
EDIT: Looks like that did it. This is how I'm reading them back, is there a more simple approach to this, or have I pretty much covered it?
//memoryAddresses
for(int i = 0; i < 100; i++)
{
QString temp;
loadFile >> temp;
memAddr.at(i)->setText(temp);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
QList
是一个指针列表(基本上是整数,因此如果将其写入文件,您将不会获得太多有用的信息。text()
方法应该做你正在寻找的事情。 文本:
如果
您只使用字符串, QTextStream 类使用起来更好一些(也可以在上面使用,而不是使用QDataStream ...为了保持一致,尽管您应该使用相同类型的流进行读取和写入),但我目前无法测试此代码,但您可以尝试如下操作:
QList<QLineEdit*>
is a list of pointers (basically ints so if you write that to a file you won't get much useful information.The
text()
method should do what you are looking for.Note the differences between displayText and text.
To read back:
If you are only working with strings, the QTextStream class is a little nicer to use (could also be used above rather than the QDataStream...to be consistent though you should use the same type of stream for reading and writing). I am not able to test this code at the moment but you can try something like this:
字符串对象不是由流分配的。
使用 QString 对象而不是指针。
或者您可以编写自己的 QLineEdit 流函数
The string object isn't allocated by the stream.
Use a QString object instead of a pointer.
Or you can write your own QLineEdit streaming functions