> [Part 1] (http://www.cnblogs.com/hangxin1940/archive/2012/12/07/2806449.html) describes how to customize rendering view items, through which you will learn how to edit view unit items
Editable unit items
---
In the [previous] (http://www.cnblogs.com/hangxin1940/archive/2012/12/07/2806449.html) completed Code, add the method for the 'mylistmodel' class:
Def flags (self, index ):
"""
Flag describes the status information of data items in the view.
"""
# First obtain the flags return value of the superclass
Flag = super (mylistmodel, self). Flags (INDEX)
# Or operation: overlay the itemiseditable (editable) mark
Return flag | QT. itemiseditable
Now, run the complete code and ** double-click * The unit item to edit it.
At this point, we can see that, although it can be edited, after double-clicking, the editing area is blank and there is no effect after the editing is complete.
First, we solve the problem of data display during editing. Add the following code to the 'mylistmodel'-> 'data' method, that is, add a judgment condition:
# The current role is in editing mode and the original data is displayed.
# In this way, when we double-click a unit item, nothing will be displayed
If role = QT. editrole:
Return self. _ DATA [row]
Then, to save the data, we add the following method for the 'mylistmodel' class:
Def setdata (self, index, value, role = QT. editrole ):
"""
Set Data
"""
# If the current role is an edit role
If role = QT. editrole:
# In this qvariant method, the returned bool type indicates whether the value can be converted to the int type.
Value_int, OK = value. toint ()
# Convert to int type
If OK:
# Save data
Self. _ DATA [index. Row ()] = value_int
# Transmit data change signals to update the view
Self. datachanged. emit (index, index)
Return true
# Whether data is successfully updated
Return false
Now, run the code to edit and update the data.
The progress bar changes based on the input value.
Model processing process
---
So far, we can write a model for read-only, editable, and custom unit items. The following is the processing process.
Model. Flags model item editable
|
| Create an editor
|
Model. Data returns the data in the editing status.
|
| Set data in the editor
|
Model. setdata is edited and the data is saved.
> Through this article, we can write editable models, [next] (http://www.cnblogs.com/hangxin1940/archive/2012/12/07/2806453.html), we will learn how to delegate the rendering mode of the model with delegate