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. How to inherit QObject based abstract class in two QWidget/QGraphicsScene views
Forum Updated to NodeBB v4.3 + New Features

How to inherit QObject based abstract class in two QWidget/QGraphicsScene views

Scheduled Pinned Locked Moved General and Desktop
model-viewqobjectslots
4 Posts 3 Posters 3.5k 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.
  • Resurr3ctionR Offline
    Resurr3ctionR Offline
    Resurr3ction
    wrote on last edited by
    #1

    I have a custom MVC classes and I want the views to either inherit QWidget or QGraphicsScene. Both should however also inherit from abstract view class that automatically handles the connection to the underlying model and provides several virtual slots to be implemented in an actual view class.

    However there seems to be no obvious way to do it because both QWidget/QGraphicsScene inherits QObject and my abstract view class must inherit QObject as well to be able to handle signals and slots.

    I think I can only duplicate the code in two view classes that inherit the QWidget and QGraphicsScene. Or is there any other way?

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

      Hi
      virtual inheritance will not work ?
      https://en.wikipedia.org/wiki/Virtual_inheritance

      Never tried it with Qt though so I have no idea with moc / signal / slot.

      JKSHJ 1 Reply Last reply
      0
      • mrjjM mrjj

        Hi
        virtual inheritance will not work ?
        https://en.wikipedia.org/wiki/Virtual_inheritance

        Never tried it with Qt though so I have no idea with moc / signal / slot.

        JKSHJ Offline
        JKSHJ Offline
        JKSH
        Moderators
        wrote on last edited by
        #3

        Hi,

        @Resurr3ction said:

        I want the views to either inherit QWidget or QGraphicsScene. Both should however also inherit from abstract view class

        QAbstractItemView already inherits QWidget, so if you inherit QAbstractItemView you already get all of QWidget's methods.

        QGraphicsScene is a "data store", not a visual object. Your view should not inherit QGraphicsScene. Use composition, not inheritance.

        However there seems to be no obvious way to do it because both QWidget/QGraphicsScene inherits QObject and my abstract view class must inherit QObject as well to be able to handle signals and slots.

        If you inherit QWidget, then your custom class is also a QObject so it can use signals and slots.

        @mrjj said:

        virtual inheritance will not work ?
        https://en.wikipedia.org/wiki/Virtual_inheritance

        Never tried it with Qt though so I have no idea with moc / signal / slot.

        Unfortunately, that won't work. You currently can't inherit from multiple QObjects.

        Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

        mrjjM 1 Reply Last reply
        0
        • JKSHJ JKSH

          Hi,

          @Resurr3ction said:

          I want the views to either inherit QWidget or QGraphicsScene. Both should however also inherit from abstract view class

          QAbstractItemView already inherits QWidget, so if you inherit QAbstractItemView you already get all of QWidget's methods.

          QGraphicsScene is a "data store", not a visual object. Your view should not inherit QGraphicsScene. Use composition, not inheritance.

          However there seems to be no obvious way to do it because both QWidget/QGraphicsScene inherits QObject and my abstract view class must inherit QObject as well to be able to handle signals and slots.

          If you inherit QWidget, then your custom class is also a QObject so it can use signals and slots.

          @mrjj said:

          virtual inheritance will not work ?
          https://en.wikipedia.org/wiki/Virtual_inheritance

          Never tried it with Qt though so I have no idea with moc / signal / slot.

          Unfortunately, that won't work. You currently can't inherit from multiple QObjects.

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

          @JKSH
          Ah, moc assumes first class only. So its simply not supported.
          Thats good to know.

          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