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. How do you use C++ Interfaces (pure virtual class) in QML
QtWS25 Last Chance

How do you use C++ Interfaces (pure virtual class) in QML

Scheduled Pinned Locked Moved QML and Qt Quick
qmlc++interface
8 Posts 5 Posters 4.1k 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.
  • B Offline
    B Offline
    Buttink
    wrote on last edited by
    #1

    I am trying to make an interface that a QObject implements and assign that to a property. This is very close to the coercion example here http://doc.qt.io/qt-5/qtqml-referenceexamples-coercion-example.html . The only difference is that I don't want Person to be an actual class I want person to be an interface, defined like

    class IPerson
    {
    public:
        virtual ~Person=default;
        virtual QString name()=0;
        virtual void setName(QString)=0;
       // ... etc
    }
    
    Q_DECLARE_INTERFACE(IPerson, "IPerson")
    
    

    Is there any way to do this? I could just make a QObject and make the constructor protected, but I really would like to program to interfaces and not concrete classes if at all possible. If someone knows how to get QML to assign an object to an property who's type is a interface, I would love an example.

    Q 1 Reply Last reply
    0
    • B Buttink

      I am trying to make an interface that a QObject implements and assign that to a property. This is very close to the coercion example here http://doc.qt.io/qt-5/qtqml-referenceexamples-coercion-example.html . The only difference is that I don't want Person to be an actual class I want person to be an interface, defined like

      class IPerson
      {
      public:
          virtual ~Person=default;
          virtual QString name()=0;
          virtual void setName(QString)=0;
         // ... etc
      }
      
      Q_DECLARE_INTERFACE(IPerson, "IPerson")
      
      

      Is there any way to do this? I could just make a QObject and make the constructor protected, but I really would like to program to interfaces and not concrete classes if at all possible. If someone knows how to get QML to assign an object to an property who's type is a interface, I would love an example.

      Q Offline
      Q Offline
      Quteroid
      wrote on last edited by
      #2

      @Buttink
      although I cannot rule out possible existence of some very tricky ways to achieve this, I doubt it's possible. Keep in mind that although QML is implemented in C++, its actual application is more tightly coupled with JavaScript. And in JavaScript there are no such things like interfaces or abstract classes (or classes at all). At least not natively. It's Java that has both. Some aspects of their behavior can be emulated in JavaScript but typically not without involving object instantiations of some kind.

      1 Reply Last reply
      0
      • C Offline
        C Offline
        c64zottel
        wrote on last edited by
        #3

        Hello,

        are there any new on this topic?

        1 Reply Last reply
        0
        • mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi
          I dont think its possible to use a pure virtual class with QML.
          I could be wrong however,
          You could try to ask on the mailing list.
          http://lists.qt-project.org/mailman/listinfo/development

          C 1 Reply Last reply
          0
          • mrjjM mrjj

            Hi
            I dont think its possible to use a pure virtual class with QML.
            I could be wrong however,
            You could try to ask on the mailing list.
            http://lists.qt-project.org/mailman/listinfo/development

            C Offline
            C Offline
            c64zottel
            wrote on last edited by
            #5

            @mrjj Do you think those people don't look at the forum?
            Serious question, because I just don't want to double (actually triple) post.

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

              Hi,

              This is a community driven user forum, so no they are not monitoring it.

              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
              1
              • C c64zottel

                @mrjj Do you think those people don't look at the forum?
                Serious question, because I just don't want to double (actually triple) post.

                mrjjM Offline
                mrjjM Offline
                mrjj
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @c64zottel
                It is fair to say they are not very active here and it is ok to ask directly.
                we do suggest that from time to time.

                I tried to google a bit but saw no answer regarding QML.

                C 1 Reply Last reply
                1
                • mrjjM mrjj

                  @c64zottel
                  It is fair to say they are not very active here and it is ok to ask directly.
                  we do suggest that from time to time.

                  I tried to google a bit but saw no answer regarding QML.

                  C Offline
                  C Offline
                  c64zottel
                  wrote on last edited by
                  #8

                  @mrjj Ok, then I give it a try.

                  Nice starfield at your website btw.

                  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