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