Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [Solved] Receiving signals from QML to C++
Forum Updated to NodeBB v4.3 + New Features

[Solved] Receiving signals from QML to C++

Scheduled Pinned Locked Moved QML and Qt Quick
19 Posts 6 Posters 12.5k Views 1 Watching
  • 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.
  • S Offline
    S Offline
    spode
    wrote on last edited by
    #7

    thank you a lot. it is 0k! =)

    1 Reply Last reply
    0
    • S Offline
      S Offline
      spode
      wrote on last edited by
      #8

      this time, why can not i call leggimi()?
      @ #include <QtGui/QApplication>
      #include "qmlapplicationviewer.h"
      #include "funzioni.h"
      #include <QDeclarativeContext>
      #include <QGraphicsObject>

      int main(int argc, char *argv[])
      {
      QApplication app(argc, argv);

      QmlApplicationViewer viewer;
      viewer.setOrientation(QmlApplicationViewer::ScreenOrientationAuto);
      viewer.setMainQmlFile&#40;QLatin1String("qml/fileTrenner2/main.qml"&#41;&#41;;
      viewer.showExpanded();
      viewer.setWindowTitle("File Trenner");
      viewer.move(600,20);
      
      Funzioni funz;
      QDeclarativeView view(QUrl::fromLocalFile&#40;"qml/fileTrenner2/main.qml"&#41;&#41;;
      view.rootContext()->setContextProperty("myObject", &funz);
      
      return app.exec&#40;&#41;;
      

      }
      @

      @ //funzioni.cpp
      #include "funzioni.h"

      Funzioni::Funzioni()
      {

      }

      void Funzioni::leggimi()
      {
      QDeclarativeView view(QUrl::fromLocalFile("qml/fileTrenner2/messaggioLeggimi.qml"));
      view.show();
      }
      @

      @ //funzioni.h
      #ifndef FUNZIONI_H
      #define FUNZIONI_H
      #include <QObject>
      #include <QDeclarativeView>
      #include <QDeclarativeContext>

      class Funzioni : public QObject
      {
      Q_OBJECT

      public:
      Funzioni();
      int caratteriAriga;

      public slots:
      void leggimi();

      };

      #endif // FUNZIONI_H
      @

      i proved to call leggimi in main.qml by using "Image { width: 100; height: 101 MouseArea { anchors.fill:parent; onClicked: myObject.leggimi() } }". error: qml/fileTrenner2/main.qml:416: ReferenceError: Can't find variable: myObject. whY?

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #9

        I see two problems here.

        • At first, your QML file is processed before the context property is added.
          @
          QDeclarativeView view(QUrl::fromLocalFile("qml/fileTrenner2/main.qml"));
          view.rootContext()->setContextProperty("myObject", &funz);
          @ should probably read
          @
          QDeclarativeView view;
          view.rootContext()->setContextProperty("myObject", &funz);
          view.setSource(QUrl::fromLocalFile("qml/fileTrenner2/main.qml"));
          @

        • At second, you add the context property to the root context of view (the QDeclarativeView) and not viewer (the QmlApplicationViewer, which is in fact just a wrapper around another QDeclarativeView). view is actually never shown, the corresponding code can be removed completely.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          spode
          wrote on last edited by
          #10

          bq. QDeclarativeView view;
          view.rootContext()->setContextProperty("myObject", &funz);
          view.setSource(QUrl::fromLocalFile("qml/fileTrenner2/main.qml"));

          thank you. you were very clear, but i do not understand why do you declare the QDeclarativeView making save to explain immediatly the file toward which methods will be applied. i mean: where is the conceptual or tecnical difference between the two snippets?

          i will use the QDeclarativeView.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            loladiro
            wrote on last edited by
            #11

            The qml is being processed as soon as you call setSource (whether directly or indirectly through the constructor). From the documentation:
            [quote]
            source : QUrl
            This property holds the URL of the source of the QML component.
            Changing this property causes the QML component to be reloaded.
            [/quote]
            Therefore, you have to add any special object and context variables before you use setSource. Otherwise they will not be present when (re)loading the qml. Does that make sense to you now?

            1 Reply Last reply
            0
            • S Offline
              S Offline
              spode
              wrote on last edited by
              #12

              this is an excellent answer! ;)

              1 Reply Last reply
              0
              • S Offline
                S Offline
                spode
                wrote on last edited by
                #13

                ideas for closing the program?

                1 Reply Last reply
                0
                • L Offline
                  L Offline
                  lgeyer
                  wrote on last edited by
                  #14

                  If you want to close your program from QML use Qt.quit().

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    spode
                    wrote on last edited by
                    #15

                    error: Signal QDeclarativeEngine::quit() emitted, but no receivers connected to handle it.

                    1 Reply Last reply
                    0
                    • L Offline
                      L Offline
                      lgeyer
                      wrote on last edited by
                      #16

                      You have to connect the QDeclarativeEngine::quit() signal to the QApplication::quit() slot.
                      @
                      connect(view->engine(), SIGNAL(quit()), QCoreApplication::instance(), SLOT(quit()));
                      @

                      Don't forget to add "[Solved]" to topic titles which have been solved for you.

                      1 Reply Last reply
                      0
                      • S Offline
                        S Offline
                        sfilippidis
                        wrote on last edited by
                        #17

                        [OFFTOPIC]
                        I would kindly like to propose to change the word "Reciving" in the title of the thread to "Receiving". :-)
                        [/OFFTOPIC]

                        https://www.filippidis.name/

                        1 Reply Last reply
                        0
                        • S Offline
                          S Offline
                          spode
                          wrote on last edited by
                          #18

                          so, that was made!
                          how to choose the solver in this thread?

                          1 Reply Last reply
                          0
                          • L Offline
                            L Offline
                            lgeyer
                            wrote on last edited by
                            #19

                            [quote author="spode" date="1308387674"]so, that was made!
                            how to choose the solver in this thread?[/quote]

                            Um... what?

                            If the question is related to this [quote author="Lukas Geyer" date="1308340073"]Don't forget to add "[Solved]" to topic titles which have been solved for you.[/quote] just click the edit button on your first post an change the title from "Receiving signals from QML to C++" to "[Solved] Receiving signals from QML to C++".

                            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