Important: Please read the Qt Code of Conduct -

Fill QCombobox from QAbstractTableModel

  • I write applications based on view / model. I have a CategoryModel class that inherits from QAbstractTableModel.
    And my problem occurs when I want to fill a combobox. I want the category name to be visible and I want to know which ID I choose after selecting one of the options.

    Does anyone know how to solve this problem?

  • Lifetime Qt Champion

    What about
    ui->comboBox->setModelColumn(2); // which col that has category name

  • @ppitu said in Fill QCombobox from QAbstractTableModel:


    You can Write the code inside QComboBox Event's like


    For Example:
    you can populate your QComboBox And select the value from it and Get the selected text using currentText() Function and find the record in your table based on your selected text in your QComboBox

  • @mrjj When i try set model i got this error: no viable conversion from 'CategoryModel' to 'QAbstractItemModel *'
    and this my code:


    CategoryModel class:

    class SUMMARYPAYMENTAPPCORE_EXPORT CategoryModel : public QAbstractTableModel
        enum Roles {
            CLASSROLE = Qt::UserRole + 1
        enum CategoryField {
            ID = 0,
        CategoryModel(QObject *parent = nullptr);
        QModelIndex addCategory(const Category& category);
        bool removeRows(int row, int count, const QModelIndex& parent) override;
        int rowCount(const QModelIndex& parent = QModelIndex()) const override;
        int columnCount(const QModelIndex& parent = QModelIndex()) const override;
        QVariant data(const QModelIndex& index, int role) const override;
        bool setData(const QModelIndex& index, const QVariant& value, int role) override;
        QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
        void fillCombobix(Q)
        bool isIndexValid(const QModelIndex& index) const;
        DatabaseManager& mDb;
        std::unique_ptr<std::vector<std::unique_ptr<Category>>> mCategories;

  • Lifetime Qt Champion

    @ppitu said in Fill QCombobox from QAbstractTableModel:

    from 'CategoryModel' to 'QAbstractItemModel *'

    Because it needs a pointer, not an object.

  • @ppitu said in Fill QCombobox from QAbstractTableModel:

    no viable conversion

    Now i see, thanks for help

Log in to reply