Pyside example with an expandable table with deligates

Does anyone know of a example using Pyside with dynamic sizable table with a combobox delegate in a column? I tried modifying this example but I keep on getting the combo box that only displays when the cell is selected.

Here is the is the example I started from:

import sys
from PySide import QtCore, QtGui


class TableModel(QtCore.QAbstractTableModel):
    """
    A simple 5x4 table model to demonstrate the delegates
    """
    def rowCount(self, parent=QtCore.QModelIndex()): return 5
    def columnCount(self, parent=QtCore.QModelIndex()): return 4

    def data(self, index, role=QtCore.Qt.DisplayRole):
        if not index.isValid(): return None
        if not role==QtCore.Qt.DisplayRole: return None
        return "{0:02d}".format(index.row())

    def setData(self, index, value, role=QtCore.Qt.DisplayRole):
        print "setData", index.row(), index.column(), value

    def flags(self, index):
        if (index.column() == 0):
            return QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled
        else:
            return QtCore.Qt.ItemIsEnabled


class ComboDelegate(QtGui.QItemDelegate):
    """
    A delegate that places a fully functioning QComboBox in every
    cell of the column to which it's applied
    """
    def __init__(self, parent):

        QtGui.QItemDelegate.__init__(self, parent)

    def createEditor(self, parent, option, index):
        combo = QtGui.QComboBox(parent)
        li = []
        li.append("Zero")
        li.append("One")
        li.append("Two")
        li.append("Three")
        li.append("Four")
        li.append("Five")
        combo.addItems(li)
        self.connect(combo, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("currentIndexChanged()"))
        return combo

    def setEditorData(self, editor, index):
        editor.blockSignals(True)
        editor.setCurrentIndex(int(index.model().data(index)))
        editor.blockSignals(False)

    def setModelData(self, editor, model, index):
        model.setData(index, editor.currentIndex())

    #@QtCore.pyqtSlot()
    def currentIndexChanged(self):
        self.commitData.emit(self.sender())

class TableView(QtGui.QTableView):
    """
    A simple table to demonstrate the QComboBox delegate.
    """
    def __init__(self, *args, **kwargs):
        QtGui.QTableView.__init__(self, *args, **kwargs)

        # Set the delegate for column 0 of our table
        # self.setItemDelegateForColumn(0, ButtonDelegate(self))
        self.setItemDelegateForColumn(0, ComboDelegate(self))


if __name__=="__main__":
    from sys import argv, exit

    class Widget(QtGui.QWidget):
        """
        A simple test widget to contain and own the model and table.
        """
        def __init__(self, parent=None):
            QtGui.QWidget.__init__(self, parent)

            l=QtGui.QVBoxLayout(self)
            self._tm=TableModel(self)
            self._tv=TableView(self)
            self._tv.setModel(self._tm)
            for row in range(0, self._tm.rowCount()):
                self._tv.openPersistentEditor(self._tm.index(row, 0))

            l.addWidget(self._tv)

    a=QtGui.QApplication(argv)
    w=Widget()
    w.show()
    w.raise_()
    exit(a.exec_())

Answers


i modified the following class to look like this

class ComboDelegate(QtGui.QItemDelegate):
    """
    A delegate that places a fully functioning QComboBox in every
    cell of the column to which it's applied
    """
    def __init__(self, parent, options):

        QtGui.QItemDelegate.__init__(self, parent)
        self.options = options

    def createEditor(self, parent, option, index):
        combo = QtGui.QComboBox(parent)
        combo.addItems(self.options)
        self.connect(combo, QtCore.SIGNAL("currentIndexChanged(int)"), self, QtCore.SLOT("currentIndexChanged()"))
        return combo

    def setEditorData(self, editor, index):
        editor.blockSignals(True)
        editor.setCurrentIndex(editor.currentIndex())
        editor.blockSignals(False)

    def setModelData(self, editor, model, index):
        model.setData(index, editor.itemText(editor.currentIndex()))

    #@QtCore.pyqtSlot()
    def currentIndexChanged(self):
        self.commitData.emit(self.sender())

it works perfectly with QTableWidget

    self.Table = QtGui.QTableWidget(parent)
    # ... other columns before
    # ...
    item = QtGui.QTableWidgetItem("DATATYPE")
    self.Table.setHorizontalHeaderItem(3, item)
    self.Table.horizontalHeaderItem(3).setWhatsThis("DATATYPE")
    self.DataType = ComboDelegate(self.MemoryTable, DataTypeOptions)
    self.Table.setItemDelegateForColumn(3,self.DataType)

This example puts a ComboBox in each row of the 3rd Column in the TableWidget


Need Your Help

Java simple date format british time

java datetime

I am using simple date format to allow users to specify which time zone they are sending data in:

This Android SDK requires Android Developer Toolkit version 22.0.0 or above. Current version is 21.1.0.v201302060044-569685.

android eclipse android-sdk-2.3 eclipse-adt

I updated the SDK to the latest version i.e 22.0.0 from help>>install new software and then I put the url " http://dl-ssl.google.com/android/eclipse/ " there then I ticked "Developer Tools" and "NDK