加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
UiCommon.cpp 2.09 KB
一键复制 编辑 原始数据 按行查看 历史
xiongqiumin 提交于 2024-07-19 18:16 . update
#include <QTreeWidget>
#include <QComboBox>
#include "UiCommon.h"
#include "JZNodeObject.h"
bool UiHelper::treeFilter(QTreeWidgetItem *item, QString name)
{
bool show = false;
int count = item->childCount();
if (count == 0)
{
show = item->text(0).contains(name);
}
else
{
for (int i = 0; i < count; i++)
{
if (treeFilter(item->child(i), name))
show = true;
}
}
item->setHidden(!show);
return show;
}
void UiHelper::clearTreeItem(QTreeWidgetItem *root)
{
while (root->childCount() > 0)
delete root->takeChild(0);
}
void UiHelper::treeUpdate(QTreeWidgetItem *root, const QStringList &names, std::function<QTreeWidgetItem*(int)> func)
{
auto indexOfItem = [](QTreeWidgetItem *node,const QString &name)->int{
for (int i = 0; i < node->childCount(); i++)
{
auto sub = node->child(i);
if (sub->text(0) == name)
return i;
}
return -1;
};
for (int i = root->childCount() - 1; i >= 0; i--)
{
if (!names.contains(root->child(0)->text(0)))
delete root->takeChild(i);
}
for (int i = 0; i < names.count(); i++)
{
QTreeWidgetItem *item = nullptr;
int cur_index = indexOfItem(root, names[i]);
if (cur_index >= 0)
{
item = root->child(cur_index);
if (cur_index != i)
{
root->takeChild(cur_index);
root->insertChild(i, item);
}
}
else
{
item = func(i);
root->addChild(item);
}
}
}
void UiHelper::updateEnumBox(QComboBox *box, int dataType, int value)
{
box->blockSignals(true);
auto meta = JZNodeObjectManager::instance()->enumMeta(dataType);
for (int i = 0; i < meta->count(); i++)
box->addItem(meta->key(i), meta->value(i));
if (value != 0xfafa)
{
int index = box->findData(value);
box->setCurrentIndex(index);
}
box->blockSignals(false);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化