Emit Signal in Constructor of Qml Registered Type doesn't WorkExposing an enum in a QML signalHow to catch C++ signal in QML signal handler after type registration?Accessing properties of a C++ object passed as signal parameter to QMLHow to call slot with QFlags argument from QMLManage the lifetime of C++ QObjects passed to QML in SignalsAfter instantiating c++ object in QML its parent is nullQt Qml connect to a signal of a QObject property of a Context PropertyConnecting callback to QML Singleton Type declared in C++C++ signal not catched on QML sideHow can i use Model c++ class in cascade with 2 other repeater in .qml file

Compelling story with the world as a villain

Did it used to be possible to target a zone?

Can't stopover at Sapporo when going from Asahikawa to Chitose airport?

Prove your innocence

Is it safe to remove the bottom chords of a series of garage roof trusses?

Can realistic planetary invasion have any meaningful strategy?

Attaching a piece of wood to a necklace without drilling

C++20 constexpr std::copy optimizations for run-time

Fried gnocchi with spinach, bacon, cream sauce in a single pan

LeetCode: Pascal's Triangle C#

Are illustrations in novels frowned upon?

In the MCU, why does Mjölnir retain its enchantments after Ragnarok?

What is the history of the university asylum law?

Is it possible to get crispy, crunchy carrots from canned carrots?

Why were the crew so desperate to catch Truman and return him to Seahaven?

Checking a beta regression model via glmmTMB with DHARMa package

How to use "Du hast/ Du hattest'?

Are modern clipless shoes and pedals that much better than toe clips and straps?

Does travel insurance for short flight delays exist?

Irish Snap: Variant Rules

Defense against attacks using dictionaries

How to prevent cutting edges on my TV, HDMI-connected?

Avoiding racist tropes in fantasy

Why is my Earth simulation slower than the reality?



Emit Signal in Constructor of Qml Registered Type doesn't Work


Exposing an enum in a QML signalHow to catch C++ signal in QML signal handler after type registration?Accessing properties of a C++ object passed as signal parameter to QMLHow to call slot with QFlags argument from QMLManage the lifetime of C++ QObjects passed to QML in SignalsAfter instantiating c++ object in QML its parent is nullQt Qml connect to a signal of a QObject property of a Context PropertyConnecting callback to QML Singleton Type declared in C++C++ signal not catched on QML sideHow can i use Model c++ class in cascade with 2 other repeater in .qml file






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I am setting up a resource manager class for my application. to manage all state of resources, i need to emmit a Signal from constructor if it don't succeed to catch resource.



In fact i want to emit signal from constructor of QObject Derived Class that registered for qml via qmlRegisterType.



this is code i have tested on Linux runnng MySql and Qt 5.12.2. but the emit signal doesn't work.



myresoureces.cpp ---- my Class that manage resources



MyResource::MyResource(QObject *parent) : QObject(parent)

if(!openResource())
// qDebug() << "Check Permission of FileSystem For Example.";
emit openResourceFailed("Check Permission of FileSystem For Example.");



bool MyResource::openResource()

// on situation opening resource failed
return false;





main.qml ---- usage of it in qml



// ...
import My.Company.Core 1.0
// ...

MyResource
onOpenResourceFailed:
msgDialog.title = "Open Resource Failed!"
msgDialog.text = error
msgDialog.open()



MessageDialog
id: msgDialog


// ...



main.cpp ---- where i register the class



qmlRegisterType<MyResource>("My.Company.Core", 1, 0, "MyResource");



I expect the Message Dialog to be opened but nothing happened.










