Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QDatastream should read a QVector from File.



  • Hi,

    I have a problem, which is occuring just with more data.

    Saved as:

    QDataStream out(&file);
     out.setVersion(QDataStream::Qt_4_5);
     out << xDataVector <<yDataVector;
     file.close();
    

    Loaded with:

    QDataStream in(&file);
       in.setVersion(QDataStream::Qt_4_5);
       in>>xDataVector>>yDataVector;
       file.close();
       qDebug()<<"FillVector"<<xDataVector.size()<<yDataVector.size();
       for(int iSamples=0;iSamples<xDataVector.size();iSamples++)
       {
           _sample.setSample(xDataVector.at(iSamples),yDataVector.at(iSamples));
           loadAllSampleVector.append(_sample);
       }
    

    If the vector is smaller than 1000 Values its no problem, if he is quite big like 2549 Values he crashes.

    the qDebug is showing an amount of data for the smaller vectors, the bigger ones are just 0.

    Does somebody have an idea why? Does it need to much time to load? How can I let the thread wait to finish the load event?



  • Hi! Can you provide a minimal example that reproduces the error? The following works for me:

    #include <QApplication>
    #include <QVector>
    #include <QDataStream>
    #include <QFile>
    #include <QDebug>
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        auto const s = 10000000L;
        auto const path = QStringLiteral("/path/to/file");
    
        {
            QVector<int> x, y;
            x.resize(s);
            y.resize(s);
    
            QFile f(path);
            if (!f.open(QIODevice::WriteOnly))
                return -1;
            QDataStream ds(&f);
            ds.setVersion(QDataStream::Qt_4_5);
            ds << x << y;
        }
    
        {
            QVector<int> x, y;
    
            QFile f(path);
            if (!f.open(QIODevice::ReadOnly))
                return -1;
            QDataStream ds(&f);
            ds.setVersion(QDataStream::Qt_4_5);
            ds >> x >> y;
    
            qDebug() << x.size() << ", " << y.size();
        }
    
        return 0;
    }
    


  • Thanks @Wieland

    You're example is working for me as well. But mine does not work, so it is not about the speed of this operation I guess.

    I have quite a large code, so I try to show the important parts.:

    QFile file(*loadfilename);
      if(!file.open(QIODevice::ReadOnly | QIODevice::Text))
      {
          QMessageBox::information(_pMainWindow, tr("Problem loading the Measurement"), tr("Error,")+file.errorString());
          return false;
      }
      QString line;
      QTextStream input(&file);
    ....
      while((line = input.readLine()) != "[Samples]")
      {
          if(line == "[Metadata]")
          {
              MarkerButtonIsActive = false;
              ConcentrationStateIsActive = false;
              ThresholdChangesIsActive = false;
              NotesIsActive = false;
    
              MetadataIsActive = true;
    
              continue;
    ...
          }
        ...........
    
         ......
          if(NotesIsActive)
          {
            _pMainUi->NotesTextEdit->append(line);
          }
      }
    //loadAllSampleVector.clear();
    //xDataVector.clear();
    //yDataVector.clear();
      QDataStream in(&file);
      in.setVersion(QDataStream::Qt_4_5);
      QVector<int>x,y;
      in<<x<<y;
    //    in>>xDataVector>>yDataVector;
    
    
      file.close();
      qDebug()<<"FillVector"<<xDataVector.size()<<yDataVector.size()<<x.size()<<y.size();
      for(int iSamples=0;iSamples<xDataVector.size();iSamples++)
      {
          _sample.setSample(xDataVector.at(iSamples),yDataVector.at(iSamples));
          loadAllSampleVector.append(_sample);
      }
      qDebug()<<"FilledVector";
    
      return true;
    

    Output

    FillVector 0 0 0 0
    FilledVector
    

    If i take a look in the file there is:

    [Samples]
      
    Ä?ð      @       @      @      @      @      @      @       @"      @$      @&      @(      @*      @,      @.      @0      @1      @2      @3      @4      @5      @6      @7      @8      @9      @:      @;      @<      @=      @>      @?      @@      @@€     @A      @A€     @B      @B€     @C      @C€     @D      @D€     @E      @E€     @F      @F€     @G      @G€     @H      @H€     @I      @I€     @J      @J€     @K      @K€     @L      @L€     @M      @M€     @N      @N€     @O      @O€     @P      @P@     @P€     @PÀ     @Q      @Q@     @Q€     @QÀ     @R      @R@     @R€     @RÀ     @S      @S@     @S€     @SÀ     @T      @T@     @T€     @TÀ     @U      @U@     @U€     @UÀ     @V      @V@     @V€     @VÀ     @W      @W@     @W€     @WÀ     @X      @X@     @X€     @XÀ     @Y      @Y@     @Y€     @YÀ     @Z      @Z@     @Z€     @ZÀ     @[      @[@     @[€     @[À     @\      @\@     @\€     @\À     @]      @]@     @]€     @]À     @^      @^@     @^€     @^À     @_      @_@     @_€     @_À     @`      @`      @`@     @``     @`€     @`      @`À     @`à     @a      @a      @a@     @a`     @a€     @a      @aÀ     @aà     @b      @b      @b@     @b`     @b€     @b      @bÀ     @bà     @c      @c      @c@     @c`     @c€     @c      @cÀ     @cà     @d      @d      @d@     @d`     @d€     @d      @dÀ     @dà     @e      @e      @e@     @e`     @e€     @e      @eÀ     @eà     @f      @f      @f@     @f`     @f€     @f      @fÀ     @fà     @g      @g      @g@     @g`     @g€     @g      @gÀ     @gà     @h      @h      @h@     @h`     @h€     @h      @hÀ     @hà     @i      @i      @i@     @i`     @i€     @i      @iÀ     @ià     @j      @j      @j@     @j`     @j€     @j      @jÀ     @jà     @k      @k      @k@     @k`     @k€     @k      @kÀ     @kà     @l      @l      @l@     @l`     @l€     @l      @lÀ     @là     @m      @m      @m@     @m`     @m€     @m      @mÀ     @mà     @n      @n      @n@     @n`     @n€     @n      @nÀ     @nà     @o      @o      @o@     @o`     @o€     @o      @oÀ     @oà     @p      @p     @p      @p0     @p@     @pP     @p`     @pp     @p€     @p     @p      @p°     @pÀ     @pÐ     @pà     @pð     @q      @q     @q      @q0     @q@     @qP     @q`     @qp     @q€     @q     @q      @q°     @qÀ     @qÐ     @qà     @qð     @r      @r     @r      @r0     @r@     @rP     @r`     @rp     @r€     @r     @r      @r°     @rÀ     @rÐ     @rà     @rð     @s      @s     @s      @s0     @s@     @sP     @s`     @sp     @s€     @s     @s      @s°     @sÀ     @sÐ     @sà     @sð     @t      @t     @t      @t0     @t@     @tP     @t`     @tp     @t€     @t     @t      @t°     @tÀ     @tÐ     @tà     @tð     @u      @u     @u      @u0     @u@     @uP     @u`     @up     @u€     @u     @u      @u°     @uÀ     @uÐ     @uà     @uð     @v      @v     @v      @v0     @v@     @vP     @v`     @vp     @v€     @v     @v      @v°     @vÀ     @vÐ     @và     @vð     @w      @w     @w      @w0     @w@     @wP     @w`     @wp     @w€     @w     @w      @w°     @wÀ     @wÐ     @wà     @wð     @x      @x     @x      @x0     
    
    ........ and much more data!!!


  • What is the definition of the datatype of the elements of your QVector?
    Provides this datatype an overloaded operator<<() ?

    regards
    Karl-Heinz



  • Thanks @karlheinzreichel

    Originaly I have data called Sample, to save it I save it as a QVector<double> const Value.

    for(int iSamples=0;iSamples<_pSampleProcessing->getRawDataVector().size();iSamples++)
    {
    xDataVector.append(_pSampleProcessing->getRawDataVector().at(iSamples).getX());
    yDataVector.append(_pSampleProcessing->getRawDataVector().at(iSamples).getY());
    }

    I dont think so, because If it would be overloaded I might get an error, because I tried it to save it as a Sample before. So saving as a double seems possible.