将QTreeWidget(或任何其他QWidget)传递给Qt中的Slot

时间:2015-11-05 10:41:48

标签: c++ qt

我可以将QTreeWidget的子类传递给任何QWidget到Qt插槽吗? 我需要在父级的成员函数中从QTreeWidget获取文本。只使用connect(signalMapper, SIGNAL(mapped(QString)), parent, SLOT(changePicture(QString)));作为额外参数传递QString工作正常,但现在我想将子类QTreeWidget ifxTreeWidget *tree = new ifxTreeWidget();传递给changePicture。我将changePicture的签名更改为ifxTreeWidget作为参数,映射如下:

QSignalMapper * signalMapper = new QSignalMapper(parent);

signalMapper->setMapping(tree, tree)

并尝试将其连接起来:

connect(signalMapper, SIGNAL(mapped(QWidget)), parent, SLOT(changePicture(ifxTreeWidget)));

connect( tree, SIGNAL(clicked(QModelIndex)), signalMapper, SLOT(map()) );

但这让我失望:

QObject::connect: No such signal QSignalMapper::mapped(QWidget) in...

我需要申报一个信号吗?如何以及在何处(如果有)?

1 个答案:

答案 0 :(得分:1)

你的连接声明错了。试试这个

connect(signalMapper, SIGNAL(mapped(QWidget*)), parent, SLOT(changePicture(QWidget*)));

然后在parent

void ParentClass::changePicture(QWidget* widget)
{
  ifxTreeWidget* tree = qobject_cast<ifxTreeWidget*>(widget);
  if (tree) {
    // do something with the tree now
  }
}