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. reuse of custom C++ class
Forum Updated to NodeBB v4.3 + New Features

reuse of custom C++ class

Scheduled Pinned Locked Moved Solved QML and Qt Quick
5 Posts 3 Posters 400 Views 3 Watching
  • 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.
  • A Offline
    A Offline
    ABest2
    wrote on last edited by
    #1

    I use the same C++ chart class multiple times in a QML desktop application. I currently use multiple copies of the class with slightly different names and register them as follows:


    ret = qmlRegisterType<MyClass0>("QwtQuick2", 1, 0, "MyClass0");

    ret = qmlRegisterType<MyClass1>("QwtQuick2", 1, 0, "MyClass1");

    ret = qmlRegisterType<MyClass2>("QwtQuick2", 1, 0, "MyClass2");

    MyClass0 myclass0;
    MyClass1 myclass1;
    MyClass2 myclass2;

    Here I connect the signals and slots to the classes for each chart.


    This method functions as needed but I would prefer to not maintain many copies of the same class. Is there an alternative method?

    Thanks,
    Aaron

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #4

      Are you using these in QML?
      Just do this in C++:

      qmlRegisterType<MyClass>("QwtQuick2", 1, 0, "MyClass");
      

      Then in qml:

      import QwtQuick2 1.0
      ...
      MyClass {
        id: myclass0
      }
      MyClass {
        id: myclass1
      }
      MyClass {
        id: myclass2
      }
      

      You can connect signals in qml via Connections, through on<signal name> callbacks, or actual connect calls to attach to functions in qml.

      What are you connecting to these objects?

      C++ is a perfectly valid school of magic.

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

        Hi,

        What are the differences between these classes ?

        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
        0
        • A Offline
          A Offline
          ABest2
          wrote on last edited by
          #3

          The classes are identical except for the name. I change the name so that I can connect different signals to each instance.

          1 Reply Last reply
          0
          • fcarneyF Offline
            fcarneyF Offline
            fcarney
            wrote on last edited by
            #4

            Are you using these in QML?
            Just do this in C++:

            qmlRegisterType<MyClass>("QwtQuick2", 1, 0, "MyClass");
            

            Then in qml:

            import QwtQuick2 1.0
            ...
            MyClass {
              id: myclass0
            }
            MyClass {
              id: myclass1
            }
            MyClass {
              id: myclass2
            }
            

            You can connect signals in qml via Connections, through on<signal name> callbacks, or actual connect calls to attach to functions in qml.

            What are you connecting to these objects?

            C++ is a perfectly valid school of magic.

            1 Reply Last reply
            1
            • A Offline
              A Offline
              ABest2
              wrote on last edited by
              #5

              Thank you, I will look into what you suggest.
              The signals to the instances contain different QVector arrays of data to be displayed.

              Aaron

              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