Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we emit a signal from a public slot

Tags:

c++

qt

class MyMainWindow:public QMainWindow {
    public:
        MyWindow* myWindow() { return myWindow ;}
    private:
        MyWindow* myWindow;
};

class MyWindow:public Qobject {
    private slot:
        void mySlot();
};

class MyWindow2: class QWidget {
    public slot:
        void refreshClick();
    signals:
        signal1();
};

MyWindow2::MyWindow2(QMainWindow* parent) {
    QPushButton* refresh;
    QObject::connect(refresh,SIGNAL(clicked()), this, SLOT(refreshClicked()));

    if(parent) {
         QObject::connect(this,SIGNAL(signal1),parent->myWindow(),SLOT(mySlot));
    }

}

void MyWindow2::refreshClicked(){
    emit signal1();
}

I want to know if it is legal to emit signal1 from slot refreshClicked and also are there any cons of emitting a signal from within a slot

like image 539
TechEnthusiast Avatar asked Feb 08 '23 10:02

TechEnthusiast


1 Answers

Yes, it is perfectly ok. But if your only goal is to "forward" a signal, you can also connect your "incoming" signal directly to the signal you are emmitting. eg.:

connect(advisor   , SIGNAL(hasAdvice()),
        this      , SIGNAL(executeAdvice())
       )

But keep in mind that this does not always benefit the extendablity of your code.

like image 154
David van rijn Avatar answered Feb 15 '23 01:02

David van rijn