Qt C++ 中不同类的信号/槽功能

发布于 2024-12-21 07:55:46 字数 150 浏览 1 评论 0原文

对于一个类,我声明了一个槽和一个信号,并且在槽方法定义中,我使用emit关键字调用信号方法。但是我怎样才能将一个类的信号发送到另一个有插槽的类。

好吧,我尝试使用按钮来修改标签文本。按钮由 A 类创建(必须发出信号),标签由 B 类创建,该类必须有一个槽来修改其上的文本

For just one class , i declare a slot and a signal , and in slot method definition i call signal method with emit keyword. But how can i emit signals with one class to another class which has a slot.

Well i try with a button to modify a label text. Button is created by A class (which must emit a signal) , and label is created by class B which must have a slot to modify text on it

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱,才寂寞 2024-12-28 07:55:46

看起来你有类 1,它有一个将被执行的方法,并将调用“emit”。当这种情况发生时,另一个班级的插槽就会发现。

第一类的定义:

class songs_text {
public:
signals:
    void send_signal();
}

int songs_text:function() { 
    emit send_signal();
}

第二类的定义:

class wind {
public slots:
    void continue_job() {
    };
}

以及你的主程序:

Wind wind(); 
Get_source songs_text(&mtempfile);

QObject::connect(&songs_text, SIGNAL(send_signal()),
    &wind, SLOT(continue_job()));

It seems like you have class 1, which has a method that will be executed, and will call "emit". When that happens, the slot of another class will find out.

definition of 1st class:

class songs_text {
public:
signals:
    void send_signal();
}

int songs_text:function() { 
    emit send_signal();
}

definition of class 2:

class wind {
public slots:
    void continue_job() {
    };
}

and your main program:

Wind wind(); 
Get_source songs_text(&mtempfile);

QObject::connect(&songs_text, SIGNAL(send_signal()),
    &wind, SLOT(continue_job()));
慢慢从新开始 2024-12-28 07:55:46

在名为 void emitSignalBlahBlah() 的类中添加一个公共方法,作为 emit 代码的包装器。然后,所有其他需要触发此信号的类将访问此对象并调用该方法来执行此操作。

Add a public method in the class named void emitSignalBlahBlah() to be a wrapper around the emit code. Then, all the other classes that need to fire this signal will access this object and call the method to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文