【发布时间】:2020-01-16 21:42:00
【问题描述】:
我想使用自定义对象的QSet。从文档中,我发现:
QSet 的值数据类型必须是可赋值的数据类型。例如,您不能将 QWidget 存储为值;相反,存储一个 QWidget *.此外,该类型必须提供 operator==(),并且还必须有一个全局 qHash() 函数,该函数为键的类型的参数返回一个哈希值。有关 qHash() 支持的类型列表,请参阅 QHash 文档。
以下代码代表我想使用的struct:
typedef struct ShortcutItem
{
QString shortcutName; // A shortcut name
QString explaination; // A shortcut explaination
bool editable; // Is editable
KeySequence sequence; // A list of key values defining a shortcut
ShortcutItem(void) {}
ShortcutItem(QString& name, QString& description, bool enabled, KeySequence seq) : shortcutName(name), explaination(description), editable(enabled), sequence(seq) {}
ShortcutItem(const ShortcutItem& other) : shortcutName(other.shortcutName), explaination(other.explaination), editable(other.editable), sequence(other.sequence) {}
bool ShortcutItem::operator==(const ShortcutItem& other) const { return shortcutName == other.shortcutName; }
} ShortcutItem;
到目前为止,我已经重载了 == 运算符,但无法确定如何处理 qHash() 函数。
请帮忙。
附:我看到了这个post,不知道该怎么办。
【问题讨论】:
-
以正常方式重载
qHash(即uint qHash(const ShortcutItem&, uint seed = 0);)。 -
只添加这一行:
uint qHash(const ShortcutItem&, uint seed = 0);? -
您显然需要实现该功能以及声明!
-
@TobySpeight 这是我的问题 :) 我不知道该怎么做。