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. [Resolved] Interfaces and Signal/Slot functionality
Forum Updated to NodeBB v4.3 + New Features

[Resolved] Interfaces and Signal/Slot functionality

Scheduled Pinned Locked Moved General and Desktop
6 Posts 4 Posters 3.4k Views 1 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.
  • V Offline
    V Offline
    videl
    wrote on last edited by
    #1

    Hello,
    I'm trying to create an interface to separate program core from user interface.

    My idea is to create an interface for user frontend, let's say
    @class Interface : public QObject
    {
    Q_OBJECT
    signals:
    void userWantsThis();
    void userWantsThat();
    public slots:
    void coreSaysThis();
    void coreSaysThat();
    }@

    Now, programmer could create the front-end:
    @class Window : public QMainWindow, public Interface {
    // ...
    }@

    But AFAIK it won't compile, because Qt, I believe moc to be more specific, doesn't let programmer to inherit QObject more than once (since both QMainWindow and Interface are QObject derivatives).

    I have an idea to create such separation via creating signal slot adapter:
    @class Adapter : public QObject {
    Q_OBJECT
    public:
    void emitCoreSaysThis() { emit coreSaysThis(); }
    void emitCoreSaysThat() { emit coreSaysThat(); }
    signals:
    void userWantsThis();
    void userWantsThat();
    void coreSaysThis();
    void coreSaysThat();
    }@

    and connecting with frontend to it by signal/slot mechanism.

    But I think it's not the elegant solution.

    Is there any good practice in creating such ( core | frontend ) separation?

    1 Reply Last reply
    0
    • K Offline
      K Offline
      KA51O
      wrote on last edited by
      #2

      Simple solution would be to make your interface class inherit from QWidget for example. This of course only works if QWidget (or QMainWindow) is not to specific for your intended uses.

      Another option would be to use your interface class (which is basically a wrapper for QObject) as a member var of the class that will use it.

      I think that the QObject class always has to be mentioned first in the deriving hierachy. Its just a guess but maybe this:
      @
      class Window : public Interface, public QMainWindow {
      // ...
      }
      @
      is already a solution to your problem.

      1 Reply Last reply
      0
      • W Offline
        W Offline
        witekt
        wrote on last edited by
        #3

        Use eg. boost::signal in the interface.

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

          Hi,

          By design, shouldn't your Window class use the interface rather than be an interface ?

          And for the inheritance problem, no, it's not order dependent (although if you inherit from a QObject, it must be first in the list), you can't inherit from two QObjects (QWidget is a QObject)

          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
          • V Offline
            V Offline
            videl
            wrote on last edited by
            #5

            Thanks, that solves the problem.

            1 Reply Last reply
            0
            • K Offline
              K Offline
              KA51O
              wrote on last edited by
              #6

              [quote author="SGaist" date="1361962765"]
              And for the inheritance problem, no, it's not order dependent (although if you inherit from a QObject, it must be first in the list), you can't inherit from two QObjects (QWidget is a QObject)[/quote]

              Thanks for clarifying SGaist. That reminded me of the multiple inheritance problem with a pure virtual class. See "this thread about a similar problem":http://qt-project.org/forums/viewthread/20044. Maybe this could be another useful approach for you videl.

              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