QT 4.5.2,QTableView不可编辑
QT 4.5.2, QTableView is not editable
我只是在我的表单中添加了一个新的 QTableView,设置了模型并进行了配置,但我无法编辑单元格。在其他 table 中编辑没有问题。我怎么了?代码:
void MethodicWorkWidget::setupMethod3Model() {
method3Model = new ExtSqlModel(this);
method3Model->setTable("methodworks3");
method3Model->setEditStrategy(QSqlTableModel::OnFieldChange);
int typeInd = method3Model->fieldIndex("typeid");
method3Model->setRelation(typeInd, QSqlRelation("methodworks3types", "id", "Name"));
method3Model->setFilter("teacherid="+QString::number(teacherID));//+" AND YearID="+QString::number(yearID));
method3Model->setHeaderData(typeInd, Qt::Horizontal, tr("Вид"));
method3Model->setHeaderData(method3Model->fieldIndex("discipline"), Qt::Horizontal, tr("Дисциплина"));
method3Model->setHeaderData(method3Model->fieldIndex("theme"), Qt::Horizontal, tr("Тема"));
method3Model->setHeaderData(method3Model->fieldIndex("date"), Qt::Horizontal, tr("Дата проведения"));
method3Model->setHeaderData(method3Model->fieldIndex("value"), Qt::Horizontal, tr("Балл"));
ui.label_2->setText(QString::number(teacherID));
method3Model->setSort(0, Qt::AscendingOrder);
//populate and check
if (!method3Model->select()) {
QMessageBox::critical(NULL, tr("Ошибка обращения к базе"), tr(
"Произошла ошибка при выборе руководства:\n")+manageModel->lastError().text());
}
ui.method3View->setModel(method3Model);
ui.method3View->hideColumn(method3Model->fieldIndex("id"));
ui.method3View->hideColumn(method3Model->fieldIndex("teacherid"));
ui.method3View->setItemDelegateForColumn(typeInd, new QSqlRelationalDelegate(ui.method3View));
ui.method3View->resizeColumnsToContents();
connect(ui.method3AddButton, SIGNAL(clicked()), this, SLOT(method3Add()));
connect(ui.method3DelButton, SIGNAL(clicked()), this, SLOT(method3Del()));
ui.method3DelButton->setEnabled(method3Model->rowCount()!=0);
if(UserInfo::role() == UserInfo::Guest){
ui.method3AddButton->setEnabled(false);
ui.method3DelButton->setEnabled(false);
ui.method3View->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
}
如果 QAbstractItemModel::flags
结果包含 Qt::ItemIsEditable
标志,则某个索引是可编辑的。默认情况下 QSqlRelationalTableModel
是可编辑的,但当不满足条件 !index.column() < realColNum-2
时,您可以覆盖 flags()
并禁用编辑。看来你在那种情况下有错误。请注意,!
运算符的优先级高于 <
。你可能想要 index.column() >= realColNum-2
。还要检查 realColNum
值。
我只是在我的表单中添加了一个新的 QTableView,设置了模型并进行了配置,但我无法编辑单元格。在其他 table 中编辑没有问题。我怎么了?代码:
void MethodicWorkWidget::setupMethod3Model() {
method3Model = new ExtSqlModel(this);
method3Model->setTable("methodworks3");
method3Model->setEditStrategy(QSqlTableModel::OnFieldChange);
int typeInd = method3Model->fieldIndex("typeid");
method3Model->setRelation(typeInd, QSqlRelation("methodworks3types", "id", "Name"));
method3Model->setFilter("teacherid="+QString::number(teacherID));//+" AND YearID="+QString::number(yearID));
method3Model->setHeaderData(typeInd, Qt::Horizontal, tr("Вид"));
method3Model->setHeaderData(method3Model->fieldIndex("discipline"), Qt::Horizontal, tr("Дисциплина"));
method3Model->setHeaderData(method3Model->fieldIndex("theme"), Qt::Horizontal, tr("Тема"));
method3Model->setHeaderData(method3Model->fieldIndex("date"), Qt::Horizontal, tr("Дата проведения"));
method3Model->setHeaderData(method3Model->fieldIndex("value"), Qt::Horizontal, tr("Балл"));
ui.label_2->setText(QString::number(teacherID));
method3Model->setSort(0, Qt::AscendingOrder);
//populate and check
if (!method3Model->select()) {
QMessageBox::critical(NULL, tr("Ошибка обращения к базе"), tr(
"Произошла ошибка при выборе руководства:\n")+manageModel->lastError().text());
}
ui.method3View->setModel(method3Model);
ui.method3View->hideColumn(method3Model->fieldIndex("id"));
ui.method3View->hideColumn(method3Model->fieldIndex("teacherid"));
ui.method3View->setItemDelegateForColumn(typeInd, new QSqlRelationalDelegate(ui.method3View));
ui.method3View->resizeColumnsToContents();
connect(ui.method3AddButton, SIGNAL(clicked()), this, SLOT(method3Add()));
connect(ui.method3DelButton, SIGNAL(clicked()), this, SLOT(method3Del()));
ui.method3DelButton->setEnabled(method3Model->rowCount()!=0);
if(UserInfo::role() == UserInfo::Guest){
ui.method3AddButton->setEnabled(false);
ui.method3DelButton->setEnabled(false);
ui.method3View->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
}
如果 QAbstractItemModel::flags
结果包含 Qt::ItemIsEditable
标志,则某个索引是可编辑的。默认情况下 QSqlRelationalTableModel
是可编辑的,但当不满足条件 !index.column() < realColNum-2
时,您可以覆盖 flags()
并禁用编辑。看来你在那种情况下有错误。请注意,!
运算符的优先级高于 <
。你可能想要 index.column() >= realColNum-2
。还要检查 realColNum
值。