gpt4 book ai didi

java - Qt:如何突出显示 QListWidget 中的重复项目? (qtjambi)

转载 作者:行者123 更新时间:2023-11-29 06:09:35 24 4
gpt4 key购买 nike

我需要实现一种突出显示重复值的机制。根据值类型(字符串 - 行编辑、长和大小数 - 旋转框)通过委托(delegate)编辑值。目前,我在附加类的帮助下实现了此功能,该类将所有值及其计数存储在两个“并行”列表中。在添加一个新值后,我增加了它的计数(或在删除重复值时减少),但这个解决方案似乎过于庞大。你们对在 QItemDelegate 的 setModelData(...) 方法中突出显示还有其他想法吗?

/**
* Stores a delegates' existing values
*/
private final class DelegateValuesStorage {
private final List<Object> values = new ArrayList<Object>();
private final List<Integer> counts = new ArrayList<Integer>();

....

//Add value or increase a count if exists
public void add(final Object value) {
if(values.contains(value)) {
final int valueIndex = values.indexOf(value);
final int oldCount = counts.get(valueIndex);
counts.remove(valueIndex);
counts.add(valueIndex, oldCount + 1);
} else {
values.add(value);
counts.add(1);
}
}

....

//Decrease a count or remove value if it doesn't exist anymore
public void decreaseCount(final Object value) {
if(value == null) {
return;
}
final int index = values.indexOf(value);
if(index >= 0) {
final int oldCount = counts.get(index);
if(oldCount >= 2) {
counts.remove(index);
counts.add(index, oldCount - 1);
} else {
values.remove(index);
counts.remove(index);
}
}
}

/**
* Delegate
*/
private class ConcreteDelegate extends QItemDelegate {

private final DelegateValuesStorage values = new DelegateValuesStorage();

...

@Override
public void setModelData(final QWidget editor, final QAbstractItemModel model, final QModelIndex index) {
if(editor instanceof ValEditor) { // ValEditor is an abstraction of line edit and spin box over values' data types

final Object value = ((ValEditor) editor).getValue();
model.setData(index, value, Qt.ItemDataRole.UserRole);
final String newData = (value == null) ? "" : String.valueOf(value);
values.add(newData);
final String oldData = (String) model.data(index, Qt.ItemDataRole.DisplayRole);
values.decreaseCount(oldData);


model.setData(index, newData, Qt.ItemDataRole.DisplayRole);
model.setData(index, new QColor(0, 0, 0), Qt.ItemDataRole.ForegroundRole);
redrawItems(model); // runs through values and colors them red if count >= 2; or black if count == 1

} else {
super.setModelData(editor, model, index);
}
}
}

最佳答案

我通常使用 map 来完成这些类型的任务:

private final class DelegateValuesStorage {
private final Map<Object, Integer> values = new HashMap<Object, Integer>();

....

//Add value or increase a count if exists
public void add(final Object value) {
Integer count = values.get(value);
if (count == null) {
values.put(value, 1);
} else {
values.put(value, count + 1);
}
}

....

//Decrease a count or remove value if it doesn't exist anymore
public void decreaseCount(final Object value) {
if(value == null) {
return;
}
Integer count = values.get(value);
if (count == null) {
// decreasing a "new" value - could be an error too
return;
}
if (count <= 1) {
// remove the value from the map
values.remove(value);
} else {
values.put(value, count - 1);
}
}
}

现在应该启用突出显示

values.get(value) > 1

为真

关于java - Qt:如何突出显示 QListWidget 中的重复项目? (qtjambi),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7383979/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com