Solved How in QSqlTableModel change all value in the column?
-
Hi!
How in QSqlTableModel change all value in the column?
I could change the values by index, buttableModel->rowCount();
return 256, whereas I have 86,000 values.
-
@Mikeeeeee Do you know the search function?
-
while (tableModel->canFetchMore()) tableModel->fetchMore();
this is how I get my 86,000 rows.
But how do I replace all the cells? This only changes the first cell.for (int i = 0; i < 10; i++) { index = tableModel->index(i, 0); tableModel->setData(index, "sdaf"); }
-
I tried to make my own model, with 3 roles, but when updating the model, the kernel requests roles from 0 to 10. What might be the bug?
-
@Mikeeeeee
Your questions jump all over the place. You ask about 256/86,000 rows. Now you are asking about "roles", and "the kernel requests roles from 0 to 10.". And "I tried to make my own model, with 3 roles,".What are you doing with roles, what do you actually want to set?
tableModel->setData(index, "sdaf");
is setting theQt::EditRole
(i.e. just what you think of as the "value" of the data). What else are actually trying to achieve? Please explain in intelligible language, we can't guess. -
it's return:
for (int i = 0; i < 10; i++) { //qDebug()<<i; index = tableModel->index(i, 0); qDebug()<<index; //tableModel->setData(index, "sdaf"); tableModel->setData(index, QDateTime::fromTime_t(tableModel->data(index).toLongLong()).toString("dd-MM-yyyy hh:mm:ss")); qDebug()<<tableModel->data(index); }
QModelIndex(0,0,0x0,QSqlTableModel(0x367f70)) QVariant(QString, "01-01-1970 03:00:00") QModelIndex(1,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820738) QModelIndex(2,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820739) QModelIndex(3,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820740) QModelIndex(4,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820741) QModelIndex(5,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820742) QModelIndex(6,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820743) QModelIndex(7,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820744) QModelIndex(8,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820745) QModelIndex(9,0,0x0,QSqlTableModel(0x367f70)) QVariant(qlonglong, 1564820746)
Why changes only first?
-
If I do this in the model, I get strange squares. What should I do with these squares? How do I remove squares?
QVariant MyModel::data(const QModelIndex & index, int role) const { return QVariant(QString("row = ") + QString::number(index.row()) + " col = " + QString::number(index.column())); }
-
it is work
#include "mymodel.h" MyModel::MyModel(QObject *parent) : QAbstractTableModel(parent) { } QVariant MyModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { switch (section) { case 0: return "Время"; case 1: return "Значение"; case 2: return "Группа"; default: return QVariant(); } } return QAbstractTableModel::headerData(section, orientation, role); } void MyModel::addRow(QString str1, long long val2, int val3) { beginInsertRows( QModelIndex(), 0, 2 ); dateTime.append(str1); value.append(val2); number.append(val3); endInsertRows(); } int MyModel::columnCount(const QModelIndex &parent) const { return 3; } int MyModel::rowCount(const QModelIndex & parent) const { Q_UNUSED(parent); return dateTime.count(); } QVariant MyModel::data(const QModelIndex & index, int role) const { if (role == Qt::DisplayRole) { if(index.column() == 0){ return QVariant(dateTime[index.row()]); } if(index.column() == 1){ return QVariant(value[index.row()]); } if(index.column() == 2){ return QVariant(number[index.row()]); } } return QVariant(); } QHash<int, QByteArray> MyModel::roleNames() const { QHash<int, QByteArray> roles; roles[Role1] = "role1"; roles[Role2] = "role2"; roles[Role3] = "role3"; return roles; } void MyModel::updateMyModel(QString nameTable) { dateTime.clear(); dateSecs.clear(); value.clear(); number.clear(); QSqlQuery query; query.prepare("SELECT " "Time," "Value," "Number" " FROM " + nameTable + ";"); if (!query.exec()) {qDebug()<<"не получило данные";} else { qDebug()<<"получило данные"; query.first(); dateTime.append(QDateTime::fromTime_t(query.value(0).toLongLong()).toString("dd-MM-yyyy hh:mm:ss")); dateSecs.append(query.value(0).toLongLong()); value.append(query.value(1).toDouble()); number.append(query.value(2).toInt()); while (query.next()) { dateTime.append(QDateTime::fromTime_t(query.value(0).toLongLong()).toString("dd-MM-yyyy hh:mm:ss")); dateSecs.append(query.value(0).toLongLong()); value.append(query.value(1).toDouble()); number.append(query.value(2).toInt()); } } } void MyModel::startDb(QString pathTable) { myDatabase = QSqlDatabase::addDatabase("QSQLITE"); myDatabase.setDatabaseName(pathTable); if (myDatabase.open()) {qDebug()<<"db is opened";} else {qDebug()<<"db not opened";} }