我只想将cpp信号连接到qml插槽并尝试不同的方式,但它总是在运行时导致相同的QML-Error:无法分配给不存在的属性" onProcessed&#34 ; !为什么呢?
这是我的Cpp对象:
#include <QObject>
class ImageProcessor : public QObject
{
Q_OBJECT
public:
explicit ImageProcessor(QObject *parent = 0);
signals:
void Processed(const QString str);
public slots:
void processImage(const QString& image);
};
ImageProcessor::ImageProcessor(QObject *parent) :
QObject(parent)
{
}
void ImageProcessor::processImage(const QString &path)
{
Processed("test");
}
这是我的main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QtQml>
#include "imageprocessor.h"
int main(int argc, char *argv[])
{
qmlRegisterType<ImageProcessor>("ImageProcessor", 1, 0, "ImageProcessor");
QGuiApplication app(argc, argv);
QQmlApplicationEngine engine;
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
return app.exec();
}
这是我的QML文件
import QtQuick 2.2
import QtQuick.Window 2.1
import QtMultimedia 5.0
import ImageProcessor 1.0
Window {
visible: true
width: maximumWidth
height: maximumHeight
Text {
id: output
text: qsTr("Hello World")
anchors.centerIn: parent
}
VideoOutput {
anchors.fill: parent
source: camera
}
Camera {
id: camera
// You can adjust various settings in here
imageCapture {
onImageCaptured: {
imageProcessor.processImage(preview);
}
}
}
MouseArea {
anchors.fill: parent
onClicked: {
camera.imageCapture.capture();
}
}
ImageProcessor{
id: imageProcessor
onProcessed: {
output.text = str;
}
}
}
我在Qt Creator 3.1.1中使用QT 5.3.0,它甚至建议我 onProcessed 并正确地突出显示它。
答案 0 :(得分:8)
要从C ++对象公开信号,您必须遵循一些命名约定:
void yourLongSignal()
on<YourLongSignal>
因此,您在代码中编辑的唯一内容就是更改
signals:
void processed(const QString& str);