Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



  • 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.



  • 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"));
    }
    
    

Log in to reply