Conclusion of fixed lists in the editing table. Qt 6.2.1
-
@jsulm Hi,
Now my code looks like this:
statusdelegat.h
#ifndef STATUSDELEGAT_H #define STATUSDELEGAT_H #include <QStyledItemDelegate> #include <QObject> #include <QComboBox> class statusDelegat : public QStyledItemDelegate { Q_OBJECT public: explicit statusDelegat(QObject *parent = nullptr); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; }; #endif // STATUSDELEGAT_H
statusdelegat.cpp
#include "statusdelegat.h" statusDelegat::statusDelegat(QObject *parent) : QStyledItemDelegate{parent} { } QWidget *statusDelegat::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QComboBox *editor = new QComboBox(parent); return editor; } void statusDelegat::setEditorData(QWidget *editor, const QModelIndex &index) const { //int value = index.model()->data(index, Qt::EditRole).toInt(); QComboBox *statusComboBox = static_cast<QComboBox*>(editor); QMap<int, QString> map; map[1] = "Show"; map[2] = "Hide"; map[5] = "Moderation"; for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e)); }
dbeditor.cpp
#include "dbeditor.h" #include "ui_dbeditor.h" #include "statusdelegat.h" model = new QSqlTableModel; model->setTable("TABLE"); model->setEditStrategy(QSqlTableModel::OnFieldChange); model->setHeaderData(0, Qt::Horizontal, tr("id")); model->setHeaderData(1, Qt::Horizontal, tr("Status")); model->setHeaderData(2, Qt::Horizontal, tr("Name")); ui->tableView->setModel(model); ui->tableView->setItemDelegateForColumn(1, new statusDelegat); model->select();
What else should I do to make it work?
-
@jsulm Hi,
Now my code looks like this:
statusdelegat.h
#ifndef STATUSDELEGAT_H #define STATUSDELEGAT_H #include <QStyledItemDelegate> #include <QObject> #include <QComboBox> class statusDelegat : public QStyledItemDelegate { Q_OBJECT public: explicit statusDelegat(QObject *parent = nullptr); QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const override; void setEditorData(QWidget *editor, const QModelIndex &index) const override; }; #endif // STATUSDELEGAT_H
statusdelegat.cpp
#include "statusdelegat.h" statusDelegat::statusDelegat(QObject *parent) : QStyledItemDelegate{parent} { } QWidget *statusDelegat::createEditor(QWidget *parent, const QStyleOptionViewItem &/* option */, const QModelIndex &/* index */) const { QComboBox *editor = new QComboBox(parent); return editor; } void statusDelegat::setEditorData(QWidget *editor, const QModelIndex &index) const { //int value = index.model()->data(index, Qt::EditRole).toInt(); QComboBox *statusComboBox = static_cast<QComboBox*>(editor); QMap<int, QString> map; map[1] = "Show"; map[2] = "Hide"; map[5] = "Moderation"; for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e)); }
dbeditor.cpp
#include "dbeditor.h" #include "ui_dbeditor.h" #include "statusdelegat.h" model = new QSqlTableModel; model->setTable("TABLE"); model->setEditStrategy(QSqlTableModel::OnFieldChange); model->setHeaderData(0, Qt::Horizontal, tr("id")); model->setHeaderData(1, Qt::Horizontal, tr("Status")); model->setHeaderData(2, Qt::Horizontal, tr("Name")); ui->tableView->setModel(model); ui->tableView->setItemDelegateForColumn(1, new statusDelegat); model->select();
What else should I do to make it work?
@MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:
What else should I do to make it work?
Would be nice if you indicated what does not work?
for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e));
You intend to make the text and the value both be the same string?
You implement
setEditorData()
but have no need forsetModelData()
?I don't know the answers to these, they are questions for you.
-
@MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:
What else should I do to make it work?
Would be nice if you indicated what does not work?
for (auto e : map.keys()) statusComboBox->addItem(map.value(e), map.value(e));
You intend to make the text and the value both be the same string?
You implement
setEditorData()
but have no need forsetModelData()
?I don't know the answers to these, they are questions for you.
@JonB Hi,
-
Nothing has changed, in the first column values of 1, 2, 5
-
No, there should be values of 1, 2, 5. I still do not know how to get them
-
Setmodeldata() probably needed, but I still do not know how to write this method.
After all, I already have ui->tableView->setModel(model);
-
-
@JonB Hi,
-
Nothing has changed, in the first column values of 1, 2, 5
-
No, there should be values of 1, 2, 5. I still do not know how to get them
-
Setmodeldata() probably needed, but I still do not know how to write this method.
After all, I already have ui->tableView->setModel(model);
@MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:
Nothing has changed, in the first column values of 1, 2, 5
With your code you should be seeing the strings. Do you mean in the model/database? You have done nothing to update the model.
To get a combobox still showing the string texts but having the values 1,2 5, you should need
for (auto e : map.keys()) statusComboBox->addItem(map.value(e), e);
You should review what
setEditorData()
is called to do. The creation of the combo items belongs in thecreateEditor()
method.setEditorData()
needs to useindex
to read the current value in the model and select the corresponding item in the combobox.Similarly nothing is going to store the user's selection back to the model till you implement
setModelData()
. Which needs to use theindex
passed to it to set the value into the model from the item chosen in the combo. -
-
@MyNick-0 said in Conclusion of fixed lists in the editing table. Qt 6.2.1:
Nothing has changed, in the first column values of 1, 2, 5
With your code you should be seeing the strings. Do you mean in the model/database? You have done nothing to update the model.
To get a combobox still showing the string texts but having the values 1,2 5, you should need
for (auto e : map.keys()) statusComboBox->addItem(map.value(e), e);
You should review what
setEditorData()
is called to do. The creation of the combo items belongs in thecreateEditor()
method.setEditorData()
needs to useindex
to read the current value in the model and select the corresponding item in the combobox.Similarly nothing is going to store the user's selection back to the model till you implement
setModelData()
. Which needs to use theindex
passed to it to set the value into the model from the item chosen in the combo.@JonB
Excellent! I added SetModeldata():void statusDelegat::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *statusComboBox = static_cast<QComboBox*>(editor); QString value = QString::number(statusComboBox->itemData(statusComboBox->currentIndex()).toInt()); model->setData(index, value, Qt::EditRole); }
and appeared ComboBox and I can change the values.
I only have to replace the numbers with the corresponding text values from the map array, with the initial show of the "Status" field, but I do not understand what method I should do this?
-
@JonB
Excellent! I added SetModeldata():void statusDelegat::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const { QComboBox *statusComboBox = static_cast<QComboBox*>(editor); QString value = QString::number(statusComboBox->itemData(statusComboBox->currentIndex()).toInt()); model->setData(index, value, Qt::EditRole); }
and appeared ComboBox and I can change the values.
I only have to replace the numbers with the corresponding text values from the map array, with the initial show of the "Status" field, but I do not understand what method I should do this?
@MyNick-0
If you usestatusComboBox->addItem(map.value(e), e)
then the numeric value,e
, is stored as userdata with the item. Then QVariant currentData(int role = Qt::UserRole) const returns the selected item's value. You will want that duringsetModelData()
.There are also methods to read any item's value or find an item with a specified value if you need them, e.g. during your
setEditorData()
. -
@MyNick-0
If you usestatusComboBox->addItem(map.value(e), e)
then the numeric value,e
, is stored as userdata with the item. Then QVariant currentData(int role = Qt::UserRole) const returns the selected item's value. You will want that duringsetModelData()
.There are also methods to read any item's value or find an item with a specified value if you need them, e.g. during your
setEditorData()
. -
@JonB
I don't understand.
This is what the "Status" field looks when opening the table - it is with numbers:
I need to replace these numbers with the corresponding values from the array map.
How can i do this?@MyNick-0
Up until now you have been asking about editing and the combobox which goes with that. Now you are asking about a different situation, when the table is just showing the data.A
QStyledItemDelegate
handles the normal display as well as the editing. You could use QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale &locale) const to do your mapping from the numeric value to the string.Another possibility is to override the model's
data()
method to return the mapped string forDisplayRole
but the numeric value forEditRole
.Since you already have an editing delegate you might as well do it the first way.
-
I need to somehow create it in my "model" model.
-
@MyNick-0
Up until now you have been asking about editing and the combobox which goes with that. Now you are asking about a different situation, when the table is just showing the data.A
QStyledItemDelegate
handles the normal display as well as the editing. You could use QString QStyledItemDelegate::displayText(const QVariant &value, const QLocale &locale) const to do your mapping from the numeric value to the string.Another possibility is to override the model's
data()
method to return the mapped string forDisplayRole
but the numeric value forEditRole
.Since you already have an editing delegate you might as well do it the first way.
-