#include <QtGui>
#include <QtSql>
#include <QDebug>
#include <QTableView>
#include <QApplication>
Q_OBJECT
public:
}
Qt
::ItemFlags flags
( const QModelIndex & index
) const { if (index.column() == 2)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable ;
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
if (role == Qt::CheckStateRole && index.column() == 2) {
const QModelIndex sibling
= index.
sibling(index.
row(),
1);
return sibling.data(Qt::EditRole);
}
else
}
bool setData(const QModelIndex& index,const QVariant& value,int role) {
if (role == Qt::CheckStateRole && index.column() == 2) {
const QModelIndex sibling
= index.
sibling(index.
row(),
1);
bool ret = setData(sibling, value, Qt::EditRole);
if (ret) emit dataChanged(index, index);
qDebug() << "Success:"<< ret << "Value:" << value;
return ret;
}
else
}
};
int main(int argc, char *argv[]) {
db.setDatabaseName(":memory:");
db.open();
query.exec("create table test ( col0 int, col1 int, col2 text )");
query.exec("insert into test values(101, 2, 'Row 0')");
query.exec("insert into test values(102, 2, 'Row 1')");
query.exec("insert into test values(103, 0, 'Row 2')");
query.exec("insert into test values(104, 0, 'Row 3')");
query.exec("insert into test values(105, 2, 'Row 4')");
MyModel model;
model.setTable("test");
model.select();
view.setModel(&model);
view.show();
return app.exec();
}
#include "main.moc"
#include <QtGui>
#include <QtSql>
#include <QDebug>
#include <QTableView>
#include <QApplication>
class MyModel: public QSqlTableModel {
Q_OBJECT
public:
explicit MyModel(QObject *p = 0): QSqlTableModel(p) {
}
Qt::ItemFlags flags ( const QModelIndex & index ) const {
if (index.column() == 2)
return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable ;
else
return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
}
QVariant data(const QModelIndex &index, int role) const {
if (role == Qt::CheckStateRole && index.column() == 2) {
const QModelIndex sibling = index.sibling(index.row(), 1);
return sibling.data(Qt::EditRole);
}
else
return QSqlTableModel::data(index, role);
}
bool setData(const QModelIndex& index,const QVariant& value,int role) {
if (role == Qt::CheckStateRole && index.column() == 2) {
const QModelIndex sibling = index.sibling(index.row(), 1);
bool ret = setData(sibling, value, Qt::EditRole);
if (ret) emit dataChanged(index, index);
qDebug() << "Success:"<< ret << "Value:" << value;
return ret;
}
else
return QSqlTableModel::setData(index, value, role);
}
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
db.open();
QSqlQuery query;
query.exec("create table test ( col0 int, col1 int, col2 text )");
query.exec("insert into test values(101, 2, 'Row 0')");
query.exec("insert into test values(102, 2, 'Row 1')");
query.exec("insert into test values(103, 0, 'Row 2')");
query.exec("insert into test values(104, 0, 'Row 3')");
query.exec("insert into test values(105, 2, 'Row 4')");
MyModel model;
model.setTable("test");
model.select();
QTableView view;
view.setModel(&model);
view.show();
return app.exec();
}
#include "main.moc"
To copy to clipboard, switch view to plain text mode
Success: false Value: QVariant(int, 2)
Success: false Value: QVariant(int, 2)
Success: false Value: QVariant(int, 2)
Success: false Value: QVariant(int, 2)
Success: false Value: QVariant(int, 0)
Success: false Value: QVariant(int, 0)
Success: false Value: QVariant(int, 0)
Success: false Value: QVariant(int, 0)
Success: false Value: QVariant(int, 0)
Success: false Value: QVariant(int, 0)
Success: false Value: QVariant(int, 0)
To copy to clipboard, switch view to plain text mode
Bookmarks