How to set data for QAbstractTableModel?
-
Hi
I am using qt to create model for my project. In that i create Qabstracttablemodel is used to store the different data. I attached the snippet of the code.
table .h#ifndef TABLEMODEL_H #define TABLEMODEL_H #include <QAbstractTableModel> #include <QList> #include <QDebug> struct DrugDetail { QString m_DrugName; QString m_concentraion; QString m_dosingUnits; }; class TableModel : public QAbstractTableModel { Q_OBJECT public: TableModel( QObject *parent = nullptr); enum roleName{ R_DrugName=1, R_Concentration=2, R_dosingUnits=3 }; int rowCount(const QModelIndex &parent) const override; int columnCount(const QModelIndex &parent) const override; QVariant data(const QModelIndex &index, int role) const override; QHash<int, QByteArray> roleNames() const override; bool setData(const QModelIndex &index, const QVariant &value, int role) override; bool insertRows(int position, int rows, const QModelIndex &index = QModelIndex()) override; private: QList<DrugDetail> drugDetails; }; #endif // TABLEMODEL_H
table.cpp
#include "Header/tablemodel.h" TableModel::TableModel( QObject *parent) : QAbstractTableModel(parent) { // drugDetails.append({"doub","150mg","ml/hr"}); // drugDetails.append({"bba","50mg","ml/hr/kg"}); } int TableModel::rowCount(const QModelIndex &parent) const { return drugDetails.size(); } int TableModel::columnCount(const QModelIndex &parent) const { return roleNames().size(); } QHash<int, QByteArray> TableModel::roleNames() const { QHash<int,QByteArray> roles; roles.insert(R_DrugName,"drugName"); roles.insert(R_Concentration,"concentration"); roles.insert(R_dosingUnits,"dosingUnits"); return roles; } bool TableModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role ==R_DrugName ) { int row = index.row(); qDebug()<<"Role Entered"; auto contact = drugDetails.value(row); if (index.column() == 0) { contact.m_DrugName = value.toString(); } else if (index.column() == 1) contact. = value.toString(); else { return false; } drugDetails.replace(row, contact); emit(dataChanged(index, index)); return true; } return false; } QVariant TableModel::data(const QModelIndex &index, int role) const { QVariant variant; const int row=index.row(); const int col=role; switch(col) { case R_DrugName: variant=drugDetails.at(row).m_DrugName; qDebug()<<"Inserted"; break; case R_Concentration: variant=drugDetails.at(row).m_concentraion; break; case R_dosingUnits: variant=drugDetails.at(row).m_dosingUnits; break; default: break; } return variant; }
I have doubt relate to set the data for model based on the roles and i tried but, it didn't display any data in the view. can i know the details of the working of the set data function to the model.
-
Role != Column. They are 2 VERY different things. Also the first 256 roles are reserved by Qt and some of them well defined: http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum
For what you want to do, you just need to considerQt::EditRole
andQt::DisplayRole
. All the other checks you do regarding roles should use columns instead -
@eswaramrth03 said in How to set data for QAbstractTableModel?:
enum roleName{
R_DrugName=1,
R_Concentration=2,
R_dosingUnits=3}
Role names should start with
Qt::UserRole + 1
, not with 1, 2, 3. -
Hi @VRonin
Thanks for reply. I need to access the drugdetail member list to qml combobox. The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?
-
Hi @VRonin
Thanks for reply. I need to access the drugdetail member list to qml combobox. The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?
@eswaramrth03 said in How to set data for QAbstractTableModel?:
access the data in combo box
The combobox will show the
Qt::DisplayRole
of the first column of the model -
Role != Column. They are 2 VERY different things. Also the first 256 roles are reserved by Qt and some of them well defined: http://doc.qt.io/qt-5/qt.html#ItemDataRole-enum
For what you want to do, you just need to considerQt::EditRole
andQt::DisplayRole
. All the other checks you do regarding roles should use columns instead@eswaramrth03 said in How to set data for QAbstractTableModel?:
I need to access the drugdetail member list to qml combobox.
A ComboBox uses a list model, not a table model. If you want to display drug name, concentration, and dosing units within the same combobox, you must concatenate them into a single string:
QString drugDetail = m_DrugName + ' ' + m_concentration + ' ' + m_dosingUnits;
The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?
In QML, you must set the textRole.
Before you rewrite your model, try a simple example first:
- Put the drug names into a
QStringListModel
- Set the QStringListModel as your ComboBox's model.
- Set your ComboBox's textRole to
"display"
.
Note:
"display"
corresponds toQt::DisplayRole
: http://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames@VRonin said in How to set data for QAbstractTableModel?:
Role != Column.
Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.
I really dislike this design. This introduces a huge inconsistency between C++ and QML; a model implemented for use with a C++ View usually can't be used with a QML View without wrapper code: http://doc.qt.io/qt-5/qtquickcontrols1-filesystembrowser-main-cpp.html
The combobox will show the Qt::DisplayRole of the first column of the model
This is true for a C++ QComboBox.
This is not true for QML ComboBox unless we set
textRole: "display"
. - Put the drug names into a
-
@eswaramrth03 said in How to set data for QAbstractTableModel?:
I need to access the drugdetail member list to qml combobox.
A ComboBox uses a list model, not a table model. If you want to display drug name, concentration, and dosing units within the same combobox, you must concatenate them into a single string:
QString drugDetail = m_DrugName + ' ' + m_concentration + ' ' + m_dosingUnits;
The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?
In QML, you must set the textRole.
Before you rewrite your model, try a simple example first:
- Put the drug names into a
QStringListModel
- Set the QStringListModel as your ComboBox's model.
- Set your ComboBox's textRole to
"display"
.
Note:
"display"
corresponds toQt::DisplayRole
: http://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames@VRonin said in How to set data for QAbstractTableModel?:
Role != Column.
Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.
I really dislike this design. This introduces a huge inconsistency between C++ and QML; a model implemented for use with a C++ View usually can't be used with a QML View without wrapper code: http://doc.qt.io/qt-5/qtquickcontrols1-filesystembrowser-main-cpp.html
The combobox will show the Qt::DisplayRole of the first column of the model
This is true for a C++ QComboBox.
This is not true for QML ComboBox unless we set
textRole: "display"
.Hi @JKSH
Thanks for the reply. I need to display the drugname, concentration and dosing units in different combo box. If i select the one of the drugname, i need to filter the concentration data and update the filter data in concentration combo box. if i choose concentration in that combobox and filter the dosing unit based on concentraion data and update in the dosing unit combobox. It is possible to use single QAbstractlistmodel to achieve this concept? or 3 different Qabstractlistmodel is need to achieve this concept. I am also new to qt and learn the model right now only.
- Put the drug names into a
-
@eswaramrth03 said in How to set data for QAbstractTableModel?:
I need to access the drugdetail member list to qml combobox.
A ComboBox uses a list model, not a table model. If you want to display drug name, concentration, and dosing units within the same combobox, you must concatenate them into a single string:
QString drugDetail = m_DrugName + ' ' + m_concentration + ' ' + m_dosingUnits;
The data will be acessed based on the textRole property in qml. How can i set the data and access the data in combo box without text roles?
In QML, you must set the textRole.
Before you rewrite your model, try a simple example first:
- Put the drug names into a
QStringListModel
- Set the QStringListModel as your ComboBox's model.
- Set your ComboBox's textRole to
"display"
.
Note:
"display"
corresponds toQt::DisplayRole
: http://doc.qt.io/qt-5/qabstractitemmodel.html#roleNames@VRonin said in How to set data for QAbstractTableModel?:
Role != Column.
Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.
I really dislike this design. This introduces a huge inconsistency between C++ and QML; a model implemented for use with a C++ View usually can't be used with a QML View without wrapper code: http://doc.qt.io/qt-5/qtquickcontrols1-filesystembrowser-main-cpp.html
The combobox will show the Qt::DisplayRole of the first column of the model
This is true for a C++ QComboBox.
This is not true for QML ComboBox unless we set
textRole: "display"
.@JKSH said in How to set data for QAbstractTableModel?:
Unfortunately, Role == Column in TableView and TreeView of Qt Quick Controls.
TableView not anymore in version 2. And they are working on changing TreeView as well.
If i select the one of the drugname, i need to filter the concentration data and update the filter data in concentration combo box.
You can use a proxy model like: http://doc.qt.io/qt-5/qtquickcontrols1-tableview-example.html
- Put the drug names into a