отображается все верно, но при нажитии на сохранить данные не сохраняются : - (
class MyView: public QTableView {
Q_OBJECT
public:
MyView(QWidget * parent = 0);
public slots:
void saveData();
};
class MyWidget: public QWidget {
Q_OBJECT
QSqlRelationalTableModel *model;
MyView *view;
public:
MyWidget(QWidget *parent = 0);
};
MyWidget::MyWidget(QWidget *parent): QWidget(parent){
QVBoxLayout * layout = new QVBoxLayout;
setLayout(layout);
model = new QSqlRelationalTableModel(this);
model->setTable("testtable");
model->setRelation(0, QSqlRelation("dict", "dict_id", "name"));
model->select();
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
view = new MyView;
view->setModel(model);
view->setItemDelegate(new QSqlRelationalDelegate(view));
layout->addWidget(view,5);
QPushButton *bSave = new QPushButton(QObject::tr("&Сохранить"));
QObject::connect(bSave,SIGNAL(clicked()),view,SLOT(saveData()));
layout->addWidget(bSave,1);
}
MyView::MyView(QWidget *parent): QTableView(parent){}
void MyView::saveData(){
if(((QSqlTableModel *) model())->submitAll()) qDebug() << "ok" ;
}
база вот такая
CREATE TABLE "dict" (
"dict_id" integer NOT NULL PRIMARY KEY AUTOINCREMENT,
"name" varchar(20) NOT NULL
);
CREATE TABLE "testtable" (
"dict_id" integer,
"value" real NOT NULL,
FOREIGN KEY(dict_id) REFERENCES dict
);
INSERT INTO dict ("name") VALUES ("A");
INSERT INTO dict ("name") VALUES ("B");
INSERT INTO testtable VALUES ("1",2.2);
INSERT INTO testtable VALUES ("2",6.6);