Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Qt线程调用_C++_Qt_Oop_Qthread - Fatal编程技术网

C++ Qt线程调用

C++ Qt线程调用,c++,qt,oop,qthread,C++,Qt,Oop,Qthread,我试图使用Qt的内置Qthread来运行一个运行一系列GPIO引脚的线程。我已经确认引脚正在运行,问题是实现Qthread 这是我在头文件中的代码: class Runtest : public QThread { public: explicit Runtest(const QString& mark) : mark_(mark) {} void run(); private: QString mark_; }; My.cpp代码: void Runtest

我试图使用Qt的内置Qthread来运行一个运行一系列GPIO引脚的线程。我已经确认引脚正在运行,问题是实现Qthread

这是我在头文件中的代码:

class Runtest : public QThread
{
public:
    explicit Runtest(const QString& mark) : mark_(mark) {}

    void run();
private:
    QString mark_;
};
My.cpp代码:

void Runtest::run()
{
    wiringPiSetup();        //enable gpio library
    pinMode(4, OUTPUT);     //gpio pin 4 enabled

   int x=0;
    while(x<1000)
    {
        x++;
        digitalWrite(4, HIGH);    //gpio output high
        delay(5);
        digitalWrite(4, LOW);     //gpio output low
        delay(5);
    }
}
这不起作用,我收到的错误是在Runtest go1行上没有初始化“Runtest”的匹配构造函数
我对面向对象的编码还不是很有经验,我做错了什么?如何让Runtest::run运行?

您已经这样声明了构造函数:

explicit Runtest(const QString& mark) : mark_(mark) {}
它需要一个QString作为参数。但您正在初始化对象,如下所示:

Runtest go1;
Runtest go1("My string");
您没有将QString传递给构造函数

您应该对其进行如下初始化:

Runtest go1;
Runtest go1("My string");

看起来您正试图定义一个名为Writer的构造函数,但您的类名为Runtest?我不知道GPIO库,但我怀疑它们是异步的,因此控制需要返回到事件循环以进行通信,而您的线程被绑定在while循环中。作为一个实验,用QCoreApplication::processEvents替换delayx行。另外,当您正在做一些主要的事情时,您只需要将QThread子类化。对于简单的任务,考虑QRunnEnter类与QthueCube类的结合。这使您可以专注于解决问题,并让Qt担心线程问题。此外,它还优化了与CPU相关的线程使用情况。如果有太多线程在运行,QRunnables将一直排队,直到上一个作业完成。除了构造函数声明中存在键入错误之外,它被称为Runtest,而不是Writer,您的代码工作正常。尝试将qDebug wiringPiSetup设置为具有返回值。检查它是否有错误。还有一个问题。。。你怎么知道它不起作用?你怎么查的?您知道delay5会产生5毫秒的延迟,这意味着您看不到LED的闪烁?