Passing singal to textfield
-
wrote on 11 Jan 2015, 22:28 last edited by
Dear Qt Community :),
i am new to qt world and iam struggling learning C++ and QML slot/signal using for QT 5.4.
i have a C++ MyClass Class which has a signal called newSpeedPosted(QString speed);
and i registered it with qmlRegisterType to get access with QML like
@ MyClassData {
id: ms
onNewSpeedPosted: subject
}@but i dont know how to path my signal to a text speedText.
hope you have an idea how can do it.
My main QML:
@Window {
signal qmlSignal(string msg)
visible: trueMyClassData { id: ms onNewSpeedPosted: console.log("Signal received: ", subject) //works fine } MainForm { anchors.fill: parent mouseArea.onClicked: { ms.cppSlot("Test") //works // speedText.text = ?? how to continue here? i want the signal from onNewSpeedPosted. //Qt.quit(); qmlSignal("yeah!!!") //works } }
}
@and my MainForm.ui.qml
@Rectangle {
property alias mouseArea: mouseArea
property alias speedText: speedText
property string speed: "1"width: 360 height: 360 MouseArea { id: mouseArea anchors.rightMargin: 0 anchors.bottomMargin: 0 anchors.leftMargin: 0 anchors.topMargin: 0 anchors.fill: parent } Text { anchors.centerIn: parent text: "Hello World xD" } Text { id: speedText anchors.centerIn: parent width: 0 height: 17 text: speed anchors.verticalCenterOffset: 31 anchors.horizontalCenterOffset: 8 }
}@
hope i could explain my problem.
thank you for your time and help.
best regards
oomqt -
Hi,
There are a couple of ways to do this. One simple way would be to create a string property(in your case), update this in onNewSpeedPosted handler. And bind this property to text property of speedText. Thus it gets updated when the signal is fired.
-
wrote on 12 Jan 2015, 09:08 last edited by
Hi p3c0,
first of all thank you for your prompt reply. Do you mean like
@
property string test: "0"@and than
@ MyClassData {
id: ms
onNewSpeedPosted: test
}@i try it before but it didn't work. i will post the error later when i am at home.
thank you
-
You need to update test's value in onNewSpeedPosted handler.
eg.
@
onNewSpeedPosted: test = newSpeed
@ -
wrote on 13 Jan 2015, 00:03 last edited by
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" ?
-
wrote on 13 Jan 2015, 00:03 last edited by
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();@
-
wrote on 13 Jan 2015, 00:04 last edited by
myClass Header:
@
class MyClass : public QObject
{
Q_OBJECT
public:
explicit MyClass(QObject *parent = 0);
~MyClass();signals:
void newSpeedPosted(const QString &subject);public slots:
void cppSlot(const QString &msg);
};
@ -
wrote on 13 Jan 2015, 00:05 last edited by
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.
-
wrote on 14 Jan 2015, 12:59 last edited by
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
-
wrote on 14 Jan 2015, 13:55 last edited by
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. -
@
anchors.fill: parent
speed: xtemouseArea.onClicked: {
ms.cppSlot(xte) //works
//Qt.quit();
qmlSignal("yeah!!!") //works
}
@What does not work here ?
1/13