将小部件添加到qtablewidget pyqt
无论如何在qtablewidget中添加像按钮一样的内容?但是必须在单元格中显示日期,例如,如果用户双击某个单元格,我是否可以像按钮一样发送信号?谢谢!
edititem():
def editItem(self,clicked):
if clicked.row() == 0:
#go to tab1
if clicked.row() == 1:
#go to tab1
if clicked.row() == 2:
#go to tab1
if clicked.row() == 3:
#go to tab1
表触发器:
self.table1.itemDoubleClicked.connect(self.editItem)
-
您有几个问题可以归结为一个…简短答案,是的,您可以向QTableWidget添加按钮-
您可以通过调用setCellWidget将任何窗口小部件添加到表格窗口小部件:# 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)
但这听起来并不像您真正想要的。
听起来您想对用户双击一个单元格做出反应,就像他们单击了一个按钮一样,大概是要弹出对话框或编辑器之类的东西。
在这种情况下,您真正需要做的就是从QTableWidget连接到itemDoubleClicked信号,如下所示:
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)