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. Qt: I defined and registered a QML singleton in C++. How do I access this singleton from C++?
QtWS25 Last Chance

Qt: I defined and registered a QML singleton in C++. How do I access this singleton from C++?

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
singletonc++
3 Posts 3 Posters 3.3k 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.
  • S Offline
    S Offline
    Stefan Monov76
    wrote on 29 Dec 2016, 17:59 last edited by
    #1

    I had no problems with defining and registering a QML singleton in C++, and using that singleton from QML. Now, however, I want to call methods on that singleton from C++ as well. How do I get access to the single instance that the singleton has?

    For now I've done the following hack:

    FileIO* fileIO;
    
    static QObject* fileIOSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine) {
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)
    
        FileIO* instantiatedObj = new FileIO();
        ::fileIO = instantiatedObj;
        return instantiatedObj;
    }
    
    // ...
    
    qmlRegisterSingletonType<FileIO>("FileIO", 1, 0, "FileIO", fileIOSingletonTypeProvider);
    

    So I store a global pointer to it at the point of instantiation.

    This works. Is there a proper way to do it, though?

    1 Reply Last reply
    0
  • P Offline
    P Offline
    p3c0 Moderators
    wrote on 30 Dec 2016, 05:15 last edited by
    #2

    @Stefan-Monov76 Another way would be to assign it to QtObject in QML and the access this property in C++. Something like:

    //QML
    property QtObject obj: MySingleTonObject 
    
    //C++
    QQmlApplicationEngine engine;
    engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
    QObject *obj = qvariant_cast<QObject *>(engine.rootObjects().first()->property("obj"));
    

    157

    1 Reply Last reply
    0
  • S Offline
    S Offline
    Schluchti
    wrote on 30 Dec 2016, 20:36 last edited by
    #3

    @Stefan-Monov76 you could also use something like that:

    class FileIO: public QObject{
        Q_OBJECT
    public:
        //singleton type provider function for Qt Quick
        static QObject* fileIOSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine);
        //singleton object provider for C++
        static FileIO* instance();
        void myFancyFunction();
    };
    
    QObject* FileIO::fileIOSingletonTypeProvider(QQmlEngine *engine, QJSEngine *scriptEngine){
        Q_UNUSED(engine)
        Q_UNUSED(scriptEngine)
        return FileIO::instance();
    }
    
    FileIO* FileIO::instance() {
        static FileIO* fileIO = new FileIO();
        return fileIO;
    }
    
    void FileIO::myFancyFunction(){
       //do something
    }
    

    In that case you could get the singleton, with:

    FileIO::instance()->myFancyFunction();
    

    Want to read more about Qt?

    https://gympulsr.com/blog/qt/

    Latest Article: https://gympulsr.com/blog/qt/2017/06/14/ios-background-music-qt.html

    1 Reply Last reply
    0

1/3

29 Dec 2016, 17:59

topic:navigator.unread, 2
  • Login

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