views:

666

answers:

2

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);
    }
+4  A: 

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);
}
Jesse
@Jesse It looks as though this worked great. I'm assuming I should read the QStrings back into another array and then copy back over? Unless there's a more simple approach
bobby
Correct. I have updated my answer for reading back
Jesse
Awesome. The only thing I did different was iterate through the existing QList<QLineEdit*> and have the values loaded into it instead of clear()'d and re-appended.
bobby
+1  A: 

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;
  }
}
TimW