Results 1 to 9 of 9

Thread: How to use QList to add many QFiles.

  1. #1
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default How to use QList to add many QFiles.

    Hi,
    I need write many files at the same time,so I define many files before use them and put them in QList. code as follows:

    QList<QFile> writeFileList;

    for(int i=0;i<count;i++)
    {
    QString writeFileName = writeDir+"/"+fileName[i];
    QFile writeFile(writeFileName);
    writeFile.open(QIODevice::WriteOnly);
    QDataStream out(&writeFile);
    writeFileList.append(writeFile);
    }

    but error occur:
    C2248 "can not access private member declared in class 'QFile'" .

    How to solve it ?thank you.

  2. #2
    Join Date
    Jul 2008
    Posts
    448
    Thanks
    9
    Thanked 58 Times in 56 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to use QList to add many QFiles.

    Hi, try storing pointers to QFile in the list.
    By the way, your QDataStream object is a local variable for each iteration, and is destroyed again when a loop iteration is finished.

    Ginsengelf

  3. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,573
    Thanks
    256
    Thanked 728 Times in 718 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to use QList to add many QFiles.

    C2248 "can not access private member declared in class 'QFile'" .
    This line:

    Qt Code:
    1. writeFileList.append(writeFile);
    To copy to clipboard, switch view to plain text mode 

    is trying to execute the QFile copy constructor. Copying and assignment is not allowed for any class derived from QObject, so the copy constructor and operator=() are private in the QObject class and all classes derived from it. So basically, you cannot construct a QList of QFile objects.

    Even if you stored QFile pointers as Ginsengelf suggested, your design of keeping a lot of open files around does not seem like a very good one. In Windows, there are a limited number of resource handles available to any program. If your list is large, then you could run out of handles (which are used for other things beside files) and your program would break because it can't open all the files in the list.

    You should probably consider a different design for your program.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  4. #4
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QList to add many QFiles.

    Thank you very much.How to design my program?Could you give me some advice?

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,652
    Thanks
    13
    Thanked 1,592 Times in 1,520 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to use QList to add many QFiles.

    Quote Originally Posted by weixj2003ld View Post
    How to design my program?Could you give me some advice?
    Only you know your requirements. Consider these things:
    • How many is "many files"? 5 is easy, 5000 would need a different approach.
    • What is being written? Text or binary blobs require different approaches.
    • How is it being written and how often? In response to an external event, or under your control. Every 50 milliseconds or every day. Different approaches needed.
    • The same thing to every file or different things? All the same... write one and duplicate it later
    • How close to real-time does it need to be? If you can accept a few seconds lag then other options are available.
    • Target files local or over a network? One performs better than the other.

  6. #6
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QList to add many QFiles.

    Thank you for your answer very much.My requirement is :
    First I read about 10-15 column dignal datum (about 50000 rows) from one text file,and then write them into 10-15 (the same as column number) binary files differently.All the operations are in a local computer.
    Last edited by weixj2003ld; 16th July 2020 at 12:09.

  7. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,512
    Thanked 277 Times in 272 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to use QList to add many QFiles.

    First step : read all data to memory creating one list/vector for one column.
    Second step : write data to out files one after the other. In this case, you need a list of file names and you only have one file open at a time.

  8. #8
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,573
    Thanks
    256
    Thanked 728 Times in 718 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: How to use QList to add many QFiles.

    First step : read all data to memory creating one list/vector for one column.
    Second step : write data to out files one after the other. In this case, you need a list of file names and you only have one file open at a time.
    This will work as long as the file can fit into memory, and a 15 x 50000 entry table is probably OK. If the files get to be too big to fit into memory, then there are at least two more options:

    1 - Open the file using memory mapping and let the OS take care managing the memory. If the data is stored in the file in row x column order, then you might want to take your original approach of keeping 10 - 15 files open so the memory mapping will be efficient.

    2 - Don't load the file into memory, but read through it 10 - 15 times, once for each column.

    It really depends on how often you want to do this conversion. If it is something you do once for each batch of data, then it really doesn't matter if it takes 1 second or 10 seconds. If it is something you will do a lot (eg. you are trying to keep up with real-time data acquisition), then you want it to be as fast as possible.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  9. #9
    Join Date
    Mar 2009
    Location
    Gansu,China
    Posts
    188
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to use QList to add many QFiles.

    Thank you all,I will try it.

Similar Threads

  1. Replies: 4
    Last Post: 6th September 2019, 14:05
  2. Qlist<QLabel *> in Qlist<QAction*>
    By Naahmi in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2011, 08:53
  3. Cast QList<Foo*> to QList<const Foo*>
    By vfernandez in forum Qt Programming
    Replies: 0
    Last Post: 4th October 2010, 16:04
  4. Replies: 4
    Last Post: 20th August 2010, 13:54
  5. QList: Out of memory - without having defined QList
    By miroslav_karpis in forum Qt Programming
    Replies: 1
    Last Post: 27th March 2009, 08: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.