简体   繁体   English

QT中Mainwindow发出的信号

[英]Emitting signal from Mainwindow in QT

I've seen alot of examples online about signals and slots, but none that show you how to emit a signal from your mainwindow class and connect into another slot of another window class. 我在网上看到了很多有关信号和插槽的示例,但是没有一个示例向您展示如何从您的mainwindow类发出信号并连接到另一个窗口类的另一个插槽。 Assume the signal emitted from the mainwindow is a bool type and if it's a 1, I want to connect it to another slot from some other class. 假设从主窗口发出的信号是布尔型,如果是1,我想将其连接到其他类的另一个插槽。 I've always seen it done the other way around. 我一直看到它以相反的方式完成。 Can someone explain the most efficient way to accomplish this? 有人可以解释实现此目标的最有效方法吗?

First, you need to inherit your own mainwindow, then add the Q_OBJECT macro and a signals section: 首先,您需要继承自己的主窗口,然后添加Q_OBJECT宏和signals部分:

class myMainWindow : public QMainWindow
{
    Q_OBJECT

signals:

    void mySignal(bool someValue);

}

When you want to signal to be executed in your window code, you'd use 当您希望在窗口代码中执行信号时,可以使用

emit mySignal(true);  // or false....

Then, you connect as usual: 然后,您照常连接:

connect(myWindowInstace, SIGNAL(mySignal(bool), someOtherWidget, SLOT(takesMySignal(bool));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM