我想使用定制对象的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,我不知道该怎么做。
发布于 2020-01-16 12:50:28
据我从您的代码中可以看到,您的散列函数应该如下所示
uint qHash(const ShortcutItem & item)
{
return qHash(item.shortcutName);
}换句话说,您可以使用可用的重载uint qHash(const QString &key, uint seed = ...),将项成员shortcutName传递给它,然后只返回它的返回值。
您可以将函数原型放在ShortcutItem结构之后,放在它的标题中:
uint qHash(const ShortcutItem & item);以及它的实现文件(.cpp)中的定义(上面)。
https://stackoverflow.com/questions/59769008
复制相似问题