hi,
i have an application out of which i want to start a thread wich reads a big textfile into a QString but after having started the thread my mainwindow out of which the thread got started gets stuck (means its stuck during thread execution and i dont know why):
here is some code parts:
gui mainwindow (created with designer):
// //////////////////////////////////////////
// //////////////////////////////////////////
{
Q_OBJECT
// //////////////////////
public:
// //////////////////////
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
public slots:
// //////////////////////
private:
// //////////////////////
Ui::MainWindow *ui;
FileLoader *fileloader;
// //////////////////////
private slots:
// //////////////////////
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};
// //////////////////////////////////////////
class MainWindow : public QMainWindow
// //////////////////////////////////////////
{
Q_OBJECT
// //////////////////////
public:
// //////////////////////
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
public slots:
void AppendTextEdit(QString);
// //////////////////////
private:
// //////////////////////
Ui::MainWindow *ui;
FileLoader *fileloader;
// //////////////////////
private slots:
// //////////////////////
void on_pushButton_2_clicked();
void on_pushButton_clicked();
};
To copy to clipboard, switch view to plain text mode
// //////////////////////////////////
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
// //////////////////////////////////
{
ui->setupUi(this);
fileloader = new FileLoader(this);
ui->textEdit->setCursorWidth(4);
ui
->textEdit
->setLineWrapMode
(QTextEdit::NoWrap);
}
// //////////////////////////////////
MainWindow::~MainWindow()
// //////////////////////////////////
{
delete ui;
delete fileloader;
}
// //////////////////////////////////
void MainWindow
::AppendTextEdit(QString str
) // //////////////////////////////////
{
ui->textEdit->setTextCursor(tc);
ui->textEdit->insertPlainText(str);
}
// //////////////////////////////////
void MainWindow::on_pushButton_clicked()
// //////////////////////////////////
{
fileloader->start();
}
// //////////////////////////////////
void MainWindow::on_pushButton_2_clicked()
// //////////////////////////////////
{
fileloader->quit();
}
// //////////////////////////////////
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
// //////////////////////////////////
{
ui->setupUi(this);
fileloader = new FileLoader(this);
QObject::connect(fileloader, SIGNAL(HaveNextChunk(QString)), this, SLOT(AppendTextEdit(QString)));
ui->textEdit->setCursorWidth(4);
ui->textEdit->setLineWrapMode(QTextEdit::NoWrap);
}
// //////////////////////////////////
MainWindow::~MainWindow()
// //////////////////////////////////
{
delete ui;
delete fileloader;
}
// //////////////////////////////////
void MainWindow::AppendTextEdit(QString str)
// //////////////////////////////////
{
QTextCursor tc = ui->textEdit->textCursor();
tc.movePosition(QTextCursor::End);
ui->textEdit->setTextCursor(tc);
ui->textEdit->insertPlainText(str);
}
// //////////////////////////////////
void MainWindow::on_pushButton_clicked()
// //////////////////////////////////
{
fileloader->start();
}
// //////////////////////////////////
void MainWindow::on_pushButton_2_clicked()
// //////////////////////////////////
{
fileloader->quit();
}
To copy to clipboard, switch view to plain text mode
now the thread-class
#include <QThread>
// //////////////////////////////////////////
// //////////////////////////////////////////
{
Q_OBJECT
// //////////////////////////////
public:
// //////////////////////////////
~FileLoader();
virtual void run();
// //////////////////////////////
signals:
// // //////////////////////////////
// //////////////////////////////
private:
// //////////////////////////////
CLogFile logfile;
};
#include <QThread>
// //////////////////////////////////////////
class FileLoader : public QThread
// //////////////////////////////////////////
{
Q_OBJECT
// //////////////////////////////
public:
// //////////////////////////////
FileLoader(QObject *parent=NULL);
~FileLoader();
virtual void run();
// //////////////////////////////
signals:
// // //////////////////////////////
void HaveNextChunk(QString);
// //////////////////////////////
private:
// //////////////////////////////
CLogFile logfile;
QString chunk;
};
To copy to clipboard, switch view to plain text mode
//// //////////////////////////////////
// //////////////////////////////////
{
}
//// //////////////////////////////////
FileLoader::~FileLoader()
// //////////////////////////////////
{
}
// //////////////////////////////////
void FileLoader::run()
// //////////////////////////////////
{
logfile.Open("big.log");
if( !logfile.IsOpen() )
return;
// ulong filesize = logfile.GetFileSize();
// ulong bytesRead = 0;
chunk.clear();
string token;
while( logfile.GetNextLines(token, 10000) )
{
chunk += token.c_str();
}
emit HaveNextChunk(chunk);
logfile.Close();
}
//// //////////////////////////////////
FileLoader::FileLoader(QObject *parent):QThread(parent)
// //////////////////////////////////
{
}
//// //////////////////////////////////
FileLoader::~FileLoader()
// //////////////////////////////////
{
}
// //////////////////////////////////
void FileLoader::run()
// //////////////////////////////////
{
logfile.Open("big.log");
if( !logfile.IsOpen() )
return;
// ulong filesize = logfile.GetFileSize();
// ulong bytesRead = 0;
chunk.clear();
string token;
while( logfile.GetNextLines(token, 10000) )
{
chunk += token.c_str();
}
emit HaveNextChunk(chunk);
logfile.Close();
}
To copy to clipboard, switch view to plain text mode
u can ignore the CLogFile class stuff (it just reads bunch of lines out of textfile and works correct).
the problem is whenever the thread is started out of my mainwindow the whole mainwindow gets stuck until thread has finished work .. why !?!?
thnx
EDIT: ok it seems the insertPlainText(str) call is the bottle-neck --> i guess the string that has to be put into the textedit is quite big (although the file being read is only half-MB of size)
any ideas as alternative approaches to reading file (maybe chunkwise) via thread and updating the textedit as soon as one chunk is read !?!?
Bookmarks