Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Interface for class in Qt. How?

Interface for class in Qt. How?

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 2 Posters 1.3k 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
    bogong
    wrote on last edited by
    #1

    Hello all!
    Is there something special in Qt if I am going to define interface class in Qt? Or it's better to deal through the class inheritance and reload methods if need? The question is only about Qt not about pure C++.

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by Chris Kawa
      #2

      The question is only about Qt not about pure C++.

      Qt is pure C++ so I'm not sure what you mean. You can have interface (pure virtual class to be exact) class and have a QObject derived class that inherits from it.

      B 1 Reply Last reply
      1
      • Chris KawaC Chris Kawa

        The question is only about Qt not about pure C++.

        Qt is pure C++ so I'm not sure what you mean. You can have interface (pure virtual class to be exact) class and have a QObject derived class that inherits from it.

        B Offline
        B Offline
        bogong
        wrote on last edited by bogong
        #3

        @Chris-Kawa I mean to use Q_DECLARE_INTERFACE(Interface, "Interface") and Q_INTERFACE in object (something like this) or using direct class inheritance when I am using empty methods prototyping in template class and reload them in class that I am using instead of first way. What is better to use in qt?

        Chris KawaC 1 Reply Last reply
        0
        • B bogong

          @Chris-Kawa I mean to use Q_DECLARE_INTERFACE(Interface, "Interface") and Q_INTERFACE in object (something like this) or using direct class inheritance when I am using empty methods prototyping in template class and reload them in class that I am using instead of first way. What is better to use in qt?

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @bogong Sorry, I'm still not sure what you mean by "reloading class methods". Using Q_DECLARE_INTERFACE registers given class in Qt's meta system. Using Q_INTERFACES tells the meta-system that your class implements that interface. Both those things combined allow you to use qobject_cast to cast your class to that interface.

          There's no better or worse. The question is do you need that functionality for something. If not then don't pay for what you don't use. That's the mantra of C++.

          B 1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            @bogong Sorry, I'm still not sure what you mean by "reloading class methods". Using Q_DECLARE_INTERFACE registers given class in Qt's meta system. Using Q_INTERFACES tells the meta-system that your class implements that interface. Both those things combined allow you to use qobject_cast to cast your class to that interface.

            There's no better or worse. The question is do you need that functionality for something. If not then don't pay for what you don't use. That's the mantra of C++.

            B Offline
            B Offline
            bogong
            wrote on last edited by bogong
            #5

            @Chris-Kawa I need to implement interface/abstract class. I am seeking solution for it IN QT. There are options to use:

            • through Q_INTERFACE declaration
            • through defining template class within virtual methods and inheriting it directly without Q_INTERFACE and override

            The question what is better to use in QT?

            Sorry wrong term. Not reload, I mean override.

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #6

              It's not one or the other. It's one on top of the other.

              You can do

              class Foo : public QObject, public SomeInterface
              {
                  Q_OBJECT
              }
              

              and that does one thing. Then you can add

              class Foo : public QObject, public SomeInterface
              {
                  Q_OBJECT
                  Q_INTERFACES(SomeInterface)
              }
              

              and that adds some extra functionality (qobject_cast for example).

              In both cases you need to inherit from that interface class and in both cases you need to override the virtual methods of that interface. The only thing it does is adds more meta-data info about your class which enables extra functionality, so again - there's no better or worse. It's just a question of do you need it.

              B 1 Reply Last reply
              2
              • Chris KawaC Chris Kawa

                It's not one or the other. It's one on top of the other.

                You can do

                class Foo : public QObject, public SomeInterface
                {
                    Q_OBJECT
                }
                

                and that does one thing. Then you can add

                class Foo : public QObject, public SomeInterface
                {
                    Q_OBJECT
                    Q_INTERFACES(SomeInterface)
                }
                

                and that adds some extra functionality (qobject_cast for example).

                In both cases you need to inherit from that interface class and in both cases you need to override the virtual methods of that interface. The only thing it does is adds more meta-data info about your class which enables extra functionality, so again - there's no better or worse. It's just a question of do you need it.

                B Offline
                B Offline
                bogong
                wrote on last edited by
                #7

                @Chris-Kawa said in Interface for class in Qt. How?:

                and that adds some extra functionality (qobject_cast for example)

                Thx ... This point is what I've been missing. Issue closed.

                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