Reading large files

I want to read a 50 MB file and send it via tcp. The file contains only floats. At first I created only Mainwindow, the witch reads one line and sends it to the server, but gui froze. So I created a class that depends on QThread called QSendThread. Here is the code for the QThread class:

#ifndef QSENDTHREAD_H
#define QSENDTHREAD_H

#include <QThread>
#include <QLabel>
#include <QFile>
#include <QMessageBox>
#include <QtNetwork/QTcpSocket>
#include <QtNetwork/QHostAddress>

class QSendThread : public QThread
{
 Q_OBJECT

public:
 QSendThread(QTcpSocket* qtcpso, QLabel* qlbl, QFile* qfiel, QObject *parent = NULL);
 ~QSendThread();

protected:
 void run(void);

private:
 QTcpSocket* qtcpsoDest;
 QLabel* qlblRef;
 QFile* qfileRef;

signals:
 void error(QString qstrError);
};

#endif // QSENDTHREAD_H

#include "qsendthread.h"

QSendThread::QSendThread(QTcpSocket* qtcpso, QLabel* qlbl, QFile* qfile, QObject *parent)
 : QThread(parent)
{
 qtcpsoDest = qtcpso;
 qlblRef = qlbl;
 qfileRef = qfile;
}

QSendThread::~QSendThread()
{
}

void QSendThread::run(void)
{
 int iLine = 0;

 do
 {
  QByteArray qbarrBlock;
  QDataStream qdstrmOut(&qbarrBlock, QIODevice::WriteOnly);

            // show witch line is read
  qlblRef->setText(tr("Reading Line: %1").arg(++iLine));

  qdstrmOut.setVersion(QDataStream::Qt_4_6);
  qdstrmOut << (quint16)0;
  qdstrmOut << qfileRef->readLine().data();
  qdstrmOut.device()->seek(0);
  qdstrmOut << (quint16)(qbarrBlock.size() - sizeof(quint16));

  qtcpsoDest->write(qbarrBlock);
  qtcpsoDest->flush();

  qbarrBlock.clear();
 } while(!qfileRef->atEnd());
}

But the program crashes in the method qregion::qt_region_strictContains(const QRegion ®ion, const QRect &rect)

Is the method reading a file as if I were doing it wrong?

Thanks for the help.

+3
source share
1 answer

-, QThread. Qt . . .

-, gui , qlblRef->setText() . gui , , , postEvent(). .

, Qt. QObjects.

:

, , , QObject. ( ) , QtConcurrent:: run() a QFuture.

+5

Source: https://habr.com/ru/post/1774021/


All Articles