C++ QTableView - 不允许用户编辑单元格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1328492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:34:08  来源:igfitidea点击:

QTableView - not allow user to edit cell

c++qtqt4qtableview

提问by Berschi

I created a QTableView with a QSqlTableModel. By standard, double-clicking on the cells will mark them and the user can edit them. I want, that the user isn't allowed to do that. He is allowed to mark the whole row by clicking on a single cell, but not to edit the cell. How can I do that?

我用 QSqlTableModel 创建了一个 QTableView。按照标准,双击单元格将标记它们并且用户可以编辑它们。我想要,不允许用户这样做。他可以通过单击单个单元格来标记整行,但不能编辑该单元格。我怎样才能做到这一点?

回答by Harald Scheirich

Depending on whether you are coding everything or doing things in the designer, set

根据你是在编码一切还是在设计器中做事,设置

  • editTriggersto QAbstractItemView::NoEditTriggers
  • selectionBehaviorto QAbstractItemView::SelectRows
  • optionally set selectionModeto QAbstractItemView::SingleSelectionif you want the user to select exactly one row
  • editTriggersQAbstractItemView::NoEditTriggers
  • selectionBehaviorQAbstractItemView::SelectRows
  • 如果您希望用户只选择一行selectionModeQAbstractItemView::SingleSelection则可以选择设置为

on the tableview object the appropriate calls will all be prefixed with sete.g setEditTriggers()in the Designer you can find these option in the AbstractItemViewsection

在 tableview 对象上,适当的调用都将带有前缀,set例如setEditTriggers()在设计器中,您可以在AbstractItemView部分中找到这些选项

回答by shoosh

Try this:

尝试这个:

table->setEditTriggers(QAbstractItemView::NoEditTriggers);

回答by Rob

Toggle off the table item's ItemIsEditablebit. e.g.:

关闭表格项目的ItemIsEditable位。例如:

QTableWidgetItem* item = new QTableWidgetItem(...);
...
item->setFlags(item->flags() &= ~Qt::ItemIsEditable);

回答by Adam W

Ideally you will want to use:

理想情况下,您将希望使用:

void QAbstractItemView::setItemDelegate ( QAbstractItemDelegate * delegate )

And then create a class that inherits from QItemDelegatelike in thisexample. Editing your class to have

然后创建一个类继承自QItemDelegate这样的例子。编辑你的班级

QWidget * QItemDelegate::createEditor ( QWidget * parent, const QStyleOptionViewItem & option, const QModelIndex & index ) const  

return NULL

返回 NULL

or use:

或使用:

table->setEditTriggers(QAbstractItemView::NoEditTriggers);

You will also want to look at

你还想看看

void setSelectionBehavior ( QAbstractItemView::SelectionBehavior behavior )

With the parameter: QAbstractItemView::SelectRows

带参数: QAbstractItemView::SelectRows

For reference: http://doc.trolltech.com/4.5/qtableview.html

供参考:http: //doc.trolltech.com/4.5/qtableview.html