2

In my application there is a saving process that blocks my UI. Optimising was not enough, so I want to implement threading with QtConcurrent but I can't get it working, though the syntax is looking easy.

That's how it looks right now (principle):

Traymenu::Traymenu(QApplication *a){
    //...

    void save(){
        //Get the savepath, choose if saving is necessary, and trigger saving
        QString path = getSavePath();
        saveNotes(path);
    }

    void saveNotes(QString savePath){
        //save to the given path, takes quite a while, should be parallel
    }    
}

What I have tried:

Traymenu::Traymenu(QApplication *a){
    //...

    void save(){
        //Get the savepath, choose if saving is necessary, and trigger saving
        QString path = getSavePath();
        QFuture<void> result = QtConcurrent(saveNotes, path); //ERROR
    }

    void saveNotes(QString savePath){
        //save to the given path
    }    
}

Besides, in my header, I am including this:

#include <QtConcurrent/QtConcurrentRun>

The error message is

C:\App\appname\traymenu.cpp:584: Error: no matching function for call 
to 'run(<unresolved overloaded function type>, QString&)'
             QFuture<void> future = QtConcurrent::run(save, outputFilename);
                                                                          ^

I have also tried this:

QFuture<void> future = QtConcurrent::run(this, this->save(outputFilename));

There the erros is

C:\App\appname\traymenu.cpp:584: Error: invalid use of void expression
                 QFuture<void> future = QtConcurrent::run(this, this->save(outputFilename));
                                                                                      ^

My header looks like:

class Traymenu : public QSystemTrayIcon
{
    Q_OBJECT
public:
    Traymenu(QApplication *);
    ~Traymenu();

    void save();        
    void saveNotes(QString);     
    //[...]     

What am I doing wrong?

user2366975
  • 4,350
  • 9
  • 47
  • 87
  • `QFuture future = QtConcurrent::run(save, outputFilename);` -> You have not even showed such code to us. Are you showing the offending code? – László Papp May 16 '14 at 21:01
  • Sorry I was typing an example so you understand it better; the error was from my real program. path=outputFilename and saveNotes=save – user2366975 May 16 '14 at 21:10
  • @user2366975 Please refer to this question: http://stackoverflow.com/questions/2152355/is-it-possible-to-use-qtconcurrentrun-with-a-function-member-of-a-class. – RA. May 16 '14 at 21:52
  • I already searched and found this. I can't adapt. Why is there the first argument a variable? The doc says, it must be a function, doesn't it? In all the examples i find the analogy not fitting for me. – user2366975 May 16 '14 at 22:15
  • 2
    @user2366975 See this from Qt's documentation: http://qt-project.org/doc/qt-4.8/qtconcurrentrun.html (particularly the part about member functions). You need something like this: `QtConcurrent::run(this, &Traymenu::saveNotes, outputFileName);` – RA. May 16 '14 at 23:00

1 Answers1

6

From the official documentation:

Using Member Functions

QtConcurrent::run() also accepts pointers to member functions. The first argument must be either a const reference or a pointer to an instance of the class. Passing by const reference is useful when calling const member functions; passing by pointer is useful for calling non-const member functions that modify the instance.

That means that you would write this to get this working:

QtConcurrent::run(this, &Traymenu::saveNotes, outputFileName);
Community
  • 1
  • 1
László Papp
  • 51,870
  • 39
  • 111
  • 135