Qt
::ItemFlags EditableSqlModel
::flags(const QModelIndex &index
) const {
if (index.column() == 1 || index.column() == 2 || index.column() == 3 || index.column() == 4 || index.column() == 5)
flags |= Qt::ItemIsEditable;
return flags;
}
bool EditableSqlModel
::setData(const QModelIndex &index,
const QVariant &value,
int /* role */) {
if (index.column() < 1 || index.column() > 5)
return false;
int id = data(primaryKeyIndex).toInt();
clear();
bool ok=FALSE;
if (index.column() == 1) ok = setName(id, value.toString());
if (index.column() == 2) ok = setCountry(id, value.toString());
if (index.column() == 3) ok = setTotal(id, value.toDouble());
if (index.column() == 4) ok = setMachine(id, value.toString());
if (index.column() == 5) ok = setOs_vendor(id, value.toString());
refresh();
return ok;
}
void EditableSqlModel::refresh()
{
setQuery("SELECT host.id, user.name, user.country, host.total, host.machine, host.os_vendor FROM host left join user on host.id = user.id where user.name IS NOT NULL order by host.total");
}
bool EditableSqlModel
::setName(int id,
const QString &name
) {
query.prepare("update user set name = ? where id = ?");
query.addBindValue(name);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel
::setCountry(int id,
const QString &Country
) {
query.prepare("update user set country = ? where id = ?");
query.addBindValue(Country);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel::setTotal(int id, const double &Total)
{
query.prepare("update host set total = ? where id = ?");
query.addBindValue(Total);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel
::setMachine(int id,
const QString &Machine
) {
query.prepare("update host set machine = ? where id = ?");
query.addBindValue(Machine);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel
::setOs_vendor(int id,
const QString &Os_vendor
) {
query.prepare("update host set os_vedor = ? where id = ?");
query.addBindValue(Os_vendor);
query.addBindValue(id);
return query.exec();
}
Qt::ItemFlags EditableSqlModel::flags(const QModelIndex &index) const
{
Qt::ItemFlags flags = QSqlQueryModel::flags(index);
if (index.column() == 1 || index.column() == 2 || index.column() == 3 || index.column() == 4 || index.column() == 5)
flags |= Qt::ItemIsEditable;
return flags;
}
bool EditableSqlModel::setData(const QModelIndex &index, const QVariant &value, int /* role */)
{
if (index.column() < 1 || index.column() > 5)
return false;
QModelIndex primaryKeyIndex = QSqlQueryModel::index(index.row(), 0);
int id = data(primaryKeyIndex).toInt();
clear();
bool ok=FALSE;
if (index.column() == 1) ok = setName(id, value.toString());
if (index.column() == 2) ok = setCountry(id, value.toString());
if (index.column() == 3) ok = setTotal(id, value.toDouble());
if (index.column() == 4) ok = setMachine(id, value.toString());
if (index.column() == 5) ok = setOs_vendor(id, value.toString());
refresh();
return ok;
}
void EditableSqlModel::refresh()
{
setQuery("SELECT host.id, user.name, user.country, host.total, host.machine, host.os_vendor FROM host left join user on host.id = user.id where user.name IS NOT NULL order by host.total");
}
bool EditableSqlModel::setName(int id, const QString &name)
{
QSqlQuery query;
query.prepare("update user set name = ? where id = ?");
query.addBindValue(name);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel::setCountry(int id, const QString &Country)
{
QSqlQuery query;
query.prepare("update user set country = ? where id = ?");
query.addBindValue(Country);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel::setTotal(int id, const double &Total)
{
QSqlQuery query;
query.prepare("update host set total = ? where id = ?");
query.addBindValue(Total);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel::setMachine(int id, const QString &Machine)
{
QSqlQuery query;
query.prepare("update host set machine = ? where id = ?");
query.addBindValue(Machine);
query.addBindValue(id);
return query.exec();
}
bool EditableSqlModel::setOs_vendor(int id, const QString &Os_vendor)
{
QSqlQuery query;
query.prepare("update host set os_vedor = ? where id = ?");
query.addBindValue(Os_vendor);
query.addBindValue(id);
return query.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks