保存 QList到一个文件?

发布于 2024-08-11 12:39:26 字数 751 浏览 11 评论 0原文

我有一个 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 技术交流群。

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

发布评论

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

评论(2

櫻之舞 2024-08-18 12:39:26

QList 是一个指针列表(基本上是整数,因此如果将其写入文件,您将不会获得太多有用的信息。

text() 方法应该做你正在寻找的事情

foreach( const QLineEdit* le, example )
{
  if( le )
  {
     ds << le->text();
  }
}

文本

如果

您只使用字符串, QTextStream 类使用起来更好一些(也可以在上面使用,而不是使用QDataStream ...为了保持一致,尽管您应该使用相同类型的流进行读取和写入),但我目前无法测试此代码,但您可以尝试如下操作:

QList<QLineEdit*> example;
while(!stream.atEnd())
{
   QString str;
   stream >> str;
   if( stream.isNull() )
     break;
   QLineEdit* le = new QLineEdit();
   le->setText(str);
   example.append(le);
}

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.

foreach( const QLineEdit* le, example )
{
  if( le )
  {
     ds << le->text();
  }
}

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:

QList<QLineEdit*> example;
while(!stream.atEnd())
{
   QString str;
   stream >> str;
   if( stream.isNull() )
     break;
   QLineEdit* le = new QLineEdit();
   le->setText(str);
   example.append(le);
}
不再让梦枯萎 2024-08-18 12:39:26

字符串对象不是由流分配的。
使用 QString 对象而不是指针。

//memoryAddresses
for(int i = 0; i < 100; i++)
{
    QString temp;
    loadFile >> temp;
    memAddr.at(i)->setText(temp);
}

或者您可以编写自己的 QLineEdit 流函数

QDataStream& operator<<(QDataStream& stream, const QLineEdit& lineEdit )
{
    const qint32 version(0);
    stream << version;
    stream << lineEdit.text();
    //... whatever you want from the lineedit
}

QDataStream& operator>>(QDataStream& stream, const QLineEdit& lineEdit )
{
    qint32 version(0);
    QString text;
    stream >> version;
    Q_ASSERT_X(version, __FUNCSIG__, "Wrong line edit version");
    stream >> text;
    lineEdit.setText(text);
    //... whatever you want from the stream
}


// write to stream
foreach( const QLineEdit* le, example )
{
  if( le )
  {
     ds << *le;
  }
}

// read from stream
foreach( const QLineEdit* le, example )
{
  if( le )
  {
     ds >> *le;
  }
}

The string object isn't allocated by the stream.
Use a QString object instead of a pointer.

//memoryAddresses
for(int i = 0; i < 100; i++)
{
    QString temp;
    loadFile >> temp;
    memAddr.at(i)->setText(temp);
}

Or you can write your own QLineEdit streaming functions

QDataStream& operator<<(QDataStream& stream, const QLineEdit& lineEdit )
{
    const qint32 version(0);
    stream << version;
    stream << lineEdit.text();
    //... whatever you want from the lineedit
}

QDataStream& operator>>(QDataStream& stream, const QLineEdit& lineEdit )
{
    qint32 version(0);
    QString text;
    stream >> version;
    Q_ASSERT_X(version, __FUNCSIG__, "Wrong line edit version");
    stream >> text;
    lineEdit.setText(text);
    //... whatever you want from the stream
}


// write to stream
foreach( const QLineEdit* le, example )
{
  if( le )
  {
     ds << *le;
  }
}

// read from stream
foreach( const QLineEdit* le, example )
{
  if( le )
  {
     ds >> *le;
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文