向qtablewidget pyq添加小部件

2024-04-25 07:56:29 发布

您现在位置:Python中文网/ 问答频道 /正文

在qtablewidget中还有像按钮一样添加的吗?但单元格中的日期仍必须显示,例如,如果用户双击单元格,我可以像按钮一样发送信号吗?谢谢!

编辑项():

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)

Tags: to用户self编辑goif信号def
2条回答

在PyQt4中,将按钮添加到qtablewidget:

btn= QtGui.QPushButton('Hello')
qtable_name.setCellWidget(0,0, btn) # qtable_name is your qtablewidget name

您有两个问题综合在一起…简短回答,是的,您可以向QTableWidget添加按钮-您可以通过调用setCellWidget向table widget添加任何widget:

# 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)

相关问题 更多 >