使用信号和插槽更新指针 [英] Updating pointer using signals and slots

查看:119
本文介绍了使用信号和插槽更新指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Qt的新手;请帮助我解决问题。

I am very new to Qt; please help me to solve the problem.

我使用线程在后台执行密集操作。同时我想更新UI,所以我使用SIGNALS和SLOTS。更新UI我发出一个信号并更新UI。

I am using a thread to perform intensive operations in the background. Meanwhile I want to update the UI, so I am using SIGNALS and SLOTS. To update UI I emit a signal and update UI.

让我们来看下面的示例代码,

Let us consider below sample code,

struct sample
{
    QString name;
    QString address;
};

void Update(sample *);

void sampleFunction()
{
    sample a;
    a.name = "Sachin Tendulkar";
    a.address = "India"
    emit Update(&a);
}

在上面的代码中,我们创建一个局部对象,局部对象。在Qt文档中,它说当我们发出一个信号时,它将被放置在队列中,并且迟到它将被传递到窗口。因为我的对象在局部范围内,所以一旦超出范围就会被删除。

In the above code we are creating a local object and passing the address of a local object. In the Qt document, it says that when we emit a signal it will be placed in the queue and late it will be delivered to the windows. Since my object is in local scope it will be delete once it goes out of the scope.

有没有办法在信号中发送指针?

Is there a way to send a pointer in a signal?

推荐答案

你坚持做错了什么,为什么?只需发送Sample本身:

You're insisting on doing the wrong thing, why? Just send the Sample itself:

void Update(sample);
//...
sample a("MSalters", "the Netherlands");
emit Update(a);

这篇关于使用信号和插槽更新指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