![](/img/trans.png)
[英]Qt - How can I make a particular Column of my QTableView as Non Editable?
[英]QT 4.5.2, QTableView is not editable
我只是在表单中添加了一个新的QTableView,设置了模型并对其进行配置,但是我无法编辑单元格。 在其他表中,编辑没有问题。 我怎么了 码:
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
条件!index.column() < realColNum-2
时,您将覆盖flags()
并禁用编辑。 看来您在这种情况下有错误。 注意!
运算符的优先级高于<
。 您可能需要index.column() >= realColNum-2
。 还要检查realColNum
值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.