-1

I'm new to implementing Threads in QT and even after reading the Documentation several times and watching Videos, I get some Error which not even Google can help me with.

thread.cpp:14: error: C2440: "Initialisierung": "QFuture" kann nicht in "QFuture" konvertiert werden

Error Codes are in German, tried to change QT Language, but didn't change the Language of the Errors. I can translate them if needed.

It seems the Error happens in this QFuture<int> future = QtConcurrent::run(&Thread::GenerateTable); command, even thought I wrote it 1:1 like from the QT Documentation. Here is the Code I want to put in a Thread, as you can see it's writing a bit bunch of Numbers into a File, which takes around a Minute.

Thread.h

#ifndef THREAD_H
#define THREAD_H

#include <QObject>
#include <QFuture>
#include <QtConcurrent/QtConcurrent>


class Thread : public QObject
{
    Q_OBJECT
public:
    explicit Thread(QObject *parent = nullptr);

    static bool start();

private:
   int GenerateTable();
};

#endif // THREAD_H

Thread.cpp

#include "thread.h"

Thread::Thread(QObject *parent) : QObject(parent)
{

}

bool Thread::start()
{

    QFuture<int> future = QtConcurrent::run(&Thread::GenerateTable);
    if (future.result() == 0){
        return true;
    }
    else
        return false;
}

int Thread::GenerateTable(){

    QString Path = QDir::currentPath();
    QFile file(Path + "/Table.csv");
    if (!file.open(QFile::WriteOnly | QFile::Text)){
        return -1;
    }
    else{
        QTextStream stream(&file);
        constexpr uint64_t upper = 10'000'000;
        QVector<uint64_t> rando(upper);

        std::iota(rando.begin(), rando.end(), 1);
        std::shuffle(rando.begin(), rando.end(),
                     std::mt19937(std::random_device{}()));

        for (uint32_t i = 0; i < 10'000'000; ++i) {
          stream << rando[i] << ',' << '\n';
        }
        return 0;
    }
}

Deto24
  • 63
  • 5
  • remove `, QString("Test")` – eyllanesc Mar 12 '21 at 22:55
  • @dresherjm Sorry, forgot to delete it. Kinda sitting already quite long on the Problem, so start to feel the exhaustion – Deto24 Mar 12 '21 at 23:21
  • Put the error message -- even if it is in German -- in the question **as text** rather than as an image. There's only one error message referring to your specific code (the top five lines or so in the image are qtconcurrent internals because of the problem in your thread.cpp) – Adriaan de Groot Mar 12 '21 at 23:44
  • Also, [minimal example](https://stackoverflow.com/help/minimal-reproducible-example), you have lots here -- in particular your UI code -- that it's not easy to paste-and-compile for people who would like to answer. – Adriaan de Groot Mar 12 '21 at 23:48
  • @AdriaandeGroot Thanks for the Tipps, quite new here. Will edit my post, so that it's easier to read :) – Deto24 Mar 12 '21 at 23:53

1 Answers1

0

Thread::GenerateTable() is a member function. It needs an object to work on. You are calling it (er .. passing it to QtConcurrent::run()) from the (static) Thread::start() and there's no Thread object to speak of.

Although you've tagged Qt6, I'll point at the Qt5 documentation for calling member functions: you can pass an object (pointer) which you'll need to allocate from somewhere.

  • Do you mean something like `Thread *pntr;` `pntr = new Thread;` and then use `pntr->GenerateTable()` instead of `Thread::GenerateTable` – Deto24 Mar 13 '21 at 01:00