0

I am trying to run a QtConcurrent::run from a QAbstractTableModel but run it as a non static method.

Here is my header file:

#ifndef DATA_INFO_HPP
#define DATA_INFO_HPP

#include <QAbstractTableModel>
#include <QObject>
#include <QFutureWatcher>
#include <QtConcurrent/QtConcurrent>

struct DATA
{
    quint32 pages;
    quint32 chapers;
    quint32 books;

};

inline QDataStream &operator<<(QDataStream &readstream, const DATA &m_data)
{
    return readstream << m_data.pages << m_data.chapers << m_data.books;
}

inline QDataStream &operator>>(QDataStream &writestream, DATA &m_data)
{
    return writestream >> m_data.pages >> m_data.chapers >> m_data.books;
}

class Data_Info : public QAbstractTableModel
{
    Q_OBJECT
    using rowData = QVector<DATA>;
    QFutureWatcher<rowData>* m_watcher;
    rowData m_rows;

public:
    Data_Info(QByteArray Data, QObject* parent= nullptr);
    rowData retrieveData(QByteArray Data);

    int rowCount(const QModelIndex &parent) const override;
    int columnCount(const QModelIndex &parent) const override;
    QVariant data(const QModelIndex &index, int role) const override;
    QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
    Qt::ItemFlags flags(const QModelIndex &index) const override;
    bool setData(const QModelIndex &index, const QVariant &value, int role) override;
    bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
    bool removeRows(int position, int rows, const QModelIndex &index = QModelIndex()) override;
    
    const QVector<DATA> &getData() const;

public slots:
    void updateData();

private:

};

#endif // DATA_INFO_HPP

Inside my .cpp file i try to call it like this:

#include "data_info.hpp"

Data_Info::Data_Info(QByteArray Data, QObject *parent):QAbstractTableModel(parent)
{
    m_watcher = new QFutureWatcher<rowData>(this);
    connect(m_watcher, &QFutureWatcher<rowData>::finished,this, &Data_Info::updateData);
    QFuture<rowData> future = QtConcurrent::run(&Data_Info::retrieveData, Data);
    m_watcher->setFuture(future);
}

Data_Info::rowData Data_Info::retrieveData(QByteArray Data)
{
    quint32 addRowCounter = 0;
    quint32 addRowCounterLoop = 0;
    rowData list;
    DATA m_data;
    QDataStream readstream(Data);
    readstream.setByteOrder(QDataStream::LittleEndian);
    readstream >> addRowCounter;
    while(!readstream.atEnd())
    {
        readstream >> m_data;
        list.append( m_data );
        ++addRowCounterLoop;
        if (addRowCounter == addRowCounterLoop)
        {
            break;
        }
    }
    return list;
}


void Data_Info::updateData()
{
    beginResetModel();
    m_rows = m_watcher->future().result();
    endResetModel();
}

my issue seems to lie that QtConcurrent::run only takes a static method?

QFuture<rowData> future = QtConcurrent::run(&Data_Info::retrieveData, Data);

How would i go about fixing this error? Just want to run a thread inside this object?

Styx
  • 1
  • 1
  • 2
    Have you read the documentation? https://doc.qt.io/qt-5/qtconcurrentrun.html All you need to know is there. – n. m. could be an AI Mar 05 '21 at 20:33
  • its not a void tho but a vector – Styx Mar 05 '21 at 21:03
  • What do you mean by `"its not a void tho but a vector"`? Please clarify what you're trying to do and the problems you encounter. – G.M. Mar 06 '21 at 08:29
  • Trying to make the function non static only way i know is using a singleton. But that would mean making the gui class a singleton and passing data that way. Static function only accepts static variables and i want it non static so i can call al signal from there. – Styx Mar 07 '21 at 19:41

0 Answers0