简体   繁体   English

在插槽中使用QMap

[英]Use a QMap in a slot

I want to use a QMap that I have previously created to be used inside a slot. 我想使用以前创建的QMap以便在插槽内使用。 I've tried following this but it still did not work (I think I am just doing something stupid). 我尝试遵循此操作,但仍然无法正常工作(我认为我只是在做一些愚蠢的事情)。 Here is the code I am using. 这是我正在使用的代码。

Constructor: 构造函数:

QMap <int, QList<int> > tiles;
connect(ui->lineEdit, SIGNAL(editingFinished()), this, SLOT(someSlot()));

Now, whenever I send the signal editingFinished, I want someSlot to check if the value is in the QMap and proceed with the various conditions if it is. 现在,每当我发送信号editingFinished时,我都希望someSlot检查该值是否在QMap中,如果是,则继续各种条件。 Problem is, how do I pass my QMap to a slot? 问题是,如何将QMap传递到插槽? Qt does not seem to allow slots with parameters. Qt似乎不允许带参数的插槽。

A slot is just a function called by Qt. 插槽只是Qt调用的函数。 There is magic in how it's called but it's just a function. 它的调用方式很神奇,但这只是一个函数。 Arguments are passed into the signal (just like a regular function) and Qt eventually passes that argument (or more likely a copy of that argument) to the recipient slot. 参数被传递到信号中(就像常规函数一样),Qt最终将该参数(或更可能是该参数的副本)传递给接收器插槽。 There is logic in Qt where you don't have to pass as many arguments to the slot as you did the signal when defining the connection but that's not for this discussion. 在Qt中有一种逻辑,您不必在定义连接时像传递信号那样向插槽传递尽可能多的参数,但这不用于本讨论。

You cannot pass arguments by name in a connection like this. 您无法在这样的连接中按名称传递参数。

You can either have your tiles object be a member variable of your class that implements someSlot() or you will have to pass tiles to a signal that is connected to someSlot(const QMap >&) 您可以让tile对象成为实现someSlot()的类的成员变量,或者必须将tile传递给连接到someSlot(const QMap>&)的信号

My recommendation is to make tiles a member variable, not a stack variable 我的建议是使tile为成员变量,而不是堆栈变量

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

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