简体   繁体   English

Qt QAction动态阵列连接

[英]Qt QAction Dynamic Array Connect

I want to create a QMenu that contains QAction objects that are checkable. 我想创建一个包含可检查QAction对象的QMenu。 Once an action is checked, it will fire and enable drawing of some 3D object. 检查一个动作后,它将触发并启用一些3D对象的绘制。 However, the number of 3D objects depends on what files to be loaded. 但是,3D对象的数量取决于要加载的文件。 Thus, this QMenu have dynamic number of QAction objects. 因此,这个QMenu具有动态数量的QAction对象。 Suppose we have 10 3D objects with names "1", "2", ... "10" and thus the QAction objects in the QMenu would be displayed as "1", "2", ... "10". 假设我们有10个名为“1”,“2”,......“10”的3D对象,因此QMenu中的QAction对象将显示为“1”,“2”,......“10”。 When one of these are checked, 3D object of that name will be enabled to display. 选中其中一个时,将启用该名称的3D对象。

Code to generate dynamic QAction objects: 用于生成动态QAction对象的代码:

QStringList labels = defaultScene->getLabels();
for(int i=0; i<labels.size(); i++){
     QAction* labelAction = new QAction(labels[i], this);
     labelAction->setToolTip("Trace Marker " + labels[i]);
     labelAction->setStatusTip("Trace Marker " + labels[i]);
     labelAction->setCheckable(true);
     traceMenu->addAction(labelAction);
}

My question is, how do I connect these QAction objects? 我的问题是,如何连接这些QAction对象? Specifically, I have an array of bool in defaultScene that will be toggled as the QAction are toggled. 具体来说,我在defaultScene中有一个bool数组,当QAction切换时,它将被切换。 How am I to know which QAction is firing? 我怎么知道哪个QAction正在开火? The SIGNAL of QAction on toggle only passes through a bool. 切换时QAction的SIGNAL只通过bool。 Ideally, I would have a single function in defaultScene: 理想情况下,我在defaultScene中只有一个函数:

void toggleObject3D(int index){
     if(index >= 0 && index < visibleSize){
          visible[index] = !visible[index];
     }
}

So to make this work, I would need some sort of SIGNAL from traceMenu that would fire an int variable. 所以为了使这个工作,我需要某种来自traceMenu的SIGNAL来触发一个int变量。 I am unaware of such SIGNAL. 我不知道这样的SIGNAL。

You can use QSignalMapper ( Link in the documentation ) 您可以使用QSignalMapper文档中的链接

The idea is to associate each QAction with an index, and then use the mapped(int) signal from QSignalMapper. 我们的想法是将每个QAction与索引相关联,然后使用来自QSignalMapper的映射(int)信号。 Of course, we need to map the toggled signal. 当然,我们需要映射切换信号。

So first, define your method toggleObject3D as a slot. 首先,将您的方法toggleObject3D定义为一个插槽。

Then, when generating the instances of QAction, create the QSignalMapper and associate each action with it: 然后,在生成QAction实例时,创建QSignalMapper并将每个动作与它关联:

QStringList labels = defaultScene->getLabels();
QSignalMapper *mapper = new QSignalMapper(this);
for(int i=0; i<labels.size(); i++){
   QAction* labelAction = new QAction(labels[i], this);
   labelAction->setToolTip("Trace Marker " + labels[i]);
   labelAction->setStatusTip("Trace Marker " + labels[i]);
   labelAction->setCheckable(true);
   traceMenu->addAction(labelAction);

   // Map this action to index i
   mapper->setMapping(labelAction, i);
   // Associate the toggled signal to map slot from the mapper
   // (it does not matter if we don't use the bool parameter from the signal)
   connect(action, SIGNAL(toggled(bool)), mapper, SLOT(map()));
}

// Connect the QSignalMapper map() signal to your method
connect(mapper, SIGNAL(mapped(int)), this, SLOT(toggleObject3D(int)));

And it should work :) 它应该工作:)

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

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