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. Testing QML application with C++ context
Forum Update on Monday, May 27th 2025

Testing QML application with C++ context

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
3 Posts 3 Posters 398 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.
  • N Offline
    N Offline
    neophyte
    wrote on last edited by
    #1

    Hello,

    I am working on a QML app, which makes use of a C++ object, passed as global context. The object communicates with hardware via USB, and is expensive to instantiate in terms of time, hence I'd like to be able to test the QML front-end with a dummy object.

    My first idea was to create a mock C++ object and use it during development, but that solution appears too clumsy.

    I am using googletest for the C++ code's automated testing, but what I am trying to do with the QML is not automated testing but more of development aid. Currently I don't think I'll need things like automated signal emitting.

    Is there an easier method to achieve that?

    Thanks

    Thanks

    L 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      Why would a mock object be clumsy ?

      Create an abstract base class that defines the API required, then derive your actual object that communicates with the hardware from it and a second class for the mock that answers whatever is required for the software to run and be tested.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • N neophyte

        Hello,

        I am working on a QML app, which makes use of a C++ object, passed as global context. The object communicates with hardware via USB, and is expensive to instantiate in terms of time, hence I'd like to be able to test the QML front-end with a dummy object.

        My first idea was to create a mock C++ object and use it during development, but that solution appears too clumsy.

        I am using googletest for the C++ code's automated testing, but what I am trying to do with the QML is not automated testing but more of development aid. Currently I don't think I'll need things like automated signal emitting.

        Is there an easier method to achieve that?

        Thanks

        Thanks

        L Offline
        L Offline
        lemons
        wrote on last edited by
        #3

        @neophyte I would prefer the mock object / DTO approach, but you could also use defines within your main objects class.
        --> not very beautiful, but it would work

        e.g.

        #.pro
        DEFINES += MY_DEVICE_EMULATED
        
        // MyDevice.cpp
        // inside some method
        #ifdef MY_DEVICE_EMULATED
            serial = "123456789";
        #else
            callSomeMethodToFetchSerialViaUsbProtocol();
        #endif
        
        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