0

I have class A : public QMainwindow, and I add a signal in Class A, just name signal_one(),I have another Class B, just has member QMainwindow* mw, and mw can connect signal_one().

    class A : public QMainWindow
    {
      Q_OBJECT
    Q_signal:
       void signal_one();
    };
    class B
    {
       Q_OBJECT

       B(QMainWindow* mainwindow)
       {
        mw = mainwindow;
        connect(mw,SIGNAL(signal_one()),this,SLOT(slot_one()));
       }
    public slots:
        void slot_one();
    public: 
        QMainWindow* mw;
    };

here is main

#include "A.h"
#include "B.h"

int main(int argc, char **argv)
{
   QApplication app(argc, argv);
   A* a = new A;
   a->show();
   B* b(a);
   emit a->signal_one();
   return app.exec();
}

So,someone can explain this?

L.C.
  • 1,098
  • 10
  • 21
wsnd
  • 1
  • 2
  • I'm not sure I understand the question, but reading this could help: https://stackoverflow.com/questions/2143183/private-public-qt-signals – L.C. Jan 29 '19 at 08:56
  • It's designed that way. It's written in documentation that "you can subclass widgets to add our own signals to them" for example. – Mher Didaryan Jan 29 '19 at 09:20

2 Answers2

1

To use Qt signal-slot connection both connected class has to be inherited from QObject. In your code class A is implicitly inherited from QObject. But class B is not. This code works:

class A : public QMainWindow
{
signals:
    void signal_one();
};
class B : public QObject
{
    Q_OBJECT
    B(A* mainwindow)
    {
        mw = mainwindow;
        connect(mainwindow, SIGNAL(signal_one()), this, SLOT(slot_one()));
    }
public slots:
    void slot_one();

public:
    QMainWindow* mw;
};

P.S. Made some changes to make the signal available.

Konstantin T.
  • 994
  • 8
  • 22
  • But, mw doesn't have signal_one() – wsnd Jan 29 '19 at 06:15
  • 2
    If you use the `SIGNAL(signal_one())`-syntax it will use the Metaobject to create the connection on run-time. It might result in a run-time-error if that signal is not available. To have compile time check, you should opt for the new syntax: `connect(mw, &A::signal_one, this, &B::slot_one);` *(if I remember right. It's some time since I last used Qt)* – derM - not here for BOT dreams Jan 29 '19 at 09:59
  • @derM, you'r completely right. I just take this part from the question. Perhaps, author has some reasons. – Konstantin T. Jan 29 '19 at 21:34
0

Without QObject inharitance and Q_OBJECT macros it doesn't work:

// file A.h
class A : public QMainWindow // implicitly inherits QObject
{
    Q_OBJECT // don't forget call this MACROS
signals:
    void signal_one(); // some event in the object must emit this signal
}

// file B.h
class B : public QObject // without this inheritance you cannot get slots/signals behaviour
{
    Q_OBJECT // don't forget this one
public:
    B() {}
public slots:
    void slot_one()
    {
        // get signal
    }
}

// connect an A's object with a B's object somewhere, for example in main.cpp

#include <A.h>
#include <B.h>

int main(int argc, int* argv[])
{
    QApplication a(argc, argv);

    A a;
    B b;
    QObject::connect( &a, SIGNAL(signal_one()), &b, SLOT(slot_one()) );
    // or 
    // QObject::connect(&a, &A::signal_one, &b, &B::slot_one);

    return a.exec(); // we need to run event loop
}
Alexander Chernin
  • 446
  • 2
  • 8
  • 17