Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. Why won't ComboBox see QVector<int> member as a model?
Forum Updated to NodeBB v4.3 + New Features

Why won't ComboBox see QVector<int> member as a model?

Scheduled Pinned Locked Moved Solved QML and Qt Quick
2 Posts 1 Posters 279 Views 1 Watching
  • 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.
  • fcarneyF Offline
    fcarneyF Offline
    fcarney
    wrote on last edited by
    #1

    Model header:

    class OkiDataPrinterUI : public QObject
    {
        Q_OBJECT
    
        Q_PROPERTY(QVector<int> cpiList READ cpiList NOTIFY cpiListChanged)
    public:
        explicit OkiDataPrinterUI(OkiDataPrinter *printer, QObject *parent = nullptr);
    
        QVector<int> cpiList(){
            return m_cpiList;
        }
    
    signals:
        void cpiListChanged(QVector<int> cpiList);
    
    private:
        OkiDataPrinter* m_printer;
    
        QVector<int> m_cpiList;
    
    };
    

    Model source:

    OkiDataPrinterUI::OkiDataPrinterUI(OkiDataPrinter *printer, QObject *parent)
        : QObject(parent)
        , m_printer(printer)
    {
        m_cpiList.push_back(10);
        m_cpiList.push_back(12);
        m_cpiList.push_back(15);
        m_cpiList.push_back(17);
        m_cpiList.push_back(20);
    }
    

    related qml:

                               ComboBox {
                                    id: printer_cpi2
                                    currentIndex: 0
                                    model: printui.cpiList
    
                                }
    

    I don't get errors. I did get an error from the ComboBox when I didn't have a notify signal.

    C++ is a perfectly valid school of magic.

    1 Reply Last reply
    0
    • fcarneyF Offline
      fcarneyF Offline
      fcarney
      wrote on last edited by
      #2

      Nevermind, I did this instead:

      class CPIVal : public QObject
      {
          Q_OBJECT
      
          Q_PROPERTY(QString name READ name)
      public:
          CPIVal(QObject* parent, QString name)
              : QObject()
              , m_name(name)
          {
          }
      
          QString name(){
              return m_name;
          }
      
      private:
          QString m_name;
      };
      
      class OkiDataPrinterUI : public QObject
      {
          Q_OBJECT
      
          Q_PROPERTY(QList<QObject*> cpiList READ cpiList NOTIFY cpiListChanged)
      public:
          explicit OkiDataPrinterUI(OkiDataPrinter *printer, QObject *parent = nullptr);
      
          QList<QObject*> cpiList(){
              return m_cpiList;
          }
      
      signals:
          void cpiListChanged(QVector<int> cpiList);
      
      private:
          OkiDataPrinter* m_printer;
      
          QList<QObject*> m_cpiList;
      
      };
      

      source:

      OkiDataPrinterUI::OkiDataPrinterUI(OkiDataPrinter *printer, QObject *parent)
          : QObject(parent)
          , m_printer(printer)
      {
          m_cpiList.push_back(new CPIVal(this, "10"));
          m_cpiList.push_back(new CPIVal(this, "12"));
          m_cpiList.push_back(new CPIVal(this, "15"));
          m_cpiList.push_back(new CPIVal(this, "17"));
          m_cpiList.push_back(new CPIVal(this, "20"));
      }
      
      

      C++ is a perfectly valid school of magic.

      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