Results 1 to 6 of 6

Thread: How to use QtConcurrent on QByteArray for parallelize qCompress

  1. #1
    Join Date
    Apr 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default How to use QtConcurrent on QByteArray for parallelize qCompress

    Hi all,
    Firstly being new on this forum, i quickly present myself. My Name is John, i'm 25 and i'm a french student to an IT engineering school.
    I'm currently trying to develop a simple QT C++ 11 program to compress all files contained into a directory.
    I want to use qCompress from QByteArray class to do this trick.

    I got to do it in a single threaded environment, so now i want to compress in a multi threaded environment by using QtConcurrent.

    But i have no idea on how to do it properly.

    Here a draft i written :

    Qt Code:
    1. QFile outFile("testCompress.ecf");
    2. outFile.open(QIODevice::WriteOnly);
    3. QByteArray nonCompressedData;
    4. foreach(const QString &file,future.results()){
    5. //Fichier d'entrée
    6. QFile inFile(file);
    7. inFile.open(QIODevice::ReadOnly);
    8. nonCompressedData.append(inFile.readAll());
    9. inFile.close();
    10. text += file + "\n";
    11. }
    12.  
    13. //QByteArray compressedData(qCompress(nonCompressedData,9));
    14. //PROBLEM HERE
    15. QFuture<QByteArray> futureCompressor = QtConcurrent::filtered(nonCompressedData,qCompress);
    16. futureCompressor.waitForFinished();
    17. QByteArray compressedData = futureCompressor.results();
    18.  
    19. outFile.write(compressedData);
    To copy to clipboard, switch view to plain text mode 

    But i have some compilation errors.

    I think it is a bad way to do this so i need some help to go on the right way.

    Compilations errors :

    First : No matching function for call to filtered(&QByteArray,).

    Second : converstion from QList to non scalar type QByteArray requested.

    Thanks in advance

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QtConcurrent on QByteArray for parallelize qCompress

    Hi John,

    You should use the "map" and not "filter" semantics. For a vector of file names you should receive a vector of byte arrays containing compressed data from subsequent files.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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

    JohnJohn (29th April 2015)

  4. #3
    Join Date
    Apr 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to use QtConcurrent on QByteArray for parallelize qCompress

    Okay, thank you for the quick reply.

    I need to understand why to use map instead of filter?

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QtConcurrent on QByteArray for parallelize qCompress

    Map converts one data (file name) into other data (compressed content). Filter creates a subset of original data matching certain criteria. In your case it could return file names of files containing images.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Apr 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: How to use QtConcurrent on QByteArray for parallelize qCompress

    Ok thank you.

    I will try this with mapped.

    Just another advice, to make sure i really understand the philosophy.

    I have to store into a Vector of QByteArray all the files name non compressed, and call map by passing the vector and qCompress method?


    Added after 7 minutes:


    Maybe i will have to use a lambda function to wrap qCompress ?
    Last edited by JohnJohn; 29th April 2015 at 15:03.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,365
    Thanks
    3
    Thanked 5,014 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: How to use QtConcurrent on QByteArray for parallelize qCompress

    Quote Originally Posted by JohnJohn View Post
    I have to store into a Vector of QByteArray all the files name non compressed, and call map by passing the vector and qCompress method?
    It has to be a function taking a file name, opening the file, reading it, compressing and returning the content. qCompress() doesn't do all that.

    An example function (ignoring memory restrictions) could be:

    Qt Code:
    1. QByteArray compressFile(const QString &fileName) {
    2. QFile f(fileName);
    3. if(!f.open(QFile::ReadOnly)) return QByteArray();
    4. return qCompress(f.readAll());
    5. }
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 6
    Last Post: 14th May 2014, 12:14
  2. qCompress a directory?
    By qlands in forum Qt Programming
    Replies: 1
    Last Post: 4th May 2012, 14:53
  3. Replies: 1
    Last Post: 22nd June 2011, 08:12
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. QtConcurrent
    By Brandybuck in forum An Introduction to QThreads
    Replies: 2
    Last Post: 9th May 2008, 14:10

Tags for this Thread

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.