Results 1 to 5 of 5

Thread: Open File in a class and have access to it in another class

  1. #1
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Open File in a class and have access to it in another class

    Hi,

    In my Dialogclass I open a file

    Qt Code:
    1. if(file.open(QIODevice::ReadOnly | QIODevice::Text)){
    2. ui->FileEdit->setText(filepath);
    3. QByteArray firstlineArray = file.readLine();
    4. ui->UpdateProgressBar->setRange(0,(file.size()-firstlineArray.size()));
    5. firstlineArray.chop(1);
    6. devicetypeFromFile = firstlineArray.toInt();
    7. enableUpdateButton(true);
    8. }
    To copy to clipboard, switch view to plain text mode 

    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?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Open File in a class and have access to it in another class

    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,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    mikrocat (12th November 2015)

  4. #3
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Open File in a class and have access to it in another class

    Okay I created the file on the heap now.
    I don't understand the next steps, can you give me an example please?

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Open File in a class and have access to it in another class

    I assume you mean the moveToThread, everything else is basic C++ just like creating on the heap.

    Qt Code:
    1. file->moveToThread(this);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

  6. #5
    Join Date
    Oct 2015
    Posts
    50
    Thanks
    4
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Open File in a class and have access to it in another class

    Ah okay, should have googled it. Thank You.

Similar Threads

  1. How to access the UI from another class ?
    By fitzy in forum Qt Programming
    Replies: 22
    Last Post: 20th July 2016, 15:21
  2. Replies: 4
    Last Post: 2nd April 2013, 10:13
  3. Replies: 4
    Last Post: 29th May 2010, 13:56
  4. Replies: 3
    Last Post: 27th December 2008, 20:34
  5. Replies: 5
    Last Post: 14th July 2006, 23:42

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.