Use c++ factory classes in qml

  • Hi!

    I have 2 factory classes that i implemented in c++. The factories are the same class, but different instances.

    Let's from simplicity say that the both create QPushButtons, but with different properties. How would you use something like this in qml?


  • @stefan.b Can you give a specific use case? QML isn't class based as C++ is and is very dynamic, I don't think there's need for the factory design pattern. If you need QML push buttons with different properties you just create push buttons with different properties.

  • Well this push button example is maybe a bit oversimplified...

    I want to make a HMI for several production machines, where all of them are controlled via 1 application.
    The machine software is written in Beckhoff Twincat 3 - a soft sps. For interfacing with the machines I am using a c library provided by Beckhoff that I nicely wrapped to c++. The c library uses unique keys to establish connections to machines. Then variables of these machines can be read and written. Additionally, notifications can be send from the machines whenever variables are changed within the machine.

    My wrapper uses a factory class for each machine (the factory class has the unique id of a machine as parameter). Variables are implemented as an object as well. For instance, if I want to read a value of a machine I can do something like this

    SpsManager *manager = new SpsManager(''); // create manager class for machine, unique id as parameter
    SpsVariable *variable = manager->connectVariable('MAIN.temperature');

    The SpsVariable class has signals that can be connected to a line edit. (Whenever the variable changes, the text in the line edit will change as well) The variables in the machine can also be of type bool and can so be connected to a QPushButton (for instance, to start the machine)

    The c++ part as described above is implemented and works just fine and if I write the HMI in c++ using qtdesigner everything works. However, I would rather like to write the actual user interface with QML.

    I hoped I could explain my scenario well enough ...

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    So in the end you would need access to the SpsVariable object in your QML code, correct ?

  • I need to create these objects in qml code and zehn connect them to different qml objects via signals.

  • Lifetime Qt Champion

    Then why not make your manager a context object of your QQmlEngine instance and then use it in your QML code to retrieve the objects you need ?

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.