Adding widgets to qtablewidget pyqt

前端 未结 2 630
南笙
南笙 2020-12-06 16:56

Is there anyway to add like a button in qtablewidget? But the date within the cell would stil have to be displaying, for example if an user double clicked a cell, could i se

相关标签:
2条回答
  • 2020-12-06 17:49

    You have a couple of questions rolled into one...short answer, yes, you can add a button to a QTableWidget - you can add any widget to the table widget by calling 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)
    

    But that doesn't sound like what you actually want.

    It sounds like you want to react to a user double-clicking one of your cells, as though they clicked a button, presumably to bring up a dialog or editor or something.

    If that is the case, all you really need to do is connect to the itemDoubleClicked signal from the QTableWidget, like so:

    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)
    
    0 讨论(0)
  • 2020-12-06 17:52

    In PyQt4 add button to qtablewidget :

    btn= QtGui.QPushButton('Hello')
    qtable_name.setCellWidget(0,0, btn) # qtable_name is your qtablewidget name
    
    0 讨论(0)
提交回复
热议问题