无法连接 C++ 和 Qml(连接)
couldn't interface C++ and Qml (connections)
我正在 raspberry pi 4 上开发一个使用 GPIO 的项目。
我交叉编译了 Qt 5.14.2 并创建了一个 QtQuick 应用程序
所以我正在使用 wiringPi 库,我按照本教程 https://www.youtube.com/watch?v=HxNHlhv74tA 创建了一个包含私有属性 m_value
的 GPIO class(它也不起作用放置我的属性时 public).
所以我创建了一个中断(上升和下降),每次检测到中断时,我都会更改我的属性值 m_value 并发出一个信号(我检查 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();
}
}
这就是我的 GPIO.h 的样子
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);
}
当您向 Qml 端发出信号时,您可以通过名称找到发出的参数。如果您将代码更改为:
Connections
{
target: input3
onInputChanged:
{
if(value ==0 )
rectInput3.opacity = 0.0
else
rectInput3.opacity = 1.0
}
}
现在应该可以了。您也可以在里面查看:
onInputChanged:
{
console.log(value)
}
您关注的tutorial也是通过这种方式到达变量的
我正在 raspberry pi 4 上开发一个使用 GPIO 的项目。
我交叉编译了 Qt 5.14.2 并创建了一个 QtQuick 应用程序
所以我正在使用 wiringPi 库,我按照本教程 https://www.youtube.com/watch?v=HxNHlhv74tA 创建了一个包含私有属性 m_value
的 GPIO class(它也不起作用放置我的属性时 public).
所以我创建了一个中断(上升和下降),每次检测到中断时,我都会更改我的属性值 m_value 并发出一个信号(我检查 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();
}
}
这就是我的 GPIO.h 的样子
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);
}
当您向 Qml 端发出信号时,您可以通过名称找到发出的参数。如果您将代码更改为:
Connections
{
target: input3
onInputChanged:
{
if(value ==0 )
rectInput3.opacity = 0.0
else
rectInput3.opacity = 1.0
}
}
现在应该可以了。您也可以在里面查看:
onInputChanged:
{
console.log(value)
}
您关注的tutorial也是通过这种方式到达变量的