{"id":3014,"date":"2011-05-07T16:50:48","date_gmt":"2011-05-07T08:50:48","guid":{"rendered":"http:\/\/edsionte.com\/techblog\/?p=3014"},"modified":"2011-05-08T20:01:13","modified_gmt":"2011-05-08T12:01:13","slug":"qtablewidget%e5%9f%ba%e6%9c%ac%e5%8a%9f%e8%83%bd%e6%80%bb%e7%bb%93","status":"publish","type":"post","link":"http:\/\/edsionte.com\/techblog\/archives\/3014","title":{"rendered":"QTableWidget\u57fa\u672c\u529f\u80fd\u603b\u7ed3"},"content":{"rendered":"<p>QTableWidget\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u6761\u76ee\uff08item\uff09\u7684\u8868\u683c\u89c6\u56fe\u6a21\u578b\uff0c\u5728\u8be5\u90e8\u4ef6\u4e2d\u6570\u636e\u4ee5item\u4e3a\u57fa\u672c\u5355\u4f4d\uff0c\u6bcf\u6761\u6570\u636e\uff08item\uff09\u5bf9\u5e94\u4e00\u4e2aQTableWidgetItem\u7c7b\u7684\u5bf9\u8c61\uff0c\u6240\u6709\u6570\u636e\u5f62\u6210\u7684item\u7ec4\u6210\u6574\u4e2a\u8868\u683c\u3002\u63a5\u4e0b\u6765\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u7528\u6765\u663e\u793a\u5b66\u751f\u4fe1\u606f\u7684\u8868\u683c\uff0c\u4ee5\u6b64\u4e3a\u4f8b\u8bf4\u660eTableWidget\u7684\u4e00\u4e9b\u5e38\u7528\u529f\u80fd\u3002<\/p>\n<p>1.\u521b\u5efaQTableWidget<\/p>\n<p>\u9996\u5148\u521b\u5efastudentInfo\u7c7b\uff0c\u7136\u540e\u5728Qt Desinger\u6a21\u5f0f\u4e0b\u521b\u5efa\u4e00\u4e2aQTableWidget\u90e8\u4ef6\uff0c\u5bf9\u5176\u547d\u540d\u4e3astuTableWidget\u3002\u901a\u8fc7\u5728\u8be5\u90e8\u4ef6\u4e0a\u70b9\u51fb\u53f3\u952e\u521b\u5efa\u5217\u9879\u8868\u5934\uff0c\u521b\u5efa\u5b8c\u6bd5\u540e\u4e5f\u5c31\u540c\u65f6\u6307\u5b9a\u4e86\u8be5\u8868\u683c\u7684\u5217\u9879\u6570\u3002\u5982\u4e0b\uff1a<\/p>\n<p><a rel=\"attachment wp-att-3025\" href=\"http:\/\/edsionte.com\/techblog\/archives\/3014\/screenshot-8\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-3025\" title=\"Screenshot\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-300x84.png\" alt=\"\" width=\"300\" height=\"84\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-300x84.png 300w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot.png 695w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>2.\u521d\u59cb\u5316<\/p>\n<p>\u9664\u4e86\u76f4\u63a5\u5728\u8bbe\u8ba1\u6a21\u5f0f\u4e0b\u521b\u5efa\u884c\u6570\u548c\u5217\u6570\u5916\uff0c\u6211\u4eec\u8fd8\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u76f8\u5e94\u7684\u65b9\u6cd5\u6765\u8bbe\u5b9a\u3002\u6bd4\u5982\u6211\u4eec\u901a\u8fc7setRowCount\u65b9\u6cd5\u5728studentInfo\u7c7b\u7684\u6784\u9020\u65b9\u6cd5\u4e2d\u5373\u53ef\u6307\u5b9a\u884c\u9879\u6570\u3002<\/p>\n<pre class=\"brush:c\">ui-&gt;stuTableWidget-&gt;setRowCount(30);<\/pre>\n<p>\u8be5\u65b9\u6cd5\u5728\u521b\u5efa\u884c\u7684\u540c\u65f6\u4f1a\u81ea\u52a8\u521b\u5efa\u4e00\u4e2a\u7528\u6765\u663e\u793a\u884c\u53f7\u7684\u5217\u9879\uff0c\u5982\u4e0b\uff1a<\/p>\n<p><a rel=\"attachment wp-att-3026\" href=\"http:\/\/edsionte.com\/techblog\/archives\/3014\/screenshot-studentinfo-1\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-3026\" title=\"Screenshot-studentInfo-1\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-studentInfo-1-300x275.png\" alt=\"\" width=\"300\" height=\"275\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-studentInfo-1-300x275.png 300w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-studentInfo-1.png 759w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>\u5982\u679c\u6211\u4eec\u7684\u8868\u683c\u53ea\u7528\u6765\u663e\u793a\u4fe1\u606f\uff0c\u5e76\u4e0d\u6d89\u53ca\u5bf9\u76f8\u5e94\u4fe1\u606f\u7684\u4fee\u6539\uff0c\u5219\u53ef\u4ee5\u901a\u8fc7\u4e0b\u8ff0\u65b9\u6cd5\u5c06\u8868\u683c\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\u6a21\u5f0f\uff1a<\/p>\n<pre class=\"brush:c\"> ui-&gt;stuTableWidget-&gt;setEditTriggers(QAbstractItemView::NoEditTriggers);<\/pre>\n<p>setEditTriggers()\u662fQAbstractItemView\u7c7b\u4e2d\u7684\u4e00\u4e2a\u65b9\u6cd5\uff0c\u901a\u8fc7\u5411\u8be5\u65b9\u6cd5\u4f20\u9012\u76f8\u5e94\u7684\u53c2\u6570\u4ee5\u8bbe\u7f6eitem\u7684\u4e00\u4e9b\u5c5e\u6027\uff0c\u6bd4\u5982NoEditTriggers\u53c2\u6570\u53ef\u5c06item\u8bbe\u7f6e\u4e3a\u53ea\u8bfb\uff0cDoubleClicked\u4ee3\u8868\u53cc\u51fb\u65f6item\u53ef\u4fee\u6539\u3002\u800cQTableWidget\u7ee7\u627f\u4e86QAbstractItemView\u65b9\u6cd5\uff0c\u56e0\u6b64\u5b83\u53ef\u4ee5\u4f7f\u7528\u8be5\u51fd\u6570\u3002<\/p>\n<p>3.\u4fe1\u606f\u663e\u793a<\/p>\n<p>\u8868\u683c\u89c6\u56fe\u4e2d\u6570\u636e\u7684\u83b7\u53d6\u968f\u7528\u9014\u7684\u4e0d\u540c\u800c\u4e0d\u540c\u3002\u5982\u679c\u4f7f\u7528\u4e8eC\/S\u6a21\u578b\u7684\u5ba2\u6237\u7aef\uff0c\u90a3\u4e48\u8868\u683c\u4e2d\u7684\u4fe1\u606f\u9700\u8981\u4ece\u670d\u52a1\u5668\u7aef\u53d1\u9001\u5230\u672c\u5730\uff0c\u518d\u76f8\u5e94\u89e3\u6790\uff1b\u5982\u679c\u4f7f\u7528\u5728\u6570\u636e\u5e93\u4e2d\uff0c\u5219\u9700\u8981\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\u76f8\u5e94\u4fe1\u606f\u3002\u8fd9\u91cc\u5047\u5b9a\u6570\u636e\u5df2\u7ecf\u5230\u8fbe\u672c\u5730\uff0c\u6211\u4eec\u901a\u8fc7\u4e0b\u9762\u7684\u65b9\u6cd5\u6765\u663e\u793a\u6570\u636e\u4fe1\u606f\u3002<\/p>\n<pre class=\"brush:c\">void studentInfo::showInfo()\r\n{\r\n    QTableWidgetItem *tmpItem;\r\n\r\n    tmpItem = new QTableWidgetItem(QString(\"04065061\"));\r\n    ui-&gt;stuTableWidget-&gt;setItem(0, 0, tmpItem);\r\n\r\n    tmpItem = new QTableWidgetItem(QString(\"edsionte\"));\r\n    ui-&gt;stuTableWidget-&gt;setItem(0, 1, tmpItem);\r\n\r\n    tmpItem = new QTableWidgetItem(QString(\"1988.01.28\"));\r\n    ui-&gt;stuTableWidget-&gt;setItem(0, 2, tmpItem);\r\n\r\n    tmpItem = new QTableWidgetItem(QString(\"male\"));\r\n    ui-&gt;stuTableWidget-&gt;setItem(0, 3, tmpItem);\r\n\r\n    tmpItem = new QTableWidgetItem(QString(\"Xi'an Institute of Posts and Telecommunications\"));\r\n    ui-&gt;stuTableWidget-&gt;setItem(0, 4, tmpItem);\r\n}<\/pre>\n<p>\u4e0a\u8ff0\u7684showInfo\u65b9\u6cd5\u4e3a\u7b2c\u4e00\u884c\u8bbe\u5b9a\u4e86\u76f8\u5e94\u4fe1\u606f\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u8868\u683c\u7684\u4e00\u884c\u4e2d\u6bcf\u4e2a\u5177\u4f53\u7684\u5217\u9879\u90fd\u5bf9\u5e94\u4e00\u4e2aQTableWidgetItem\u5bf9\u8c61\uff0c\u5e76\u901a\u8fc7\u5728setItem\u65b9\u6cd5\u4e2d\u6307\u5b9a\u884c\u53f7\u548c\u5217\u53f7\u5c06\u8be5item\u5bf9\u8c61\u8bbe\u7f6e\u5230\u8868\u683c\u7684\u5177\u4f53\u4f4d\u7f6e\u3002\u5728\u4e0a\u8ff0\u7684showInfo\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5206\u522b\u901a\u8fc7\u8be5\u65b9\u6cd5\u521b\u5efa\u4e86\u7b2c\u4e00\u884c\u7684\u7b2c\u4e00\u5230\u7b2c\u4e94\u5217\u7684\u6570\u636e\uff08\u884c\u5217\u4e0b\u8868\u4ece0\u5f00\u59cb\uff09\u3002<\/p>\n<p><a rel=\"attachment wp-att-3027\" href=\"http:\/\/edsionte.com\/techblog\/archives\/3014\/screenshot-studentinfo\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-3027\" title=\"Screenshot-studentInfo\" src=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-studentInfo-300x275.png\" alt=\"\" width=\"300\" height=\"275\" srcset=\"http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-studentInfo-300x275.png 300w, http:\/\/edsionte.com\/techblog\/wordpress\/wp-content\/uploads\/2011\/05\/Screenshot-studentInfo.png 759w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>4.\u4e3a\u8868\u683c\u6570\u636e\u6dfb\u52a0\u53f3\u952e\u83dc\u5355<\/p>\n<p>\u6709\u65f6\u5019\u6211\u4eec\u60f3\u901a\u8fc7\u70b9\u51fb\u9f20\u6807\u53f3\u952e\u5bf9\u8868\u683c\u6570\u636e\u8fdb\u884c\u4e00\u4e9b\u5176\u4ed6\u64cd\u4f5c\uff0c\u6bd4\u5982\u590d\u5236\u3001\u67e5\u770b\u8be6\u60c5\u7b49\uff0c\u6211\u4eec\u53ef\u4ee5\u6309\u7167\u4e0b\u9762\u7684\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4e3a\u4e86\u5b9e\u73b0\u70b9\u51fb\u53f3\u952e\u5f39\u51fa\u83dc\u5355\u8fd9\u4e2a\u529f\u80fd\uff0c\u6211\u4eec\u5fc5\u987b\u5728\u7c7bstudentInfo\u7c7b\u4e2d\u58f0\u660e\u4e00\u4e2a\u83dc\u5355\u53d8\u91cfpopMenu\u548c\u4e00\u4e2a\u83dc\u5355\u9009\u9879\u53d8\u91cfaction\u3002<\/p>\n<pre class=\"brush:c\">class studentInfo : public QMainWindow\r\n{\r\n\u2026\u2026\u2026\u2026\r\nprivate:\r\n    Ui::studentInfo *ui;\r\n    QMenu *popMenu;\r\n    QAction *action;\r\n\r\nprivate slots:\r\n    void on_stuTableWidget_customContextMenuRequested(QPoint pos);\r\n\u2026\u2026\u2026\u2026\r\n};<\/pre>\n<p>\u58f0\u660e\u5b8c\u6bd5\u540e\uff0c\u6211\u4eec\u5728studentInfo\u7c7b\u7684\u6784\u9020\u51fd\u6570\u4e2d\u5bf9\u5176\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">    ui-&gt;stuTableWidget-&gt;setContextMenuPolicy(Qt::CustomContextMenu);\r\n    popMenu = new QMenu(ui-&gt;stuTableWidget);\r\n    action = new QAction(\"Copy\", this);<\/pre>\n<p>setContextMenuPolicy\u65b9\u6cd5\u7528\u6765\u8bbe\u7f6ewidget\u83dc\u5355\u9879\u7684\u663e\u793a\u65b9\u6cd5\uff0c\u800cCustomContextMenu\u662f\u552f\u4e00\u4e0e\u90ae\u4ef6\u83dc\u5355\u6709\u5173\u7684\u53c2\u6570\uff0c\u56e0\u6b64\u8fd9\u91cc\u6211\u4eec\u5c06\u83dc\u5355\u663e\u793a\u65b9\u6cd5\u8bbe\u7f6e\u4e3a\u8be5\u7c7b\u578b\u3002\u5982\u679cwidget\u8bbe\u7f6e\u4e3aCustomContextMenu\u65f6\uff0c\u5f53\u5728\u6570\u636e\u4e0a\u70b9\u51fb\u53f3\u952e\u65f6\u5c31\u4f1a\u53d1\u9001customContextMenuRequested ( const QPoint &amp; pos )\u4fe1\u53f7\uff0c\u8be5\u4fe1\u53f7\u8fd8\u4f1a\u6355\u6349\u5230\u70b9\u51fb\u53f3\u952e\u7684\u4f4d\u7f6e\uff0c\u5e76\u7528pos\u53c2\u6570\u6765\u5b58\u50a8\u3002\u4e0e\u6b64\u4fe1\u53f7\u5173\u8054\u7684\u69fd\u51fd\u6570\u6211\u4eec\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">void studentInfo::on_stuTableWidget_customContextMenuRequested(QPoint pos)\r\n{\r\n    popMenu-&gt;addAction(action);\r\n    popMenu-&gt;exec(QCursor::pos());\r\n}<\/pre>\n<p>\u6211\u4eec\u9996\u5148\u5c06\u83dc\u5355\u9009\u9879action\u6dfb\u52a0\u5230\u90ae\u4ef6\u5f39\u51fa\u83dc\u5355popMenu\u4e2d\uff0c\u518d\u901a\u8fc7exec\u65b9\u6cd5\u5728pos()\u4f4d\u7f6e\u663e\u793a\u8be5\u90ae\u4ef6\u83dc\u5355\uff0cpos()\u8fd4\u56de\u7684\u4f4d\u7f6e\u5373\u4e3a\u70b9\u51fb\u9f20\u6807\u7684\u4f4d\u7f6e\u3002<\/p>\n<p>\u73b0\u5728\uff0c\u5982\u679c\u70b9\u51fb\u53f3\u952e\u83dc\u5355\u9009\u9879\u5e76\u4e0d\u4f1a\u53d1\u751f\u4efb\u4f55\u52a8\u4f5c\uff0c\u8fd9\u662f\u56e0\u4e3a\u6211\u4eec\u5e76\u6ca1\u6709\u5173\u8054\u76f8\u5e94\u7684\u69fd\u51fd\u6570\u3002\u7531\u4e8e\u5177\u4f53\u7684\u83dc\u5355\u9009\u9879\u4e0d\u540c\uff0c\u5176\u51fd\u6570\u7684\u5b9e\u73b0\u4e5f\u4e0d\u540c\uff0c\u8fd9\u91cc\u6211\u4eec\u53ea\u7ed9\u51fa\u6846\u67b6\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">void studentInfo::rightClickedOperation()\r\n{\r\n    \/\/do something\r\n}<\/pre>\n<p>\u5b9a\u4e49\u597d\u69fd\u51fd\u6570\uff0c\u6700\u5173\u952e\u7684\u662f\u4e0e\u76f8\u5e94\u7684\u4fe1\u53f7\u8fde\u63a5\u3002\u5bf9\u4e8e\u4e0a\u8ff0\u4e24\u4e2a\u69fd\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u4e24\u79cd\u65b9\u6cd5\u8fdb\u884c\u4fe1\u53f7\u548c\u69fd\u7684\u5173\u8054\uff1a\u5728Qt Desinger\u6a21\u5f0f\u4e0b\u6dfb\u52a0\u6216\u624b\u52a8\u8fdb\u884cconnect\u5173\u8054\u3002\u5bf9\u4e8ecustomContextMenuRequested\u4fe1\u53f7\uff0c\u6211\u4eec\u4f7f\u7528\u524d\u79cd\u65b9\u6cd5\u5b9e\u73b0\u4fe1\u53f7\u548c\u69fd\u7684\u5173\u8054\uff1b\u5bf9\u4e8e\u53f3\u952e\u83dc\u5355\u9009\u9879\u7684\u529f\u80fd\u5b9e\u73b0\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7connect\u51fd\u6570\u5b9e\u73b0\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush:c\">connect(action, SIGNAL(triggered()), this, SLOT(rightClickedOperation()));<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>QTableWidget\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u57fa\u4e8e\u6761\u76ee\uff08item\uff09\u7684\u8868\u683c\u89c6\u56fe\u6a21\u578b\uff0c\u5728\u8be5\u90e8\u4ef6\u4e2d\u6570\u636e\u4ee5item\u4e3a\u57fa\u672c\u5355\u4f4d\uff0c\u6bcf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[199],"tags":[220,202,200],"class_list":["post-3014","post","type-post","status-publish","format-standard","hentry","category-gui","tag-cs","tag-gui","tag-qt"],"views":33972,"_links":{"self":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3014","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/comments?post=3014"}],"version-history":[{"count":0,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/posts\/3014\/revisions"}],"wp:attachment":[{"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/media?parent=3014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/categories?post=3014"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/edsionte.com\/techblog\/wp-json\/wp\/v2\/tags?post=3014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}