通过 Qt 中的信号/槽设置传递类

发布于 2024-10-13 20:41:02 字数 756 浏览 4 评论 0原文

我正在尝试获取插槽/信号设置接收端的几个类成员变量的信息,因此我想传递整个类。不幸的是,类传递完毕后,成员变量似乎是空的。这是一些代码片段:

这设置了传递类的信号

signals:
    void selected(const ControlIcon *controlIcon);

这是槽/信号连接

connect(controllerList->serialController, SIGNAL(selected(const ControlIcon*)),
        infoView, SLOT(serialControllerSelected(const ControlIcon*)));

我从要传递的类中发出信号

emit selected(this);

这是调用类成员数据的代码 标签

QLabel *ASCIIStringHolder = new QLabel;
ASCIIStringHolder->setText(controlIcon->m_ASCIIString);

中没有显示任何内容,当我设置断点时,我可以看到 m_ASCIIString 内没有任何内容。

我首先确保它被分配了一些文本,但这不是问题。我还尝试了使用和不使用 const 的信号/槽设置。

任何帮助将不胜感激。

I'm trying to get the information of several of a class' member variables on the receiving end of a slot/signal setup, so I'd like to pass the entire class through. Unfortunately, after the class has been passed, the member variables seem to be empty. Here's some code snippets:

This sets up the signal to pass the class

signals:
    void selected(const ControlIcon *controlIcon);

this is the slot/signal connection

connect(controllerList->serialController, SIGNAL(selected(const ControlIcon*)),
        infoView, SLOT(serialControllerSelected(const ControlIcon*)));

I emit the signal from within the class to be passed

emit selected(this);

Here's the code to call on the class' member data

QLabel *ASCIIStringHolder = new QLabel;
ASCIIStringHolder->setText(controlIcon->m_ASCIIString);

Nothing shows up in the label, and when I set a breakpoint, I can see that there's nothing inside m_ASCIIString.

I looked to make sure that it was being assigned some text in the first place, and that's not the problem. I also tried the signal/slot setup with and without const.

Any help would be appreciated.

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

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

发布评论

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

评论(3

溺渁∝ 2024-10-20 20:41:02

Qt 信号/槽机制需要有关您的自定义类型的元信息,以便能够在发出的信号中发送它们。
为此,请使用 qRegisterMetaType("MyDataType"); 注册您的类型,

请参阅官方 QMetaType 文档 了解更多相关信息。

Qt signal/slot mechanism needs metainformation about your custom types, to be able to send them in emitted signals.
To achieve that, register your type with qRegisterMetaType<MyDataType>("MyDataType");

Consult official QMetaType documentation for more information about this.

与君绝 2024-10-20 20:41:02

首先,由于您使用的是自动连接,发送者和接收者是否都位于同一个线程中?如果不是,则可能会发生呼叫排队的情况,并且当呼叫到达时,发送方中的数据已经被修改。您可以尝试使用直接连接来确保这不是问题。

其次,只是为了好玩,您是否尝试通过在插槽内使用 qobject_cast(sender()) 来访问发送者?如果信号不将其作为参数传递,通常会这样做。像这样:

QLabel *ASCIIStringHolder = new QLabel;
// this is instead of the argument to the slot:
ControlIcon *controlIcon = qobject_cast<ControlIcon*>(sender());
ASCIIStringHolder->setText(controlIcon->m_ASCIIString); 

First, since you are using an auto connection, do both sender and receiver live in the same thread? If not, it could happen that the call is queued and when it arrives, the data in the sender was already modified. You could try to use a direct connection just to make sure this isn't the problem.

Second, just for the fun of it, did you try to access the sender by using qobject_cast<ControlIcon*>(sender()) within the slot? This is how it is usually done if the signal doesn't pass this as an argument. Like this:

QLabel *ASCIIStringHolder = new QLabel;
// this is instead of the argument to the slot:
ControlIcon *controlIcon = qobject_cast<ControlIcon*>(sender());
ASCIIStringHolder->setText(controlIcon->m_ASCIIString); 
风轻花落早 2024-10-20 20:41:02

信号不能声明为传递一个类,然后实际传递该类的子类。我将信号、插槽和 connect() 更改为 SerialController(ControllerIcon 的子级),一切正常。

The signal can't be declared to be passing a class and then actually pass the child of that class. I changed the signal, slot, and connect() to be SerialController (the child of ControllerIcon), and everything worked fine.

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