Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct


    Qt World Summit: Early-Bird Tickets

    Solved Fill QCombobox from QAbstractTableModel

    General and Desktop
    4
    6
    303
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • P
      ppitu last edited by

      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?

      Ketan__Patel__0011 1 Reply Last reply Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

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

        P 1 Reply Last reply Reply Quote 0
        • Ketan__Patel__0011
          Ketan__Patel__0011 @ppitu last edited by

          @ppitu said in Fill QCombobox from QAbstractTableModel:

          QAbstractTableModel

          You can Write the code inside QComboBox Event's like

          currentIndexChange 
          
          CurrentTextChange
          

          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

          1 Reply Last reply Reply Quote 0
          • P
            ppitu @mrjj last edited by

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

            ui->categoryComboBox->setModel(mCategoryModel);
            

            CategoryModel class:

            class SUMMARYPAYMENTAPPCORE_EXPORT CategoryModel : public QAbstractTableModel
            {
                Q_OBJECT
            public:
                enum Roles {
                    CLASSROLE = Qt::UserRole + 1
                };
            
                enum CategoryField {
                    ID = 0,
                    NAME
                };
            
                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)
            
            private:
                bool isIndexValid(const QModelIndex& index) const;
            
            private:
                DatabaseManager& mDb;
                std::unique_ptr<std::vector<std::unique_ptr<Category>>> mCategories;
            };
            
            1 Reply Last reply Reply Quote 0
            • Christian Ehrlicher
              Christian Ehrlicher Lifetime Qt Champion last edited by

              @ppitu said in Fill QCombobox from QAbstractTableModel:

              from 'CategoryModel' to 'QAbstractItemModel *'

              Because it needs a pointer, not an object.

              Qt has to stay free or it will die.

              1 Reply Last reply Reply Quote 1
              • P
                ppitu last edited by

                @ppitu said in Fill QCombobox from QAbstractTableModel:

                no viable conversion

                Now i see, thanks for help

                1 Reply Last reply Reply Quote 0
                • First post
                  Last post