将小部件添加到qtablewidget pyqt

发布于 2021-01-29 18:01:57

无论如何在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)
关注者
0
被浏览
49
1 个回答
  • 面试哥
    面试哥 2021-01-29
    为面试而生,有面试问题,就找面试哥。

    您有几个问题可以归结为一个…简短答案,是的,您可以向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)
    


知识点
面圈网VIP题库

面圈网VIP题库全新上线,海量真题题库资源。 90大类考试,超10万份考试真题开放下载啦

去下载看看