Results 1 to 2 of 2

Thread: Error calling C++ function from QML

  1. #1
    Join Date
    Apr 2015
    Posts
    18
    Thanks
    1
    Qt products
    Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Error calling C++ function from QML

    Not quite sure where my error is. I wanted to share an enum and class to Qt...I read that qmlRegisterUncreatableMetaObject does not import properly and experienced that issue myself so the below is what I came up with by I'm receiving an error:

    qrc:/Pages/AddCustomFields.qml:288: Error: Unknown method parameter type: OpsMode::ModeCRF

    Any insight appreciated!

    Thanks,
    -Rich

    ---------------8<--------------------------------------------
    Header:
    Qt Code:
    1. class OpsMode : public QObject {
    2. Q_OBJECT
    3.  
    4. public:
    5. enum ModeCRF : int {ModeIdle = 0, ModeActiveRun, ModeWarming, ModeRemote};
    6. Q_ENUM(ModeCRF)
    7. };
    8.  
    9. class OperationMode : public QObject
    10. {
    11. Q_OBJECT
    12.  
    13. public:
    14. explicit OperationMode(QObject *parent = nullptr);
    15. ~OperationMode();
    16.  
    17. Q_INVOKABLE OpsMode::ModeCRF getCRFMode();
    18. Q_INVOKABLE void setCRFMode(OpsMode::ModeCRF md);
    19.  
    20. private:
    21. OpsMode::ModeCRF CRFMode;
    22. QMutex mutexMode;
    23. };
    To copy to clipboard, switch view to plain text mode 

    C++:
    Qt Code:
    1. void OperationMode::setCRFMode(OpsMode::ModeCRF md)
    2. {
    3. qDebug() << "Set CRF mode!" << md;
    4. QMutexLocker locker(&mutexMode);
    5. CRFMode = md;
    6. }
    7.  
    8. OpsMode::ModeCRF OperationMode::getCRFMode()
    9. {
    10. qDebug() << "Get CRF mode!";
    11. //QMutexLocker locker(&mutexMode);
    12. //return CRFMode;
    13. }
    To copy to clipboard, switch view to plain text mode 

    Example QML:
    Qt Code:
    1. import MyOperationsMode 1.0
    2. import MyOpsMode 1.0
    3.  
    4. CustomOperationsMode {
    5. id: operationsMode
    6. }
    7.  
    8. operationsMode.setCRFMode(CustomOpsMode.ModeIdle)
    To copy to clipboard, switch view to plain text mode 

    Main (registering)

    Qt Code:
    1. OpMode = new OperationMode();
    2. qmlRegisterType<OperationMode>("MyOperationsMode", 1, 0, "CustomOperationsMode");
    3. qmlRegisterUncreatableType<OpsMode>("MyOpsMode", 1, 0, "CustomOpsMode", "ERROR!");
    To copy to clipboard, switch view to plain text mode 


    Added after 17 minutes:


    Thinking this may be related: https://bugreports.qt.io/browse/QTBUG-19741
    Last edited by rhb327; 15th January 2020 at 00:39.

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,490
    Thanks
    252
    Thanked 717 Times in 707 Posts
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Error calling C++ function from QML

    Thinking this may be related
    Looks like it could be. But why do you derive OpsMode from QObject, when all OpsMode serves is as a scope qualifier for your enum? Why not simply declare OpsMode as a namespace? And why does the ModeCRF enum need to be separate from OperationsMode class? Can't you define it within that class?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 2
    Last Post: 20th January 2018, 23:32
  2. calling a function with objects
    By herculis in forum Newbie
    Replies: 3
    Last Post: 28th August 2014, 10:36
  3. compiler error in calling friend function!!??
    By aurora in forum Qt Programming
    Replies: 11
    Last Post: 15th November 2011, 21:25
  4. Calling external DLL function with Qt
    By yaba in forum Newbie
    Replies: 2
    Last Post: 29th September 2011, 07:47
  5. SIGNAL/SLOT Calling the same function. Possible?
    By matthieunc in forum Qt Programming
    Replies: 4
    Last Post: 28th July 2011, 23:24

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.