Passing singal to textfield
-
-
You need to update test's value in onNewSpeedPosted handler.
eg.
@
onNewSpeedPosted: test = newSpeed
@ -
Hi p3c0 :),
thank you again.
i tried it out but still have issues. i got the TypeError: Type error
when i try it in this way@ property string xte: "value"
MyClassData { id: ms onNewSpeedPosted: xte = new newSpeedPosted() }
@
i also tried
but it gives me Error: Insufficient arguments also it doesnt make sense for me :S..
@ MyClassData {
id: ms
onNewSpeedPosted: xte = newSpeedPosted()
}@where i should implement the "handler" ?
-
may be a look at my main and myclass will help.
main.cpp
@ qmlRegisterType<MyClass>("com.test.qt", 1, 0, "MyClassData");
qmlRegisterType<Message>("com.mycompany.messaging", 1, 0, "Message");QGuiApplication app(argc, argv); QQmlApplicationEngine engine; MyClass myClass; engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); QObject *item = engine.rootObjects().value(0); QQuickWindow *window = qobject_cast<QQuickWindow *>(item); //C++ to QML //QString speed = "1337"; //emit myClass.newSpeedPosted(speed); //QML to C++ QObject::connect(item, SIGNAL(qmlSignal(QString)),&myClass, SLOT(cppSlot(QString))); window->show(); return app.exec();@
-
and finally the myClass.cpp
@
MyClass::MyClass(QObject *parent) : QObject(parent)
{}
MyClass::~MyClass()
{}
void MyClass::cppSlot(const QString &msg){
qDebug() << "Called the C++ slot with message:" << msg;
newSpeedPosted("123");
}
@iam sorry for the single posts, had problems the site was thinking my code is spam :).
-
@
onNewSpeedPosted: xte = new newSpeedPosted()
@This wont work here. Instead this should work
@
onNewSpeedPosted: xte = subject
@bq. where i should implement the “handler” ?
onNewSpeedPosted: is the handler. Whenever you create a signal, QML automatically creates an handler for it and you can access the parameters passed to signal there.
Have a look at "this":http://doc.qt.io/qt-5/qtqml-cppintegration-exposecppattributes.html#exposing-signals. It explains exactly what you need.
-
Hi p3c0 :),
thank you for your help! it works now with
@onNewSpeedPosted: xte = subject@but i have a nother problem now :/.
i created a thread and created an instance of myclass. and invoked the method mc.newSpeedPosted("111") but my qml doesn't receive it.
here is my thread class header file:
@#ifndef CANTHREAD_H
#define CANTHREAD_H#include <QObject>
#include <QDebug>
#include <QThread>
#include "myclass.h"class canThread : public QThread, public QObject
{
public:
explicit canThread(QObject *parent = 0);~canThread();
protected:
void run();
};#endif // CANTHREAD_H@
and my cpp
@#include "canthread.h"
canThread::canThread(QObject *parent) : QObject(parent)
{}
void canThread::run(){
qDebug() << "QTHREAD" << endl;MyClass myc; for(int i =0; i<1000; i++){ sleep(1); emit myc.newSpeedPosted("45"); qDebug() << "QTHREAD" << endl; }
}
canThread::~canThread()
{@What do i miss? Why is there no conenction. do you have an idea?
thank you for your time and help
best regards
-
i have also the feeling even if i do send signals that the property is not update ( only after mouse click)
i tried something like that
@ anchors.fill: parent
speed: xtemouseArea.onClicked: { ms.cppSlot(xte) //works //Qt.quit(); qmlSignal("yeah!!!") //works }@
-
bq. What do i miss? Why is there no conenction. do you have an idea?
That is because MyClass myc in run() is not the same as what you have instantiated in QML.
You will need to get a pointer to that instance. For that you can use findChild to get that instance, cast it into MyClass and then emit the signal newSpeedPosted using that object.