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. With Qt 5.6 is it possible to avoid the diamond problem when signals and slots are defined in two branches?

With Qt 5.6 is it possible to avoid the diamond problem when signals and slots are defined in two branches?

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 1.7k 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.
  • A Offline
    A Offline
    Alain38 0
    wrote on last edited by
    #1

    Hi,
    I would write a class A that implements some functions and declares some signals and slots. In my program, this class will be inherited by several other classes. Unfortunately, in a lot of cases these classes are already a QObject. So I'm facing about the classical diamond problem from QObject.

    Before, I worked with QT 4.8 that had no nice solution to this problem. But I do not know if a solution has been found in QT 5.6.

    For the moment I have a (bad) solution consisting in:

    class A
    {
    public:
    virtual void sigToto() = 0;
    virtual QObject * asQObject() = 0;
    protected:
     virtual void slotToto();
    };
    
    class B: public QObject, public A
    {
    public:
    Q_SIGNAL void  sigToto() override final;
    QObject * asQObject() override final {return this;}
    protected:
    Q_SLOT voif slotToto() override final {A::slotToto();}
    };
    

    And for the connections (performed in class A source code) I have to use the old-style syntax.

    Some news to solve this (endless) problem?

    1 Reply Last reply
    0
    • sierdzioS Offline
      sierdzioS Offline
      sierdzio
      Moderators
      wrote on last edited by
      #2

      You can inherit from QObject in class A, and then not inherit from QObject in class B, only declare Q_OBJECT macro there. Should work (class B will inherit from QObject through class A).

      I guess, though, that you have only simplified a less straightforward situation, where indeed it is impossible to inherit from QObject twice. If so, then unfortunately (or fortunately... multiple inheritance is in general avoided) the situation remains (officially) unchanged - it's not possible and not supported. There are workarounds, however: https://forum.qt.io/topic/11864/multiple-inheritance-from-qobject-qobject-is-an-ambiguous-base-of/12

      (Z(:^

      A 1 Reply Last reply
      2
      • sierdzioS sierdzio

        You can inherit from QObject in class A, and then not inherit from QObject in class B, only declare Q_OBJECT macro there. Should work (class B will inherit from QObject through class A).

        I guess, though, that you have only simplified a less straightforward situation, where indeed it is impossible to inherit from QObject twice. If so, then unfortunately (or fortunately... multiple inheritance is in general avoided) the situation remains (officially) unchanged - it's not possible and not supported. There are workarounds, however: https://forum.qt.io/topic/11864/multiple-inheritance-from-qobject-qobject-is-an-ambiguous-base-of/12

        A Offline
        A Offline
        Alain38 0
        wrote on last edited by
        #3

        Hi @sierdzio,
        Thanks. So it is still impossible to create a base class for a variety of QWidget-derived objects whether this class needs to define generic signals, slots, and connections. We still have to specialize each QWidget_derived object by duplicating common code.

        1 Reply Last reply
        0
        • kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          As long as you stick to pointer-to-member connects it should be possible. Depends on what you intend to do exactly. However I can count on my fingers the number of times that virtual multiple inheritance (i.e. the diamond) is really warranted, usually it is a sign of a bad design.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved