0

I would like to add QComboBox with inputs (Id, Name, Year) to the first row of QAbstractTableModel? I tried similar posts How to set data to QComboBox using QAbstractTableModel (Model/View)? and How to add "Select one..." to QComboBox when using QAbstractTableModel (Model/View)?. However, it did not work out (app is crashing all the time). How can I adjust my existing code? I am new to PyQT, any help highly appreciated. Thanks a lot!

My Code

class PandasModel(QtCore.QAbstractTableModel):

    def __init__(self, df = pd.DataFrame(), parent=None):

        QtCore.QAbstractTableModel.__init__(self, parent=parent)
        self._df = df
        row_count = 700
        column_count = 700

    def rowCount(self, parent=QtCore.QModelIndex()):
        return len(self._df.index)

    def columnCount(self, parent=QtCore.QModelIndex()):
        return len(self._df.columns)


    def headerData(self, section, orientation, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        if orientation == QtCore.Qt.Horizontal:
            try:
                return self._df.columns.tolist()[section]
            except (IndexError, ):
                return QtCore.QVariant()
        elif orientation == QtCore.Qt.Vertical:
            try:
                # return self.df.index.tolist()
                return self._df.index.tolist()[section]
            except (IndexError, ):
                return QtCore.QVariant()


    def data(self, index, role=QtCore.Qt.DisplayRole):
        if role != QtCore.Qt.DisplayRole:
            return QtCore.QVariant()

        if not index.isValid():
            return QtCore.QVariant()

        return QtCore.QVariant(str(self._df.iloc[index.row(), index.column()]))
  • 2
    I'm afraid you are a bit confused. A model is a *data structure*, you cannot add a widget to it, since a widget is a *visual* element (part of the UI, so *not* the model). What you want to do is to *show* a combo box in the *view* (probably, a QTableView). I strongly suggest you to go through the [Qt model/view documentation](https://doc.qt.io/qt-5/model-view-programming.html) and [tutorial](https://doc.qt.io/qt-5/modelview.html). – musicamante May 03 '21 at 15:42
  • @musicamante I have an empty frame where I click the load CSV data. Do you mean QComboBox should appear right from the beginning on this frame? – Aslan Aliyev May 03 '21 at 15:50
  • 2
    I believe you still don't understand. QComboBox is a UI element, it has nothing to do with the model: it's something that will be eventually shown in the *view*. If the model is empty and you want to show a combo in the first row, then, first of all, at least one row must be added to the model. How the combo is *then* shown depends on the case: you can use a *delegate* and open a persistent editor, or [`setIndexWidget`](https://doc.qt.io/qt-5/qabstractitemview.html#setIndexWidget). Please, carefully study the links given above, you cannot do model/view programming if you don't know what it is. – musicamante May 03 '21 at 16:00

0 Answers0