c++ - QVector 与 Qthreads

标签 c++ multithreading qt vector

你好,我正在尝试构建 QThread 对象的 QVector 但是当我尝试构建时出现此错误 'QObject::QObject' : cannot访问类“QObject”中声明的私有(private)成员。有人可以告诉我为什么会出现此错误以及如何克服它或指出答案的方向。谢谢你的时间。

主要.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();
}

线程.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

线程.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;
}

最佳答案

简单地说,QObject 对象不可复制也不可赋值——检查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.

因此,您需要使用 QVector<Thread*>存储线程(不要忘记在使用它们之前创建对象)。

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.

关于c++ - QVector 与 Qthreads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14668132/

相关文章:

c++ - 堆栈上的可变大小对象

c# - 具有优先级的信号量

java - 在 "Java Concurrency In Practice"之后阅读更多并发示例?

python - 如何在 QFileDialog 上设置样式表?

qt - 通过 QProcess 运行 .sh 脚本时出错

c++ - OpenCV Canny + 分水岭

c++ - Mongodb insert_many 性能 - C++

c++ - C++ STL 容器的空间复杂度

multithreading - 虚假解除 boost 线程中的阻塞

c++ - 如何将输出写入Qt中特定标签旁边的statusBar