C++ 不能';t接口C++;和Qml(连接)
我正在做一个关于我的raspberry pi 4的项目,它使用GPIO 我交叉编译了Qt5.14.2,并创建了一个QtQuick应用程序 因此,我正在使用wiringPi库,我遵循本教程,创建了一个包含私有属性C++ 不能';t接口C++;和Qml(连接),c++,qt,qml,connection,C++,Qt,Qml,Connection,我正在做一个关于我的raspberry pi 4的项目,它使用GPIO 我交叉编译了Qt5.14.2,并创建了一个QtQuick应用程序 因此,我正在使用wiringPi库,我遵循本教程,创建了一个包含私有属性m_值的GPIO类(将我的属性公开时,它也不起作用) 因此,我创建了一个中断(上升和下降),每次检测到中断时,我都会更改属性m_值的值,并发出一个信号(我用qdebug检查一切正常) 根据属性的值,我想改变矩形的不透明度,但它不起作用 Connections { tar
m_值
的GPIO类(将我的属性公开时,它也不起作用)
因此,我创建了一个中断(上升和下降),每次检测到中断时,我都会更改属性m_值的值,并发出一个信号(我用qdebug检查一切正常)
根据属性的值,我想改变矩形的不透明度,但它不起作用
Connections
{
target: input3
onInputChanged:
{
if(input3.m_value ==0 )
rectInput3.opacity = 0.0
else
rectInput3.opacity = 1.0
}
}
我做错什么了吗
这就是我的main.cpp的样子
#include "gpio.h"
#include<QDebug>
int impulsion=0;
static void isrInput3();
static GPIO input3(3, GPIO_INPUT, isrInput3);
static void isrInput3()
{
impulsion ++;
input3.isrCallback();
qDebug()<<"the number of pulses"<<impulsion<<"le nombre de tour est "<<impulsion/8;
qDebug()<<"m_value "<<input3.readPin();
int main(int argc, char *argv[])
{
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
QQmlContext* ctx = engine.rootContext();
ctx->setContextProperty("input3", &input3);
const QUrl url(QStringLiteral("qrc:/main.qml"));
QObject::connect(&engine, &QQmlApplicationEngine::objectCreated,
&app, [url](QObject *obj, const QUrl &objUrl) {
if (!obj && url == objUrl)
QCoreApplication::exit(-1);
}, Qt::QueuedConnection);
engine.load(url);
return app.exec();
}
}
class GPIO : public QObject
{
Q_OBJECT
public:
explicit GPIO(int pin, int type, void (*isrInput)(void) = nullptr, QObject *parent = nullptr);
void isrCallback();
int readPin();
private:
int m_value;
signals:
void inputChanged(int value);
};
#endif // GPIO_H
这是我的GPIO.cpp
GPIO::GPIO(int pin, int type, void (*isrInput)(void), QObject *parent) : QObject(parent)
{
wiringPiSetup();
m_pin = pin;
switch(type)
{
case GPIO_INPUT:
{
pinMode(m_pin, INPUT);
wiringPiISR(m_pin, INT_EDGE_BOTH, isrInput);
} break;
case GPIO_OUTPUT:
{
pinMode(m_pin, OUTPUT);
} break;
}
}
void GPIO::isrCallback()
{ if (digitalRead(m_pin)== 1)
m_value=1;
else m_value=0;
emit inputChanged(m_value);
qDebug()<<"signal emitted";
}
int GPIO::readPin()
{
return digitalRead(m_pin);
}
GPIO::GPIO(int-pin,int-type,void(*isrInput)(void),QObject*父对象):QObject(父对象)
{
wiringPiSetup();
m_pin=pin;
开关(类型)
{
案例GPIO_输入:
{
引脚模式(m_引脚,输入);
接线PIISR(m_引脚、INT_边缘、ISR输入);
}中断;
案例GPIO_输出:
{
引脚模式(m_引脚,输出);
}中断;
}
}
void GPIO::isrCallback()
{如果(数字读取(m_引脚)==1)
m_值=1;
否则m_值=0;
发射输入改变(m_值);
qDebug()当您向Qml侧发出信号时,您可以通过其名称访问发出的参数。如果您更改代码如下:
Connections
{
target: input3
onInputChanged:
{
if(value ==0 )
rectInput3.opacity = 0.0
else
rectInput3.opacity = 1.0
}
}
现在它应该可以工作了。您还可以检查内部:
onInputChanged:
{
console.log(value)
}
<>你的跟随者也通过这种方式达到变量。你的成员变量<代码> MyValue/Cuff>只知道C++,你必须使用它使它在QML中可用。你也可以在QML中使用<代码>控制台。log < /COD>来查看信号是否被实际处理(但我确实这么认为)。@Amfasis感谢您的澄清,非常感谢它现在正在工作。实际上,它在一开始使用值时不起作用,我看不出值指的是什么(我认为应该由moc实施)…我也做了一些修改..但现在很清楚了