简体   繁体   中英

QVector with Qthreads

Hello I'm trying to build a QVector of QThread objects but when I try to build I get this error 'QObject::QObject' : cannot access private member declared in class 'QObject' . Can someone tell me why I am getting this error and how to over come it or point me in the direction of answer. Thank you for you time.

MAIN.CPP

#include <QCoreApplication>

#include "thread.h"
#include <QDebug>
#include <QThread>

main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    unsigned long long startingNumberAnswer = 0;
    unsigned long long totalIterationsAnswer = 0;
    int numberOfThreads = 10;


    QVector<Thread> threads(numberOfThreads);

    for(int l = 0; l < threads.size(); l++ ){
        threads[l].setPriority(QThread::TimeCriticalPriority);
    }


    for(int i = 1; i< 2000000; i+=numberOfThreads){
        qDebug() << "Longest iteration Number =  " << startingNumberAnswer;
        qDebug() << "Number of iterations for " << startingNumberAnswer << " is " << totalIterationsAnswer;
        qDebug() << "Running # " << i;


        system("CLS");

    }



    qDebug() << "Longest iteration Number =  " << startingNumberAnswer;
    qDebug() << "Number of iterations for " << startingNumberAnswer << " is " << totalIterationsAnswer;



    return a.exec();
}

THREAD.H

#ifndef THREAD_H
#define THREAD_H

#include <QObject>
#include <QThread>

class Thread :  public QThread
{
    Q_OBJECT
public:
    explicit Thread();

    unsigned long long getloops();
    unsigned long long getnumber();
signals:

public slots:
    void run(unsigned long long value);
private:
    unsigned long long largestNumber;
    unsigned long long loops;
    unsigned long long number;


};

#endif // THREAD_H

THREAD.CPP

#include "thread.h"

Thread::Thread()
{
}

void Thread::run(unsigned long long value)
{
   unsigned long long n = value;
   unsigned long long counter = 0;

   while ( n > 1){

       if(n%2 == 0){
          n = n/2;
       } else {
          n = (3*n) + 1;
       }

       counter++;
   }

    loops = counter;
    number = value;

}

unsigned long long Thread::getloops(){
    return loops;
}

unsigned long long Thread::getnumber(){
    return number;
}

Simply, QObject objects are not copyable nor assignable - check the official documentation :

No copy constructor or assignment operator

QObject has neither a copy constructor nor an assignment operator. This is by design. Actually, they are declared, but in a private section with the macro Q_DISABLE_COPY(). In fact, all Qt classes derived from QObject (direct or indirect) use this macro to declare their copy constructor and assignment operator to be private. The reasoning is found in the discussion on Identity vs Value on the Qt Object Model page.

The main consequence is that you should use pointers to QObject (or to your QObject subclass) where you might otherwise be tempted to use your QObject subclass as a value. For example, without a copy constructor, you can't use a subclass of QObject as the value to be stored in one of the container classes. You must store pointers.

So, you will need to use QVector<Thread*> to store threads (don't forget to create objects before using them).

It is also noted in QVector's documentation :

QVector's value type must be an assignable data type. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, indexOf() and lastIndexOf() expect the value type to support operator==(). These requirements are documented on a per-function basis.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM