简体   繁体   English

将QTreeWidget(或任何其他QWidget)传递到Qt中的插槽

[英]Passing a QTreeWidget (or any other QWidget) to a Slot in Qt

Can i pass in any QWidget, in my Case a Subclass of a QTreeWidget to a Qt Slot? 我可以将QTreeWidget的子类传递给Qt插槽吗? I need to get text from the QTreeWidget in a member function of parent. 我需要从QTreeWidget的父成员函数中获取文本。 Just passing in a QString as extra argument with connect(signalMapper, SIGNAL(mapped(QString)), parent, SLOT(changePicture(QString))); 只是将一个带有附加参数的QString传递给connect(signalMapper, SIGNAL(mapped(QString)), parent, SLOT(changePicture(QString))); works fine, but now i want to pass in a subclassed QTreeWidget ifxTreeWidget *tree = new ifxTreeWidget(); 工作正常,但现在我想传入一个子类QTreeWidget ifxTreeWidget *tree = new ifxTreeWidget(); to changePicture . 更改changePicture I changed the Signature of changePicture to take an ifxTreeWidget as argument and the mapping like: 我将changePicture的Signature更改为以ifxTreeWidget作为参数和如下映射:

QSignalMapper * signalMapper = new QSignalMapper(parent);

signalMapper->setMapping(tree, tree)

and tried to connect it like: 并尝试像这样连接它:

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

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

but this leaves me with: 但这给我留下了:

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

Do i need to declare a Signal? 我需要声明信号吗? How and where (if so)? 如何以及在哪里(如果有)?

your connect statement is wrong. 您的connect语句错误。 Try this 尝试这个

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

and then in the parent class 然后在parent类中

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

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

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