我们可以从公共插槽发出信号吗?

时间:2016-03-22 09:22:09

标签: 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();
}

我想知道从插槽refreshClicked发出signal1是否合法,还有从插槽内发出信号的任何缺点

1 个答案:

答案 0 :(得分:6)

是的,完全没问题。但如果您的唯一目标是“转发”信号,您还可以将“输入”信号直接连接到您正在发出的信号。例如:

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

但请记住,这并不总是有益于代码的可扩展性。