擅长:python、mysql、java
<p>您有两个问题综合在一起…简短回答,是的,您可以向QTableWidget添加按钮-您可以通过调用setCellWidget向table widget添加任何widget:</p>
<pre><code># initialize a table somehow
table = QTableWidget(parent)
table.setRowCount(1)
table.setColumnCount(1)
# create an cell widget
btn = QPushButton(table)
btn.setText('12/1/12')
table.setCellWidget(0, 0, btn)
</code></pre>
<p>但听起来不像你真正想要的。</p>
<p>听起来好像你想对用户双击你的一个单元格做出反应,就好像他们点击了一个按钮,大概是为了打开一个对话框或编辑器什么的。</p>
<p>如果是这样的话,您真正需要做的就是从QTableWidget连接到itemDoubleClicked信号,如下所示:</p>
<pre><code>def editItem(item):
print 'editing', item.text()
# initialize a table widget somehow
table = QTableWidget(parent)
table.setRowCount(1)
table.setColumnCount(1)
# create an item
item = QTableWidgetItem('12/1/12')
table.setItem(0, 0, item)
# if you don't want to allow in-table editing, either disable the table like:
table.setEditTriggers( QTableWidget.NoEditTriggers )
# or specifically for this item
item.setFlags( item.flags() ^ Qt.ItemIsEditable)
# create a connection to the double click event
table.itemDoubleClicked.connect(editItem)
</code></pre>