Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Passing singal to textfield

Passing singal to textfield

Scheduled Pinned Locked Moved General and Desktop
13 Posts 2 Posters 3.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    oomqt
    wrote on last edited by
    #1

    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: true

    MyClassData {
        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

    1 Reply Last reply
    0
    • p3c0P Offline
      p3c0P Offline
      p3c0
      Moderators
      wrote on last edited by
      #2

      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.

      157

      1 Reply Last reply
      0
      • O Offline
        O Offline
        oomqt
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • p3c0P Offline
          p3c0P Offline
          p3c0
          Moderators
          wrote on last edited by
          #4

          You need to update test's value in onNewSpeedPosted handler.
          eg.
          @
          onNewSpeedPosted: test = newSpeed
          @

          157

          1 Reply Last reply
          0
          • O Offline
            O Offline
            oomqt
            wrote on last edited by
            #5

            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" ?

            1 Reply Last reply
            0
            • O Offline
              O Offline
              oomqt
              wrote on last edited by
              #6

              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();@
              
              1 Reply Last reply
              0
              • O Offline
                O Offline
                oomqt
                wrote on last edited by
                #7

                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);
                };
                @

                1 Reply Last reply
                0
                • O Offline
                  O Offline
                  oomqt
                  wrote on last edited by
                  #8

                  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 :).

                  1 Reply Last reply
                  0
                  • p3c0P Offline
                    p3c0P Offline
                    p3c0
                    Moderators
                    wrote on last edited by
                    #9

                    @
                    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.

                    157

                    1 Reply Last reply
                    0
                    • O Offline
                      O Offline
                      oomqt
                      wrote on last edited by
                      #10

                      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

                      1 Reply Last reply
                      0
                      • O Offline
                        O Offline
                        oomqt
                        wrote on last edited by
                        #11

                        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: xte

                            mouseArea.onClicked: {
                                ms.cppSlot(xte) //works
                                //Qt.quit();
                                qmlSignal("yeah!!!") //works
                            }@
                        
                        1 Reply Last reply
                        0
                        • p3c0P Offline
                          p3c0P Offline
                          p3c0
                          Moderators
                          wrote on last edited by
                          #12

                          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.

                          157

                          1 Reply Last reply
                          0
                          • p3c0P Offline
                            p3c0P Offline
                            p3c0
                            Moderators
                            wrote on last edited by
                            #13

                            @
                            anchors.fill: parent
                            speed: xte

                            mouseArea.onClicked: {
                            ms.cppSlot(xte) //works
                            //Qt.quit();
                            qmlSignal("yeah!!!") //works
                            }
                            @

                            What does not work here ?

                            157

                            1 Reply Last reply
                            0

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved