PDA

View Full Version : Open File in a class and have access to it in another class



mikrocat
11th November 2015, 12:54
Hi,

In my Dialogclass I open a file


if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
ui->FileEdit->setText(filepath);
QByteArray firstlineArray = file.readLine();
ui->UpdateProgressBar->setRange(0,(file.size()-firstlineArray.size()));
firstlineArray.chop(1);
devicetypeFromFile = firstlineArray.toInt();
enableUpdateButton(true);
}

Now I want to have access to this file in my WorkerThread Class, because I need to read some lines from this file (it's a file with many hex-lines). The Dialog starts with a click on a Button the WorkerThread.
What is the best way to get the file to my Worker Thread?

anda_skoa
11th November 2015, 16:12
1) You create the file on the heap
2) You pass it to the thread's constructor and store it in a member of the thread
3) You all moveToThread on the file object
4) You use the file in the thread and let it delete it when you are done

Cheers,
_

mikrocat
12th November 2015, 12:32
Okay I created the file on the heap now.
I don't understand the next steps, can you give me an example please? :)

anda_skoa
12th November 2015, 12:51
I assume you mean the moveToThread, everything else is basic C++ just like creating on the heap.



file->moveToThread(this);


Cheers,
_

mikrocat
12th November 2015, 13:00
Ah okay, should have googled it. Thank You.