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

How to display a comboBox and a TableView using Model that I make in c++.



  • Hello.

    I have a problem about how to display a comboBox and a TableView.
    I try to make a screen that displays a comboBox and a TableView .

    I checked below souece of youtube :

    https://www.youtube.com/watch?v=9BcAYDlpuT8

    This Model data is only one set below


    todolist.h

    struct ToDoItem
    {
    bool done;
    QString description;
    };

    But I want to use 2 data set and Qlist like


    todolist.h
    typedef struct t_todoitem
    {
    ModelDataMisssionID MisssionIDItems;
    ModelDataWpSelection WpSelectionItems;
    } attribute ((packed)) T_ToDoItem;

    static T_MODELDATA* s_ptToDoItem;;

    ・・・・・・・・

    private:
    QList<T_MODELDATA> mItems;

    todolist.cpp
    ToDoList::ToDoList(QObject* parent): QObject(parent)
    {
    mItems.append(s_ptToDoItem->MisssionIDItems("TestA");) ←///////////////////////error////////////////////////
    mItems.append(s_ptToDoItem->MisssionIDItems("TestA");) ←///////////////////////error////////////////////////
    mItems.append(s_ptToDoItem-WpSelectionItems;("TestX","TestY","TestZ",);) ←///////////////////////error////////////////////////
    }

    modeldatamissionid.h

    class ModelDataMisssionID
    {
    public:
    ModelDataMisssionID(const QString &missionID):m_missionID(missionID){
    qDebug() << "Push Btn:" << missionID;
    }
    const QString& missionID() const { return m_missionID; }

    private:
    QString m_missionID;

    };


    class modeldataselection.h
    {
    public:
    ModelDataWpSelection(const QString &wpNumber, const QString &date, const QString &type)
    :m_wpNumber(wpNumber),m_date(date),m_type(type){
    qDebug() << "Push Btn:" << wpNumber;
    }
    const QString& wpNumber() const { return m_wpNumber; }
    const QString& date() const { return m_date; }
    const QString& type() const { return m_type; }

    private:
    QString m_wpNumber;
    QString m_date;
    QString m_type;
    };

    Now i do not know how to make Qlist including 2 set data of struct.
    And using reference of youtube, I do not know it possible to acocmplish my goal.
    My goal is that I display a comboBox and a Tableview using Model that I make in c++.

    My question is

    1. How do you make Qlist inclding struct data ?
    2. Using Qlist multi set data and insert them to model source(todomodel.cpp of youtube source) ,is it possible to disolay comboBox and TableView?

  • Moderators

    Please use ``` tags to wrap your code. It's very hard to read without them.

    How do you make Qlist inclding struct data ?

    Just use it, it works :-) I think you have added a lot of typedef, static and pointer cruft to that code and it's needlessly complicated.

    struct SomeStruct {
      bool abc = false;
      QString someText;
    };
    
    QList<SomeStruct> list;
    

    Using Qlist multi set data and insert them to model source(todomodel.cpp of youtube source) ,is it possible to disolay comboBox and TableView?

    I won't watch the youtube video, but will answer without it: it's definitely possible to use C++ models in QML. There is a very detailed entry about it in the documentation.



  • This post is deleted!


  • @sierdzio

    Thank you for your answer.

    I separated into two QLIST like

    ex.
    ■todolist.h

    QList<ModelDataMisssionID> mMissionItems;
    QList<ModelDataWpSelection> mWpSelectionItems;

    But I have a problems about how to make model a list.

    I have a model file which have the function rowcount , function data, and function rolwNames etc.

    If I use 2 Qlists of todolist.h in model file(called MyModel.h/cpp) , I currently have a problem in the function rowcount.

    A current error is below.

    ASSERT failure in QList<T>::at: "index out of range", file C:\Qt\Qt5.14.1\5.14.1\mingw73_32\include/QtCore/qlist.h, line 571

    My current source is below (I pick up important code)

    ■todolist.h
    public:
    QList<ModelDataMisssionID> ModelDataMisssionIDItems() const;
    QList<ModelDataWpSelection> items() const;

    ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・

    private:
    QList<ModelDataMisssionID> mMissionItems;
    QList<ModelDataWpSelection> mWpSelectionItems;

    ■todolist.h
    todolist:todolist(QObject* parent){

    //comboBox initial data
    mMissionItems.append(ModelDataMisssionID("TestA"));
    mMissionItems.append(ModelDataMisssionID("TestB"));
    
    //TableView initial data
    mWpSelectionItems.append(ModelDataWpSelection("1","1217  09:00","TestA"));
    mWpSelectionItems.append(ModelDataWpSelection("2","1218 09:00","TestB"));
    mWpSelectionItems.append(ModelDataWpSelection("3","1219 09:00","TestC"));
    mWpSelectionItems.append(ModelDataWpSelection("4","1220 09:00","TestD"));
    

    }

    QList<ModelDataMisssionID> todolist::ModelDataMisssionIDItems() const
    {
    return mMissionItems;
    }

    QList<ModelDataWpSelection> todolist::items() const
    {
    return mWpSelectionItems;
    }

    ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・

    ■MyModel.h
    class MyModel : public QAbstractListModel
    {
    Q_OBJECT

    ・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・・

    todolist *m_list;
    

    ■MyModel.cpp
    int MyModel::rowCount(const QModelIndex &parent) const
    {
    if ( parent.isValid() || !m_list )
    return 0;

    return m_list->ModelDataMisssionIDItems().count()  + m_list->items().count(); ←error
    

    }

    I guess I should not add 2 list counts in the function rowCount. But I do not understand this solution .
    Because I think I can make only on the function rowCounts.
    Do you have any solutions about this problem ??



  • I wont have a look at the video either. Could you please describe your use case ?You have two separate kind of data. So you need two seperate model implementations. One that contains and manages the list of ModelDataMisssionID and one that manages and lists a list of ModelDataWpSelection. This would be one ( the esay) solution for the beginning. If you need to aggregate ModelDataWpSelection in ModelDataMisssion then you must implement a class according to this requirement.

    // derive from AbstractListModel if the list data will not be updated otherwise derive from AbstractItemModel !
    
    ModelDataWpSelection: : public QObject
    {
    ...
    }
    
    class ModelDataMisssion : public QObject
    {
    ...
    private:
        ModelDataWpSelection _wpSelectionModel;
    }
    
    // you then can control the type of data returned by the model using roles
    class ModelDataMisssion : public QAbstractListModel   
    {
       // MissonDataRoles
        enum MissonDataRoles
        {
            MissionRole = Qt::UserRole + 1,
            WaypointSelectionRole
        };
    ...
    }
    
      
    }
    

Log in to reply