![](/img/trans.png)
[英]How can I make a QMap object emit a signal when `QMap::insert(…)` is called?
[英]How I can define a QMap with struct as key
我嘗試定義一個 QMap,它的關鍵是 C++ 結構。
struct ProcInfo_S
{
quint8 tech = 0;
quint8 direction = 0;
quint8 category = 0;
};
QMap<ProcInfo_S, uint64_t> G;
G[{2,3,4}] = 2;
但是當我編譯這段代碼時,我得到以下編譯錯誤:
error: no match for ‘operator<’ (operand types are ‘const MainWindow::MainWindow(QWidget*)::ProcInfo_S’
map 中元素的順序是通過調用鍵的operator<
來確定的。 從文檔:
[...] QMap 的鍵類型必須提供 operator<() 指定總順序。 由於 Qt 5.8.1 使用指針類型作為鍵也是安全的,即使底層 operator<() 不提供總順序。
實現它的一種方法是例如:
struct ProcInfo_S
{
quint8 tech = 0;
quint8 direction = 0;
quint8 category = 0;
bool operator<( const ProcInfo_S& other) const {
return std::tie(tech,direction,category) < std::tie(other.tech,other.direction,other.category);
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.