share|improve this question
































    1















    I am setting up a resource manager class for my application. to manage all state of resources, i need to emmit a Signal from constructor if it don't succeed to catch resource.



    In fact i want to emit signal from constructor of QObject Derived Class that registered for qml via qmlRegisterType.



    this is code i have tested on Linux runnng MySql and Qt 5.12.2. but the emit signal doesn't work.



    myresoureces.cpp ---- my Class that manage resources



    MyResource::MyResource(QObject *parent) : QObject(parent)

    if(!openResource())
    // qDebug() << "Check Permission of FileSystem For Example.";
    emit openResourceFailed("Check Permission of FileSystem For Example.");



    bool MyResource::openResource()

    // on situation opening resource failed
    return false;





    main.qml ---- usage of it in qml



    // ...
    import My.Company.Core 1.0
    // ...

    MyResource
    onOpenResourceFailed:
    msgDialog.title = "Open Resource Failed!"
    msgDialog.text = error
    msgDialog.open()



    MessageDialog
    id: msgDialog


    // ...



    main.cpp ---- where i register the class



    qmlRegisterType<MyResource>("My.Company.Core", 1, 0, "MyResource");



    I expect the Message Dialog to be opened but nothing happened.










    share|improve this question




























      1












      1








      1


      0






      I am setting up a resource manager class for my application. to manage all state of resources, i need to emmit a Signal from constructor if it don't succeed to catch resource.



      In fact i want to emit signal from constructor of QObject Derived Class that registered for qml via qmlRegisterType.



      this is code i have tested on Linux runnng MySql and Qt 5.12.2. but the emit signal doesn't work.



      myresoureces.cpp ---- my Class that manage resources



      MyResource::MyResource(QObject *parent) : QObject(parent)

      if(!openResource())
      // qDebug() << "Check Permission of FileSystem For Example.";
      emit openResourceFailed("Check Permission of FileSystem For Example.");



      bool MyResource::openResource()

      // on situation opening resource failed
      return false;





      main.qml ---- usage of it in qml



      // ...
      import My.Company.Core 1.0
      // ...

      MyResource
      onOpenResourceFailed:
      msgDialog.title = "Open Resource Failed!"
      msgDialog.text = error
      msgDialog.open()



      MessageDialog
      id: msgDialog


      // ...



      main.cpp ---- where i register the class



      qmlRegisterType<MyResource>("My.Company.Core", 1, 0, "MyResource");



      I expect the Message Dialog to be opened but nothing happened.










      share|improve this question
















      I am setting up a resource manager class for my application. to manage all state of resources, i need to emmit a Signal from constructor if it don't succeed to catch resource.



      In fact i want to emit signal from constructor of QObject Derived Class that registered for qml via qmlRegisterType.



      this is code i have tested on Linux runnng MySql and Qt 5.12.2. but the emit signal doesn't work.



      myresoureces.cpp ---- my Class that manage resources



      MyResource::MyResource(QObject *parent) : QObject(parent)

      if(!openResource())
      // qDebug() << "Check Permission of FileSystem For Example.";
      emit openResourceFailed("Check Permission of FileSystem For Example.");



      bool MyResource::openResource()

      // on situation opening resource failed
      return false;





      main.qml ---- usage of it in qml



      // ...
      import My.Company.Core 1.0
      // ...

      MyResource
      onOpenResourceFailed:
      msgDialog.title = "Open Resource Failed!"
      msgDialog.text = error
      msgDialog.open()



      MessageDialog
      id: msgDialog


      // ...



      main.cpp ---- where i register the class



      qmlRegisterType<MyResource>("My.Company.Core", 1, 0, "MyResource");



      I expect the Message Dialog to be opened but nothing happened.







      qt constructor qml qt5 qt-signals






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 17:40









      eyllanesc

      106k12 gold badges38 silver badges70 bronze badges




      106k12 gold badges38 silver badges70 bronze badges










      asked Mar 27 at 16:42









      Aliakbar RashidiAliakbar Rashidi

      159 bronze badges




      159 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          1















          The signals will invoke the methods that are connected at the time of the signal emission, in your case in the constructor is not connected to any slot so the data will be lost, a possible solution is to use a QTimer::singleShot(0, ...) to be emitted a moment after the creation:



          MyResource::MyResource(QObject *parent=nullptr) : QObject(parent)
          if(!openResource())
          QTimer::singleShot(0, this, [this]()
          emit openResourceFailed("Check Permission of FileSystem For Example.");
          );




          Another alternative solution is to use QQmlParserStatus as an interface and emit the signal in the componentComplete() method:



          *.h



          #ifndef MYRESOURCE_H
          #define MYRESOURCE_H

          #include <QObject>
          #include <QQmlParserStatus>

          class MyResource: public QObject, public QQmlParserStatus

          Q_OBJECT
          Q_INTERFACES(QQmlParserStatus)
          public:
          MyResource(QObject *parent=nullptr);
          void classBegin();
          void componentComplete();
          signals:
          void openResourceFailed(const QString & error);
          private:
          bool openResource();
          ;


          #endif // MYRESOURCE_H


          *.cpp



          #include "myresource.h"

          MyResource::MyResource(QObject *parent) : QObject(parent)

          void MyResource::classBegin()

          void MyResource::componentComplete()
          if(!openResource())
          emit openResourceFailed("Check Permission of FileSystem For Example.");



          bool MyResource::openResource()
          // on situation opening resource failed
          return false;






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382426%2femit-signal-in-constructor-of-qml-registered-type-doesnt-work%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1















            The signals will invoke the methods that are connected at the time of the signal emission, in your case in the constructor is not connected to any slot so the data will be lost, a possible solution is to use a QTimer::singleShot(0, ...) to be emitted a moment after the creation:



            MyResource::MyResource(QObject *parent=nullptr) : QObject(parent)
            if(!openResource())
            QTimer::singleShot(0, this, [this]()
            emit openResourceFailed("Check Permission of FileSystem For Example.");
            );




            Another alternative solution is to use QQmlParserStatus as an interface and emit the signal in the componentComplete() method:



            *.h



            #ifndef MYRESOURCE_H
            #define MYRESOURCE_H

            #include <QObject>
            #include <QQmlParserStatus>

            class MyResource: public QObject, public QQmlParserStatus

            Q_OBJECT
            Q_INTERFACES(QQmlParserStatus)
            public:
            MyResource(QObject *parent=nullptr);
            void classBegin();
            void componentComplete();
            signals:
            void openResourceFailed(const QString & error);
            private:
            bool openResource();
            ;


            #endif // MYRESOURCE_H


            *.cpp



            #include "myresource.h"

            MyResource::MyResource(QObject *parent) : QObject(parent)

            void MyResource::classBegin()

            void MyResource::componentComplete()
            if(!openResource())
            emit openResourceFailed("Check Permission of FileSystem For Example.");



            bool MyResource::openResource()
            // on situation opening resource failed
            return false;






            share|improve this answer





























              1















              The signals will invoke the methods that are connected at the time of the signal emission, in your case in the constructor is not connected to any slot so the data will be lost, a possible solution is to use a QTimer::singleShot(0, ...) to be emitted a moment after the creation:



              MyResource::MyResource(QObject *parent=nullptr) : QObject(parent)
              if(!openResource())
              QTimer::singleShot(0, this, [this]()
              emit openResourceFailed("Check Permission of FileSystem For Example.");
              );




              Another alternative solution is to use QQmlParserStatus as an interface and emit the signal in the componentComplete() method:



              *.h



              #ifndef MYRESOURCE_H
              #define MYRESOURCE_H

              #include <QObject>
              #include <QQmlParserStatus>

              class MyResource: public QObject, public QQmlParserStatus

              Q_OBJECT
              Q_INTERFACES(QQmlParserStatus)
              public:
              MyResource(QObject *parent=nullptr);
              void classBegin();
              void componentComplete();
              signals:
              void openResourceFailed(const QString & error);
              private:
              bool openResource();
              ;


              #endif // MYRESOURCE_H


              *.cpp



              #include "myresource.h"

              MyResource::MyResource(QObject *parent) : QObject(parent)

              void MyResource::classBegin()

              void MyResource::componentComplete()
              if(!openResource())
              emit openResourceFailed("Check Permission of FileSystem For Example.");



              bool MyResource::openResource()
              // on situation opening resource failed
              return false;






              share|improve this answer



























                1














                1










                1









                The signals will invoke the methods that are connected at the time of the signal emission, in your case in the constructor is not connected to any slot so the data will be lost, a possible solution is to use a QTimer::singleShot(0, ...) to be emitted a moment after the creation:



                MyResource::MyResource(QObject *parent=nullptr) : QObject(parent)
                if(!openResource())
                QTimer::singleShot(0, this, [this]()
                emit openResourceFailed("Check Permission of FileSystem For Example.");
                );




                Another alternative solution is to use QQmlParserStatus as an interface and emit the signal in the componentComplete() method:



                *.h



                #ifndef MYRESOURCE_H
                #define MYRESOURCE_H

                #include <QObject>
                #include <QQmlParserStatus>

                class MyResource: public QObject, public QQmlParserStatus

                Q_OBJECT
                Q_INTERFACES(QQmlParserStatus)
                public:
                MyResource(QObject *parent=nullptr);
                void classBegin();
                void componentComplete();
                signals:
                void openResourceFailed(const QString & error);
                private:
                bool openResource();
                ;


                #endif // MYRESOURCE_H


                *.cpp



                #include "myresource.h"

                MyResource::MyResource(QObject *parent) : QObject(parent)

                void MyResource::classBegin()

                void MyResource::componentComplete()
                if(!openResource())
                emit openResourceFailed("Check Permission of FileSystem For Example.");



                bool MyResource::openResource()
                // on situation opening resource failed
                return false;






                share|improve this answer













                The signals will invoke the methods that are connected at the time of the signal emission, in your case in the constructor is not connected to any slot so the data will be lost, a possible solution is to use a QTimer::singleShot(0, ...) to be emitted a moment after the creation:



                MyResource::MyResource(QObject *parent=nullptr) : QObject(parent)
                if(!openResource())
                QTimer::singleShot(0, this, [this]()
                emit openResourceFailed("Check Permission of FileSystem For Example.");
                );




                Another alternative solution is to use QQmlParserStatus as an interface and emit the signal in the componentComplete() method:



                *.h



                #ifndef MYRESOURCE_H
                #define MYRESOURCE_H

                #include <QObject>
                #include <QQmlParserStatus>

                class MyResource: public QObject, public QQmlParserStatus

                Q_OBJECT
                Q_INTERFACES(QQmlParserStatus)
                public:
                MyResource(QObject *parent=nullptr);
                void classBegin();
                void componentComplete();
                signals:
                void openResourceFailed(const QString & error);
                private:
                bool openResource();
                ;


                #endif // MYRESOURCE_H


                *.cpp



                #include "myresource.h"

                MyResource::MyResource(QObject *parent) : QObject(parent)

                void MyResource::classBegin()

                void MyResource::componentComplete()
                if(!openResource())
                emit openResourceFailed("Check Permission of FileSystem For Example.");



                bool MyResource::openResource()
                // on situation opening resource failed
                return false;







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 27 at 17:13









                eyllanesceyllanesc

                106k12 gold badges38 silver badges70 bronze badges




                106k12 gold badges38 silver badges70 bronze badges





















                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







                    Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55382426%2femit-signal-in-constructor-of-qml-registered-type-doesnt-work%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                    Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript