【问题标题】:QTreeWidgetItem find child by textQTreeWidgetItem 按文本查找子项
【发布时间】:2015-04-17 08:45:15
【问题描述】:

如何通过文本在 QTreeWidgetItem 中查找项目?有没有类似 QTreeWidget 的 findItem 方法?

【问题讨论】:

  • 您的意思是在 QTreeWidgetItem 的子项列表中查找项目吗?
  • @Ashot,是的,我想逐条查找
  • QTreeWidgetItem 中好像没有模拟方法。无论如何,您可以遍历 item 的子项并找出您想要的文本。
  • @Ashot,我决定这样做。谢谢回答

标签: c++ qt qtreewidgetitem


【解决方案1】:

我相信您正在寻找的是 QTreeWidget 中的递归搜索。为此,您必须使用 Qt::MatchContains | Qt::MatchRecursive 的组合作为标志。

因此,如果 pMyTreeWidget 是指向您的 QTreeWidget 的指针,而 myText 是包含您要搜索的文本的 QString,假设搜索必须在第 0 列,则代码将类似于:

QList<QTreeWidgetItem*> clist = pMyTreeWidget->findItems(myText, Qt::MatchContains|Qt::MatchRecursive, 0);
foreach(QTreeWidgetItem* item, clist)
{
    qDebug() << item->text(0);
}

如果您的要求是匹配确切的文本,那么您可以使用Qt::MatchExactly|Qt::MatchRecursive 而不是Qt::MatchContains|Qt::MatchRecursive

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 2023-04-05
    • 2016-04-08
    • 1970-01-01
    • 2016-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多