QColor进入列表供以后检索

时间:2017-12-13 11:11:47

标签: qt qcolor

我想将所有选定的颜色添加到一个没有重复条目的列表中,之后所有选定的颜色都会在窗口小部件中弹出。

如何将QColor添加到列表中,然后将其解压缩

1 个答案:

答案 0 :(得分:2)

默认情况下,

QSet不会为std::unordered_set#include <QColor> #include <QDebug> #include <QSet> #include <unordered_set> // hash function for QColor for use in QSet / QHash QT_BEGIN_NAMESPACE uint qHash(const QColor &c) { return qHash(c.rgba()); } QT_END_NAMESPACE // hash function for QColor for std::unordered_set namespace std { template<> struct hash<QColor> { using argument_type = QColor; using result_type = size_t; result_type operator()(const argument_type &c) const { return hash<QRgb>()(c.rgba()); } }; } // namespace std int main(int argc, char *argv[]) { QSet<QColor> qs; qs.insert(QColor(100, 100, 100)); qs.insert(QColor(100, 100, 100)); qs.insert(QColor(50, 100, 100)); qDebug() << qs.size() << qs.toList(); std::unordered_set<QColor> ss; ss.insert(QColor(100, 100, 100)); ss.insert(QColor(100, 100, 100)); ss.insert(QColor(50, 100, 100)); qDebug() << ss.size(); for (auto c : ss) qDebug() << c; return 0; } 提供哈希函数。您可以在本地(或全局为您的程序)添加,但是将其委托给QRgb的哈希函数(包括alpha值):

QColor

或者您也不能将QRgb放入集合中,而是放置QColor::rgba()值(通过QColor),然后再将其转换回QColor(QRgb) <{1}}构造函数。

相关问题