2

I'm using Qt now.

I've written a C++ class A and there are some public functions in it. And now, I'm writing a Qt class B which has multiple inheritences from both QObject and A.

And I want to change one public function in A to public slots in B.

Can it be possible?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
Rubbish_Oh
  • 155
  • 1
  • 7

1 Answers1

1

You can do that by simply creating a slot in B and delegating to A's function in there.
Here's an example:

#include <QtCore>

class A {
    public:
        A() {}
        void foo() { qDebug() << "In A::foo()"; }
};

class B: public QObject, public A {
    Q_OBJECT

    public:
        B(QObject *parent=0): QObject(parent), A() {
            connect(this, SIGNAL(fire()), this, SLOT(foo()));
        }
    public slots:
        void foo() {
            qDebug() << "In slot B::foo()";
            A::foo();
        }
    signals:
        void fire();
    public:
        void test() { emit fire(); }
};

Class A doesn't need to be "aware" of Qt at all.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • It is definitely the effect I want. But does it have a more direct way to achieve this? – Rubbish_Oh Feb 07 '12 at 07:58
  • I don't think so. Either `A` has a slot or it doesn't. If it doesn't, you'll need to call it through/delegate from `B` somehow. For that, `B` needs a slot. You can't use `using`-directives in `slots` sections, so I don't see how you could bypass a member function in B. (Short of modifying the `moc` output.) – Mat Feb 07 '12 at 08:07