这是另一个版本的使用方法,我觉得对初学者来说更容易理解
您需要在类中定义信号和槽。
添加到类的标题中,例如向 MainWindow 发出信号,向 Computations 添加槽
public slots:
void something();
signals:
void something_happend();
然后,在您想要使用它的任何地方,在 mainwindow.cpp 中的示例中,您需要连接信号和插槽。通过 QObject::connect 执行此操作:
QObject::connect(who_emit,SIGNAL(what_signal),who_receive,SLOT(what_slot))
例子:
mainwindow.h
signals:
void something_happend();
computations.h
public slots:
void something_happend();
mainwindow.cpp
Computations *c = new Computations(this);
QObject::connect(this,SIGNAL(something_happend()),c,SLOT(something()));
如果你想传递一些参数,你想连接的 SIGNAL 和 SLOT 需要有相同类型的参数:
public slots:
void something(int c);
signals:
void something_happend(int c);
QObject::connect(this,SIGNAL(something_happend(int)),c,SLOT(something(int)));