Changing PyQT Table Item from QComboBox to QTableWidgetItem
<p>In my PyQT window, I have a table containing <code>QComboBox</code> in one column. How can the <code>QComboBox</code> later be changed to the regular <code>QTableWidgetItem</code> to display some text?</p>

<p>I tried the following but the <code>QComboBox</code> was not replaced by text from <code>QTableWidgetItem</code>.</p>

<pre><code>myTable= QTableWidget()
myTable.setRowCount(6)
myTable.setColumnCount(2)
myTable.setHorizontalHeaderLabels(QString("Name;Age;").split(";"))
myTable.horizontalHeader().setResizeMode(QHeaderView.Stretch)


# Populate with QComboBox in column 1
for i, name in enumerate(nameList):
    myTable.setItem(i, 0, QTableWidgetItem(name ))

    ageCombo = QComboBox()
    for option in ageComboOptions:
        ageCombo.addItem(option)
    myTable.setCellWidget(i, 1, ageCombo)

# Change column 1 to QTableWidgetItem
for i, name in enumerate(nameList):
    myTable.setItem(i, 1, QTableWidgetItem(name))
</code></pre>

<p>The short answer is that if you just <code>removeCellWidget</code> you'll get what you want.  Example code below.</p>

<p>But in more detail:</p>

<p>The "Item" as set by <code>setItem</code> and the "Widget" as set by <code>setCellWidget</code> are different - they play different roles.  The item carries the data for the cell: in the model view architecture it's in the model.  The widget is doing the display: it's in the view.  So, when you set the cell widget you might still expect it to use an item in the model behind it.  However, the <code>QTableWidget</code> provides a simplified API to the full model view architecture as used in QT (e.g. see the <code>QTableView</code> and <code>QAbstractitemModel</code>).  It provides its own default model which you access via an item for each cell.  Then, when you replace the widget on a cell it dispenses with any item at all and just allows you to control the widget directly. Remove the widget and it goes back to using the item.</p>

<p>Here's a working example:</p>

<pre><code>class MainWindow(QtGui.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.initUI()

    def initUI(self):

        self.myTable= QtGui.QTableWidget()
        self.myTable.setRowCount(1)
        self.myTable.setColumnCount(2)

        item1 = QtGui.QTableWidgetItem("a")
        self.myTable.setItem(0, 0, item1)
        item2 = QtGui.QTableWidgetItem("b")
        self.myTable.setItem(0, 1, item2)

        self.setCentralWidget(self.myTable)

        menubar = QtGui.QMenuBar(self)
        self.setMenuBar(menubar)
        menu = QtGui.QMenu(menubar)
        menu.setTitle("Test")
        action = QtGui.QAction(self)
        action.setText("Test 1")
        action.triggered.connect(self.test1)
        menu.addAction(action)
        action = QtGui.QAction(self)
        action.setText("Test 2")
        action.triggered.connect(self.test2)
        menu.addAction(action)
        menubar.addAction(menu.menuAction())


        self.show()

    def test1(self):
        self.myTable.removeCellWidget(0, 1)

    def test2(self):
        combo = QtGui.QComboBox()
        combo.addItem("c")
        combo.addItem("d")
        self.myTable.setCellWidget(0, 1, combo)


def main():
    app = QtGui.QApplication(sys.argv)
    mw = MainWindow()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()
</code></pre>

python python-2.7 pyqt pyqt4