问题
I connect a c++ signal to qml function in qt4.8.4. It's working fine but makes warning in application output as below:
Object::connect: No such slot QDeclarativeItem_QML_9::onValue_changed(double) Object::connect: (sender name: 'MyWidget')
I have defined qml slot like this :
import QtQuick 1.0
Item {
id: root
property real value : 0
Connections
{
target: controllerObject
onValue_changed :
{
root.value = value
}
}
}
And this is my c++ Signal and how it is connected to qml slot :
ui->view->rootContext()->setContextProperty("controllerObject",this);
ui->view->setSource(QUrl("qrc:/myQml.qml"));
ui->view->setStyleSheet("background-color: rgba(255, 255, 255, 0);");
ui->view->setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform | QPainter::HighQualityAntialiasing);
ui->view->setResizeMode(QDeclarativeView::SizeRootObjectToView);
connect(this,SIGNAL(value_changed(double)),(QObject *)ui->view->rootObject(),SLOT(onValue_changed(double)));
Why is it making that warning?
How to omit the warning?
回答1:
I have defined qml slot like this :
You are wrong. It is not slot definion, it's connection
itself
(the adding of QML handler for signal value_changed
of object controllerObject
).
That's why you code works. But in this line :
connect(this, SIGNAL(value_changed(double)), (QObject*)ui->view->rootObject(), SLOT(onValue_changed(double)));
You are trying to connect existing signal value_changed
to inexisting onValue_changed
(obviously, it exists in your code, but not in rootObject
of view).
That's why you got warning.
Conclusion:
You tried to connect signal twice, but only one method was successful, so code worked well.
来源:https://stackoverflow.com/questions/27097944/warning-when-connecting-c-signal-to-qml-slot