Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Fill QCombobox from QAbstractTableModel

Fill QCombobox from QAbstractTableModel

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 4 Posters 777 Views
  • 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 Offline
    P Offline
    ppitu
    wrote on last edited by
    #1

    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__0011K 1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      P 1 Reply Last reply
      0
      • P ppitu

        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__0011K Offline
        Ketan__Patel__0011K Offline
        Ketan__Patel__0011
        wrote on last edited by
        #3

        @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
        0
        • mrjjM mrjj

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

          P Offline
          P Offline
          ppitu
          wrote on last edited by
          #4

          @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
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @ppitu said in Fill QCombobox from QAbstractTableModel:

            from 'CategoryModel' to 'QAbstractItemModel *'

            Because it needs a pointer, not an object.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            1
            • P Offline
              P Offline
              ppitu
              wrote on last edited by
              #6

              @ppitu said in Fill QCombobox from QAbstractTableModel:

              no viable conversion

              Now i see, thanks for help

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved