passing qtconcurrent



  • whats the current syntax to pass function within a class and its arguement to qtconcurrent.

    for example

    typedef struct
    {
    QString t1;
    float t2;
    int t3;

    } mystrc;

    class test : public QWidget
    {
    Q_OBJECT

    public:

    void test_func(mystrc *mystrc_ptr);
    }

    class Main_Widget : public QWidget
    {
    Q_OBJECT

    public:

    test *test_ptr;

    }

    Main_Widget::Main_Widget(QWidget *parent){

    mystrc *myst_ptr;
    test_ptr = new test;
    QFuture<void> future = QtConcurrent::run(this->test_ptr, &test::test_func, myst_ptr);

    }

    ?


  • Lifetime Qt Champion

    Hi,

    It's explained here.

    Note that in your code, you are passing a local uninitialised pointer which is likely not a good idea.



  • @SGaist

    if i do this,

    Main_Widget::Main_Widget(QWidget *parent){

    mystrc *myst_ptr;

    test_ptr = new test;

    for(n=0; n<100; n++){
    QFuture<void> future = QtConcurrent::run(this->test_ptr, &test::test_func, myst_ptr);
    }
    }

    does it recognize to create 1 thread for the function or its try to create 100 thread. if its latter how do i run the function repeatly in different thread, with out destroy then recreate the thread etc. basically i like to run the function in 1 thread over and over.


  • Lifetime Qt Champion

    Neither, as explained in the QtConcurrent::run documentation, it's going to use a thread pool to run the 100 task you asking it to run.

    What exactly is your function going to do ? From the little you shared, it looks like you should subclass QThread and re-implement the run function with a breakable infinite loop.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.