{"rsdb":{"rid":"397515","subhead":"","postdate":"0","aid":"273604","fid":"49","uid":"1","topic":"1","content":"
\n

\u5217\u8868\u6a21\u578b\uff08Item Model\uff09\uff0c\u8001\u5468\u6ca1\u6709\u7ffb\u8bd1\u4e3a\u201c\u9879\u76ee\u6a21\u578b\u201d\uff0c\u56e0\u4e3a Project \u548c Item \u90fd\u53ef\u4ee5\u7ffb\u8bd1\u4e3a\u201c\u9879\u76ee\u201d\uff0c\u5bb9\u6613\u51fa\u73b0\u6b67\u4e49\u3002\u5e72\u8106\u53eb\u5217\u8868\u6a21\u578b\u3002\u8fd9\u4e2a\u6a21\u578b\u4e5f\u786e\u5b9e\u662f\u4e3a\u6570\u636e\u5217\u8868\u51c6\u5907\u7684\uff0c\u5b83\u4ee5 MVC \u7684\u6982\u5ff5\u4e3a\u57fa\u7840\uff0c\u5728\u539f\u59cb\u6570\u636e\u548c\u7528\u6237\u754c\u9762\u89c6\u56fe\u4e4b\u95f4\u642d\u5efa\u6865\u6881\uff0c\u4f7f\u4e24\u8005\u53ef\u4ee5\u4f20\u9012\u6570\u636e\uff08\u63d0\u53d6\u3001\u4fee\u6539\uff09\u3002<\/p> \n

Qt \u91cc\u9762\u4f7f\u7528\u5217\u8868\u63a7\u5236\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u5148\u521b\u5efa\u6a21\u578b\uff08Model\uff09\u3002\u5f53\u7136\uff0c\u4e5f\u6709\u50cf QListWidget \u7c7b\u8fd9\u6837\u5df2\u7ecf\u5c01\u88c5\u597d\uff0c\u5f00\u7bb1\u5373\u98df\u7684\uff0c\u8fd9\u4e2a\u540e\u9762\u518d\u626f\uff0c\u73b0\u5728\u54b1\u4eec\u7684\u91cd\u70b9\u662f\u5f04\u6e05\u695a Item Model \u662f\u5565\u73a9\u610f\u513f\u3002<\/p> \n

\u8fd9\u91cc\u6240\u8bf4\u7684 Item Model \u5e76\u4e0d\u662f\u771f\u6b63\u7684\u6570\u636e\uff0c\u5e94\u8be5\u8bf4\u7b97\u662f\u4e2a\u63a7\u5236\u5668\u3002\u5f53\u7528\u6237\u754c\u9762\u8981\u663e\u793a\u6570\u636e\u65f6\uff0c\u6a21\u578b\u8d1f\u8d23\u4ece\u539f\u59cb\u6570\u636e\u90a3\u91cc\u63d0\u53d6\u503c\uff0c\u518d\u628a\u503c\u4f20\u5230\u754c\u9762\u4e0a\u5448\u73b0\uff1b\u5982\u679c\u7528\u6237\u754c\u9762\u8981\u4fee\u6539\u6570\u636e\uff0c\u901a\u8fc7\u8f93\u5165\u6846\uff08QLineEdit\u7b49\uff09\u8f93\u5165\/\u4fee\u6539\u5185\u5bb9\uff0c\u7136\u540e\u4f20\u7ed9\u6a21\u578b\uff0c\u6a21\u578b\u8d1f\u8d23\u4fee\u6539\u539f\u59cb\u6570\u636e\u3002\u8fd9\u4e48\u770b\u6765\uff0c\u89c6\u56fe\u548c\u539f\u59cb\u6570\u636e\u4e0d\u662f\u76f4\u63a5\u901a\u4fe1\u7684\uff0c\u6a21\u578b\u5c31\u6210\u4e86\u201c\u4e2d\u95f4\u5546\u201d\u3002\u8fd9\u4e2a\u201c\u4e2d\u95f4\u5546\u201d\u53ef\u4ee5\u4e0d\u8d5a\u5dee\u4ef7\uff08\u6309\u539f\u59cb\u6570\u636e\u7684\u6837\u5b50\u5448\u73b0\uff09\uff0c\u4e5f\u53ef\u80fd\u8d5a\u5dee\u4ef7\uff08\u628a\u539f\u59cb\u6570\u636e\u52a0\u5de5\u4e00\u4e0b\u518d\u8ba9\u4f60\u770b\uff09\u3002<\/p> \n

\u5217\u8868\u6a21\u578b\u6709\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u53eb QAbstractItemModel\uff1b\u5bf9\u5e94\u5730\uff0c\u89c6\u56fe\u7ec4\u4ef6\u4e5f\u6709\u4e00\u4e2a\u62bd\u8c61\u57fa\u7c7b\uff0c\u53eb QAbstractItemView\u3002\u53e6\u5916\uff0c\u5728\u6a21\u578b\u548c\u89c6\u56fe\u4e4b\u95f4\u8fd8\u6709\u4e00\u4e2a\u201c\u4ee3\u7406\u4eba\u201d\uff0c\u62bd\u8c61\u57fa\u7c7b\u53eb QAbstractItemDelegate\uff0c\u5b83\u5e72\u5417\u7684\u5462\uff1f\u8fd9\u662f\u4e13\u4e1a\u7ecf\u7eaa\u4eba\uff0c\u8d1f\u8d23\u95e8\u9762\u5de5\u4f5c\u3002\u6bd4\u5982\uff0c\u5728\u89c6\u56fe\u7ec4\u4ef6\u91cc\u5448\u73b0\u6570\u636e\u65f6\u7528\u4ec0\u4e48\u5b57\u4f53\uff0c\u4ec0\u4e48\u989c\u8272\u6765\u7ed8\u5236\u6587\u672c\uff0c\u7528\u4ec0\u4e48\u65b9\u5f0f\u4ece\u6a21\u578b\u63d0\u53d6\u6570\u636e\u7b49\uff1b\u5728\u7f16\u8f91\u6570\u636e\u65f6\uff0c\u6709\u4ec0\u4e48\u63a7\u4ef6\u6765\u8f93\u5165\u6587\u672c\u3002\u4ee5\u53ca\u5728\u7f16\u8f91\u7ed3\u675f\u540e\uff0c\u8f93\u5165\u7684\u5185\u5bb9\u600e\u4e48\u4f20\u7ed9\u6a21\u578b\u7b49\u3002\u65e5\u5e38\u4f7f\u7528\u65f6\u54b1\u4eec\u7528\u5230 QAbstractItemDelegate \u4e0d\u591a\uff0c\u9664\u975e\u4f60\u81ea\u5df1\u60f3\u4e3a\u6570\u636e\u9879\u7ed8\u5236 UI\uff0c\u6216\u7528\u81ea\u5b9a\u4e49\u7684\u7f16\u8f91\u7ec4\u4ef6\u3002\u5982\u679c\u53ea\u662f\u6539\u6539\u5916\u89c2\u4ec0\u4e48\u7684\uff0c\u8fd8\u4e0d\u5982\u7528 QSS \u65b9\u4fbf\u3002<\/p> \n

\u884c\u4e86\uff0c\u4e0d\u626f\u592a\u8fdc\u4e86\uff0c\u54b1\u4eec\u53ea\u8981\u77e5\u9053\u8fd9\u51e0\u4e2a\u57fa\u7c7b\u4e4b\u95f4\u7684\u5173\u7cfb\u5c31\u884c\u4e86\u3002\u54b1\u4eec\u7684\u91cd\u70b9\u8fd8\u662f\u653e\u5728 QAbstractItemModel \u7c7b\u4e0a\u9762\u3002<\/p> \n

QAbstractItemModel \u6709\u51e0\u4e2a\u7eaf\u865a\u51fd\u6570\u662f\u5fc5\u987b\u5728\u6d3e\u751f\u7c7b\u4e2d\u91cd\u5199\u7684\uff1a<\/p> \n

1\u3001index \u65b9\u6cd5<\/span><\/strong><\/span>\uff0c\u58f0\u660e\u5982\u4e0b\uff1a<\/p> \n

\n
virtual<\/span> QModelIndex index<\/span>(\n             <\/span>int<\/span> row, \n             <\/span>int<\/span> column,\n             <\/span>const<\/span> QModelIndex &parent = QModelIndex()) const<\/span> = 0<\/span>;<\/pre> \n <\/div> \n 

\u5217\u8868\u6a21\u578b\u4e2d\u7684\u7d22\u5f15\uff0c\u4e13\u95e8\u7528\u4e00\u4e2a\u53eb QModelIndex \u7684\u7c7b\u8868\u793a\u3002index \u65b9\u6cd5\u662f\u6839\u636e\u4f20\u5165\u7684\u53c2\u6570\uff0c\u8fd4\u56de QModelIndex \u5bf9\u8c61\u3002\u4e4b\u6240\u4ee5\u8981\u7528 QModelIndex \u7c7b\u6765\u8868\u793a\u5217\u8868\u9879\u7684\u7d22\u5f15\uff0c\u662f\u56e0\u4e3a\u5b83\u662f\u7531\u51e0\u4e2a\u503c\u7ec4\u6210\u7684\uff1a<\/p> \n

\n
a\u3001\u884c\u53f7\uff1b\nb\u3001\u5217\u53f7\uff1b\nc\u3001\u7236\u7d22\u5f15\u3002<\/span><\/pre> \n <\/div> \n 

Qt \u4e2d\u7684\u5217\u8868\u6a21\u578b\u7528\u7684\u662f\u4e8c\u7ef4\u8868\u7ed3\u6784\uff0c\u5373\u7531\u884c\u548c\u5217\u7ec4\u6210\uff0c\u5c31\u50cf\u8fd9\u6837\uff1a<\/p> \n

\"\"<\/p> \n

\u95ee\uff1aD\u5728\u54ea\u91cc\uff1f<\/p> \n

\u7b54\uff1arow = 1\uff0ccolumn = 0\u3002<\/p> \n

\u6bcf\u4e2a\u9879\u53c8\u53ef\u4ee5\u5305\u542b\u7236\u7ea7\u8282\u70b9\u548c\u5b50\u7ea7\u8282\u70b9\uff0c\u4f46\u4e0a\u9762\u7684\u4e8c\u7ef4\u8868\u53ea\u6709\u4e00\u5c42\uff0c\u6ca1\u6709\u7236\u7ea7\uff0c\u6240\u4ee5\u5b83\u7684 parent = QModelIndex()\u3002\u7528\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521b\u5efa\u7684 QModelIndex \u8868\u793a\u65e0\u6548\u7d22\u5f15\uff0c\u5373\u884c\u53f7\u662f -1\uff0c\u5217\u53f7\u662f -1\uff0c\u65e0\u7236\u65e0\u5b50\u3002<\/p> \n

\u7efc\u4e0a\u6240\u8a00\uff0cD \u7684\u7d22\u5f15\u5c31\u662f\uff1arow = 1\uff0ccol = 0\uff0cparent = QModelIndex()\u3002<\/p> \n

\u8fd9\u4e2a\u6a21\u578b\u771f\u6b63\u53ef\u6015\u7684\u5730\u65b9\u5728\u4e8e\uff0c\u6bcf\u4e2a\u7d22\u5f15\u90fd\u6709\u7236\u3001\u5b50\u7ea7\u3002\u4e8e\u662f\u4f60\u53ef\u4ee5\u6784\u60f3\u4e0b\u9762\u8fd9\u4e48\u6050\u6016\u7684\u5217\u8868\uff1a<\/p> \n

\"\"<\/p> \n

Root\u662f\u4e00\u4e2a\u65e0\u6548\u7684\u7d22\u5f15\uff0c\u53ef\u4ee5\u8ba4\u4e3a\u662f\u9876\u5c42\u7684\u201d\u7236\u7ea7\u201c\u3002A\u3001B\u3001C\u3001D\u3001E\u3001F \u7684\u7236\u7ea7\u90fd\u662f Root\uff0c\u884c\u5217\u53f7\u75310\u5f00\u59cb\u7f16\u6392\uff0cA\u5728\u7b2c\u4e00\u884c\u7b2c\u4e00\u5217\uff0c\u6240\u4ee5 row=0\uff0ccol=0\uff0cparent=Root\u3002E\u6709\u5b50\u8282\u70b9\uff0c\u5373 M\u3001N\u3001O\u3001P\uff0c\u7136\u540eMNOP\u7684\u884c\u53f7\u548c\u5217\u53f7\u4e5f\u8981\u4ece 0 \u91cd\u65b0\u8ba1\u7b97\uff0c\u5373 N \u7684\u7d22\u5f15\u662f row=0, col=1, parent=E\u3002\u6700\u540e\uff0cQ \u8fd9\u53ae\u53c8\u6709\u5b50\u8282\u70b9\uff0c\u662f\u4e00\u4e2a\u53ea\u6709\u4e00\u884c\u7684\u5217\u8868\uff1aR\u3001S\u3001T\u3002\u4e8e\u662f\uff0cRST\u7684\u884c\u5217\u53f7\u4e5f\u91cd\u65b0\u8ba1\u7b97\u3002\u5373 R \u7684\u7d22\u5f15\u662f row=0, col=0, parent=Q\u3002<\/p> \n

\u4e0d\u8fc7\uff0c\u5b9e\u9645\u4f7f\u7528\u65f6\uff0c\u4e00\u822c\u4e0d\u9700\u8981\u6784\u5efa\u8fd9\u4e48\u795e\u7684\u6570\u636e\u7ed3\u6784\uff0c\u800c\u4e14\u8fd9\u73a9\u610f\u653e\u5230\u7528\u6237\u754c\u9762\u4e0a\u8fd8\u4e0d\u77e5\u9053\u600e\u4e48\u663e\u793a\u597d\u5462\u3002\u6bd5\u7adf\uff0c\u54b1\u4eec\u5728\u754c\u9762\u4e0a\u5e38\u89c1\u7684\u89c6\u56fe\u4e5f\u5c31\u4ee5\u4e0b\u4e09\u79cd\uff1a<\/p> \n

1\uff09\u3001\u591a\u884c\uff0c\u53ea\u6709\u4e00\u5217\uff0c\u8fd9\u5c31\u76f8\u5f53\u4e8e\u50cf\u6570\u7ec4\u8fd9\u6837\u7684\u6570\u636e\u4e86\u3002\u7528 QListView \u7ec4\u4ef6\u6765\u5448\u73b0\uff1b<\/p> \n

2\uff09\u3001\u4e00\u7ea7\u4e8c\u7ef4\u8868\uff0c\u7531\u884c\u3001\u5217\u7ec4\u6210\uff0c\u7531 QTableView \u7ec4\u4ef6\u5448\u73b0\uff1b<\/p> \n

3\uff09\u3001\u591a\u7ea7\u8282\u70b9\uff0c\u5178\u578b\u7684\u5c31\u662f QTreeView \u7ec4\u4ef6\u4e86\u3002Qt \u7684 TreeView \u6bd4 .NET \u7684\u63a7\u4ef6\u591a\u4e86\u4e00\u4e2a\u7279\u70b9\u2014\u2014\u53ef\u4ee5\u5728\u663e\u793a\u591a\u7ea7\u8282\u70b9\u7684\u540c\u65f6\u663e\u793a\u8868\u683c\u3002\u4f46\u8981\u6ce8\u610f\u7684\u662f\uff0c\u53ea\u6709\u9996\u5217\u624d\u652f\u6301\u7236\u5b50\u8282\u70b9\u3002\u6240\u4ee5\uff0c\u5bf9\u4e8e QTreeView \u89c6\u56fe\uff0c\u6784\u5efa\u8fd9\u6837\u7684\u6570\u636e\u4e5f\u8db3\u591f\u4e86\uff1a<\/p> \n

\"\"<\/p> \n

2\u3001parent \u65b9\u6cd5<\/span><\/strong>\u3002\u5b83\u7684\u58f0\u660e\u5982\u4e0b\uff1a<\/p> \n

\n
virtual<\/span> QModelIndex parent<\/span>(const<\/span> QModelIndex &child) const<\/span> = 0<\/span>;<\/pre> \n <\/div> \n 

\u8fd4\u56de child \u8282\u70b9\u7684\u7236\u7ea7\u8282\u70b9\uff0c\u5bf9\u4e8e\u53ea\u6709\u4e00\u5c42\u7684\u5217\u8868\uff0c\u8fd4\u56de QModelIndex() \u5373\u53ef\u3002<\/p> \n

3\u3001rowCount \u65b9\u6cd5<\/span><\/strong>\u3002\u58f0\u660e\u5982\u4e0b\uff1a<\/p> \n

\n
virtual<\/span> int<\/span> rowCount<\/span>(const<\/span> QModelIndex &parent = QModelIndex()) const<\/span> = 0<\/span>;<\/pre> \n <\/div> \n 

\u8fd4\u56de\u539f\u59cb\u6570\u636e\u6709\u603b\u5171\u6709\u591a\u5c11\u884c\u3002<\/p> \n

4\u3001columnCount \u65b9\u6cd5<\/span><\/strong>\u3002\u5b83\u7684\u58f0\u660e\u5982\u4e0b\uff1a<\/p> \n

\n
virtual<\/span> int<\/span> columnCount<\/span>(const<\/span> QModelIndex &parent = QModelIndex()) const<\/span> = 0<\/span>;<\/pre> \n <\/div> \n 

\u8be5\u65b9\u6cd5\u8fd4\u56de\u539f\u59cb\u6570\u636e\u6709\u591a\u5c11\u5217\uff0c\u5982\u679c\u662f\u6570\u7ec4\u4e4b\u7c7b\u7684\uff0c\u8fd4\u56de 1\u3002<\/p> \n

5\u3001data \u65b9\u6cd5<\/span><\/strong>\u3002\u58f0\u660e\u5982\u4e0b\uff1a<\/p> \n

\n
virtual<\/span> QVariant data<\/span>(const<\/span> QModelIndex &index, int<\/span> role = Qt::DisplayRole) const<\/span> = 0<\/span>;<\/pre> \n <\/div> \n 

\u8fd9\u662f\u4e2a\u91cd\u8981\u7684\u6210\u5458\uff0c\u5b83\u8981\u6839\u636e index \u53c2\u6570\u6307\u5b9a\u7684\u7d22\u5f15\uff0c\u8fd4\u56de\u6570\u636e\u9879\u7684\u503c\u3002\u8fd9\u91cc\u8981\u8bf4\u4e00\u4e0b\u53eb\u201d\u6570\u636e\u89d2\u8272\u201c\u7684\u6982\u5ff5\u3002\u8bf4\u901a\u4fd7\u4e0d\u6613\u61c2\u4e00\u70b9\u5c31\u662f\u8fd4\u56de\u7684\u503c\u7684\u7528\u9014<\/strong><\/em><\/span>\u3002\u6bd4\u5982\uff0crole \u53c2\u6570\u7684\u9ed8\u8ba4\u503c\u6307\u5b9a\u4e86 DisplayRole\uff0c\u610f\u601d\u5c31\u662f\u4f60\u8fd4\u56de\u7684\u503c\u662f\u8981\u663e\u793a\u5728\u7528\u6237\u754c\u9762\u4e0a\u7684\uff0c\u5c31\u662f\u4f60\u60f3\u8ba9\u7528\u6237\u770b\u5230\u7684\u6587\u672c\u3002Qt::ItemDataRole \u679a\u4e3e\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u6570\u636e\u89d2\u8272\u3002<\/p> \n

\n
enum<\/span> ItemDataRole {\n    DisplayRole <\/span>= 0<\/span>,               \/\/<\/span> \u663e\u793a\u5728\u754c\u9762\u7684\u5185\u5bb9<\/span>\n    DecorationRole = 1<\/span>,          \/\/<\/span> \u548c\u6587\u672c\u4e00\u8d77\u663e\u793a\u7684\u56fe\u6807\uff0c\u7c7b\u578b\u4e00\u822c\u662fQIcon<\/span>\n    EditRole = 2<\/span>,                    \/\/<\/span> \u5f53\u7f16\u8f91\u6570\u636e\u65f6\uff0c\u8fd4\u56de\u7ed9\u7528\u6237\u770b\u7684\u503c<\/span>\n    ToolTipRole = 3<\/span>,                \/\/<\/span> \u663e\u793a\u5728\u5de5\u5177\u63d0\u793a\u4e2d\u7684\u6587\u672c<\/span>\n    StatusTipRole = 4<\/span>,            \/\/<\/span> \u663e\u793a\u5728\u72b6\u6001\u680f\u4e2d\u7684\u6587\u672c<\/span>\n    WhatsThisRole = 5<\/span>,           \/\/<\/span> \u5e2e\u52a9\u4fe1\u606f\uff0c\u663e\u793a\u5728\u201d\u8fd9\u662f\u5565\uff1f\u201c\u63d0\u793a\u4e2d\n    <\/span>\/\/<\/span> Metadata<\/span>\n    FontRole = 6<\/span>,                    \/\/<\/span> \u5448\u73b0\u6570\u636e\u65f6\u7528\u5565\u5b57\u4f53<\/span>\n    TextAlignmentRole = 7<\/span>,      \/\/<\/span> \u6587\u672c\u7684\u5bf9\u9f50\u65b9\u5f0f<\/span>\n    BackgroundRole = 8<\/span>,          \/\/<\/span> \u8fd4\u56de\u753b\u5237\u5bf9\u8c61\uff0c\u7528\u6765\u7ed8\u5236\u5217\u8868\u9879\u7684\u80cc\u666f<\/span>\n    ForegroundRole = 9<\/span>,           \/\/<\/span> \u6587\u672c\u7684\u989c\u8272<\/span>\n    CheckStateRole = 10<\/span>,         \/\/<\/span> \u5982\u679c\u754c\u9762\u4e0a\u663e\u793a\u4e86 checkbox\uff0c\u90a3\u4e48\u8fd4\u56decheckbox\u7684\u72b6\u6001\uff08\u9009\u4e2d\uff1f\u672a\u9009\u4e2d\uff1f\u672a\u77e5\uff1f\uff09\n    <\/span>\/\/<\/span> Accessibility<\/span>\n    AccessibleTextRole = 11<\/span>,     \/\/<\/span> \u7b80\u7ec3\u7684\u8f85\u52a9\u4fe1\u606f\u3002\u7528\u4e8e\u50cf\u201d\u8bb2\u8ff0\u4eba\u201c\u8fd9\u4e9b\u8f85\u52a9\u5de5\u5177<\/span>\n    AccessibleDescriptionRole = 12<\/span>,    \/\/<\/span>\u8be6\u7ec6\u8f85\u52a9\u4fe1\u606f\uff0c\u7528\u4e8e\u50cf\u201d\u8bb2\u8ff0\u4eba\u201c\u7c7b\u4f3c\u7684\u8f85\u52a9\u5de5\u5177\n    <\/span>\/\/<\/span> More general pur","orderid":"0","title":"\u3010Qt6\u3011\u5217\u8868\u6a21\u578b\u2014\u2014\u62bd\u8c61\u57fa\u7c7b(\u4e00)","smalltitle":"","mid":"0","fname":"c++\u7f16\u7a0b\u57fa\u7840","special_id":"0","bak_id":"0","info":"0","hits":"644","pages":"4","comments":"0","posttime":"2023-09-23 15:44:27","list":"1695455067","username":"admin","author":"","copyfrom":"","copyfromurl":"","titlecolor":"","fonttype":"0","titleicon":"0","picurl":"https:\/\/www.cppentry.com\/upload_files\/","ispic":"0","yz":"1","yzer":"","yztime":"0","levels":"0","levelstime":"0","keywords":"Qt6<\/A>","jumpurl":"","iframeurl":"","style":"","template":"a:3:{s:4:\"head\";s:0:\"\";s:4:\"foot\";s:0:\"\";s:8:\"bencandy\";s:0:\"\";}","target":"0","ip":"112.94.1.100","lastfid":"0","money":"0","buyuser":"","passwd":"","allowdown":"","allowview":"","editer":"","edittime":"0","begintime":"0","endtime":"0","description":"\u3010Qt6\u3011\u5217\u8868\u6a21\u578b\u2014\u2014\u62bd\u8c61\u57fa\u7c7b","lastview":"1715493331","digg_num":"585","digg_time":"1715789665","forbidcomment":"0","ifvote":"0","heart":"","htmlname":"","city_id":"0"},"page":"1"}