Pyqt model/view framework 3. editable items

Source: Internet
Author: User
> [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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.