gpt4 book ai didi

c++ - QT 4.5.2,QTableView不可编辑

转载 作者:行者123 更新时间:2023-11-28 06:14:04 25 4
gpt4 key购买 nike

我只是将一个新的 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默认情况下是可编辑的,但您可以覆盖 flags()并在条件 !index.column() < realColNum-2 时禁用编辑不满意。看来你在那种情况下有错误。注意 !运算符的优先级高于 < .你可能想要 index.column() >= realColNum-2 .还要检查 realColNum值(value)。

关于c++ - QT 4.5.2,QTableView不可编辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30671018/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com