TextEdited set QTableView cell value
-
Hello. I have a little problem (I hope). A few days ago I added a QTableView array to my project which inherits from the QAbstractTableView. I added text placeholders. I would like that when I change a value in QLineEdit, it sets in a specific QTableView cell. I have created a summarymodel that contains everything relevant to my QTableView. Unfortunately I don't really know how to set the value from QLineEdit as a value in the table. I know it needs setData() and a dataChanged() signal.
QLineEdit
void QueriesCreator::on_quantityGeneralInformationLineEdit_textEdited(const QString &arg1) { calculations.SetMaterialQuantity(ui->quantityGeneralInformationLineEdit->text().toInt()); CalculateMaterialCostInZloty(); summaryModel->SetSummaryQuantity(calculations.GetMaterialQuantity()); qDebug()<<summaryModel->setData(ui->summaryDefaultTableView->model()->index(6,0),10,Qt::EditRole); qDebug()<<summaryModel->GetSummaryQuantity(); }
Model Settings
summaryModel = new SummaryModel(ui->summaryDefaultTableView); ui->summaryDefaultTableView->setModel(summaryModel); ui->summaryDefaultTableView->setEditTriggers(QAbstractItemView::NoEditTriggers); ui->summaryDefaultTableView->setSelectionMode(QAbstractItemView::SingleSelection); connect(summaryModel,SIGNAL(dataChanged(QModelIndex,QModelIndex)),this,SLOT(UpdateData(QModelIndex,QModelIndex)));
summarymodel.h
#ifndef SUMMARYMODEL_H #define SUMMARYMODEL_H #include <QAbstractTableModel> class SummaryModel : public QAbstractTableModel { Q_OBJECT public: explicit SummaryModel(QObject *parent = nullptr); // Header: QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; // Basic functionality: int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; bool setData(const QModelIndex &index, const QVariant &value, int role); void SetSummaryQuantity(const int&); int GetSummaryQuantity() const; private: const int rowQuantity = 8; const int columnQuantity = 5; int summaryQuantity = 0; }; #endif // SUMMARYMODEL_H
summarymodel.cpp
#include "summarymodel.h" SummaryModel::SummaryModel(QObject *parent) : QAbstractTableModel(parent) { } QVariant SummaryModel::headerData(int section, Qt::Orientation orientation, int role) const { if(role==Qt::DisplayRole) { if(orientation == Qt::Horizontal) { switch(section) { case 0: return QString("Ilość"); case 1: return QString("Waluta"); case 2: return QString("Obróbka"); case 3: return QString("Materiał"); case 4: return QString("Różnica"); default: return QVariant(); } } return QVariant(); } return QVariant(); } int SummaryModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return rowQuantity; } int SummaryModel::columnCount(const QModelIndex &parent) const { if (parent.isValid()) return 0; return columnQuantity; } QVariant SummaryModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) { //Quantity if (index.row() == 0 && index.column() == 0) return QString("50"); if (index.row() == 2 && index.column() == 0) return QString("100"); if (index.row() == 4 && index.column() == 0) return QString("1000"); if (index.row() == 6 && index.column() == 0) return QString("0"); //Currency- byc w moze w przyszlosci zmienic na inne if (index.row() == 0 && index.column() == 1) return QString("PLN"); if (index.row() == 1 && index.column() == 1) return QString("EUR"); if (index.row() == 2 && index.column() == 1) return QString("PLN"); if (index.row() == 3 && index.column() == 1) return QString("EUR"); if (index.row() == 4 && index.column() == 1) return QString("PLN"); if (index.row() == 5 && index.column() == 1) return QString("EUR"); if (index.row() == 6 && index.column() == 1) return QString("PLN"); if (index.row() == 7 && index.column() == 1) return QString("EUR"); //Price Placeholders //Material if (index.row() == 0 && index.column() == 2) return QString("0,00"); if (index.row() == 1 && index.column() == 2) return QString("0,00"); if (index.row() == 2 && index.column() == 2) return QString("0,00"); if (index.row() == 3 && index.column() == 2) return QString("0,00"); if (index.row() == 4 && index.column() == 2) return QString("0,00"); if (index.row() == 5 && index.column() == 2) return QString("0,00"); if (index.row() == 6 && index.column() == 2) return QString("0,00"); if (index.row() == 7 && index.column() == 2) return QString("0,00"); //Machining if (index.row() == 0 && index.column() == 3) return QString("0,00"); if (index.row() == 1 && index.column() == 3) return QString("0,00"); if (index.row() == 2 && index.column() == 3) return QString("0,00"); if (index.row() == 3 && index.column() == 3) return QString("0,00"); if (index.row() == 4 && index.column() == 3) return QString("0,00"); if (index.row() == 5 && index.column() == 3) return QString("0,00"); if (index.row() == 6 && index.column() == 3) return QString("0,00"); if (index.row() == 7 && index.column() == 3) return QString("0,00"); //Diference if (index.row() == 0 && index.column() == 4) return QString("0,00"); if (index.row() == 1 && index.column() == 4) return QString("0,00"); if (index.row() == 2 && index.column() == 4) return QString("0,00"); if (index.row() == 3 && index.column() == 4) return QString("0,00"); if (index.row() == 4 && index.column() == 4) return QString("0,00"); if (index.row() == 5 && index.column() == 4) return QString("0,00"); if (index.row() == 6 && index.column() == 4) return QString("0,00"); if (index.row() == 7 && index.column() == 4) return QString("0,00"); } if(role==Qt::TextAlignmentRole) { return Qt::AlignCenter; } return QVariant(); } bool SummaryModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { emit dataChanged(index,index); return true; } else { return false; } } void SummaryModel::SetSummaryQuantity( const int &quantity) { summaryQuantity = quantity; } int SummaryModel::GetSummaryQuantity() const { return summaryQuantity; }
I want the cell with index 6,0 to take the value from QLineEdit. I have no idea how to do this. Method setData() return true. When I connect signal and try it with qDebug() it works as I expect (code below)
SLOT (UpdateData())
void QueriesCreator::UpdateData(QModelIndex indexA, QModelIndex indexB) { qDebug()<<"Hello"; }
Any idea how to fix it?
Thanks and Have a great day!
-
Hi,
Your setData method does nothing. If you want to see changes, you need to store the data passed through setData and return that stored data in the data function.
-
@SGaist This is the solution! Thank you very much. Below is my new setData() and TextEdited()
setData()
bool SummaryModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::EditRole) { SetSummaryQuantity(value.toInt()); emit dataChanged(index,index); return true; } else { return false; } return false; }
TextEdited()
void QueriesCreator::on_quantityGeneralInformationLineEdit_textEdited(const QString &arg1) { calculations.SetMaterialQuantity(ui->quantityGeneralInformationLineEdit->text().toInt()); CalculateMaterialCostInZloty(); summaryModel->setData(ui->summaryDefaultTableView->model()->index(6,0),ui->quantityGeneralInformationLineEdit->text().toInt(),Qt::EditRole); }
and of course the data()
QVariant SummaryModel::data(const QModelIndex &index, int role) const { if (role == Qt::DisplayRole) { //Quantity if (index.row() == 0 && index.column() == 0) return QString("50"); if (index.row() == 2 && index.column() == 0) return QString("100"); if (index.row() == 4 && index.column() == 0) return QString("1000"); if (index.row() == 6 && index.column() == 0) return GetSummaryQuantity(); //Currency- byc w moze w przyszlosci zmienic na inne if (index.row() == 0 && index.column() == 1) return QString("PLN"); if (index.row() == 1 && index.column() == 1) return QString("EUR"); if (index.row() == 2 && index.column() == 1) return QString("PLN"); if (index.row() == 3 && index.column() == 1) return QString("EUR"); if (index.row() == 4 && index.column() == 1) return QString("PLN"); if (index.row() == 5 && index.column() == 1) return QString("EUR"); if (index.row() == 6 && index.column() == 1) return QString("PLN"); if (index.row() == 7 && index.column() == 1) return QString("EUR"); //Price Placeholders //Material if (index.row() == 0 && index.column() == 2) return QString("0,00"); if (index.row() == 1 && index.column() == 2) return QString("0,00"); if (index.row() == 2 && index.column() == 2) return QString("0,00"); if (index.row() == 3 && index.column() == 2) return QString("0,00"); if (index.row() == 4 && index.column() == 2) return QString("0,00"); if (index.row() == 5 && index.column() == 2) return QString("0,00"); if (index.row() == 6 && index.column() == 2) return QString("0,00"); if (index.row() == 7 && index.column() == 2) return QString("0,00"); //Machining if (index.row() == 0 && index.column() == 3) return QString("0,00"); if (index.row() == 1 && index.column() == 3) return QString("0,00"); if (index.row() == 2 && index.column() == 3) return QString("0,00"); if (index.row() == 3 && index.column() == 3) return QString("0,00"); if (index.row() == 4 && index.column() == 3) return QString("0,00"); if (index.row() == 5 && index.column() == 3) return QString("0,00"); if (index.row() == 6 && index.column() == 3) return QString("0,00"); if (index.row() == 7 && index.column() == 3) return QString("0,00"); //Diference if (index.row() == 0 && index.column() == 4) return QString("0,00"); if (index.row() == 1 && index.column() == 4) return QString("0,00"); if (index.row() == 2 && index.column() == 4) return QString("0,00"); if (index.row() == 3 && index.column() == 4) return QString("0,00"); if (index.row() == 4 && index.column() == 4) return QString("0,00"); if (index.row() == 5 && index.column() == 4) return QString("0,00"); if (index.row() == 6 && index.column() == 4) return QString("0,00"); if (index.row() == 7 && index.column() == 4) return QString("0,00"); } if(role==Qt::TextAlignmentRole) { return Qt::AlignCenter; } return QVariant(); }
Now It works as I expect. Thanks and Have a good day!
-