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
QTableView - not allow user to edit cell
提问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
根据你是在编码一切还是在设计器中做事,设置
editTriggers
toQAbstractItemView::NoEditTriggers
selectionBehavior
toQAbstractItemView::SelectRows
- optionally set
selectionMode
toQAbstractItemView::SingleSelection
if you want the user to select exactly one row
editTriggers
到QAbstractItemView::NoEditTriggers
selectionBehavior
到QAbstractItemView::SelectRows
- 如果您希望用户只选择一行
selectionMode
,QAbstractItemView::SingleSelection
则可以选择设置为
on the tableview object the appropriate calls will all be prefixed with set
e.g setEditTriggers()
in the Designer you can find these option in the AbstractItemView
section
在 tableview 对象上,适当的调用都将带有前缀,set
例如setEditTriggers()
在设计器中,您可以在AbstractItemView
部分中找到这些选项
回答by shoosh
Try this:
尝试这个:
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
回答by Rob
Toggle off the table item's ItemIsEditable
bit. 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 QItemDelegate
like 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