Qt model/view Study Notes (6)

Source: Internet
Author: User
Select data items in views

Concept

The Selection Model Used in the New View class has greatly improved compared with the model in qt3. It provides a more comprehensive description of the model/View-based architecture. Although the standard classes used to manipulate the selected options are sufficient for the provided views, you can also create a specific selection model to meet your special needs.
Information about the selected data items in the view is kept in the qitemselectionmodel
. It also maintains the model indexes information for each data item in an independent model and is associated with any views. Since a model can be used for multiple views, sharing and selecting information among multiple views is also possible, so that multiple views can be displayed in a consistent manner.
The selection consists of multiple selection ranges. By recording only the start model indexes and the end model indexes, You can maximize the record of the available range. Non-continuous selection data items are described by multiple selection ranges. Select a set of model record model indexes to describe a selection. The recently selected data item is called Current
Selection
. An application can use a certain type of selection command to modify the selection effect.
During the selection operation, you can set qitemselectionmodel
View
Is a record of the Selection status of all data items in the model. Once a selection model is created, all items can be selected in the Set, unselect, or select the status to switch without knowing which data item
Whether it has been selected. Indexes of all selected items can be obtained at any time. The signal slot mechanism can be used to notify other components of changes.

Use select model

The standard view class provides default selection models that can be used in a large number of programs. You can call the selectionmodel function of a view to select a model.
(). You can also use setselectionmodel.
() Share the selected model among multiple views. Therefore, building a new model is generally not necessary.
By giving the qitemselection
Specify a model and a pair of model indexes. You can create an option. The indexes usage depends on the given model. The two indexes are interpreted as the items in the upper left corner and lower right corner of the selected block. The selection of items in the model is subject to the Selection Model.

Select item

Build a table model with 32 items and display it with one table View:
Tablemodel
* Model = new tablemodel (8, 4, & App );

Qtableview
* Table = new qtableview (0 );
Table-> setmodel
(Model );

Qitemselectionmodel
* Selectionmodel = table-> selectionmodel
();
Qmodelindex
Topleft;
Qmodelindex
Bottomright;

Topleft = model-> index (0, 0, qmodelindex ());
Bottomright = model-> index (5, 2, qmodelindex ());

Qitemselection
Selection (topleft, bottomright );
Selectionmodel-> select
(Selection, qitemselectionmodel: select
);
The result is as follows:

Read selection status

Stored in the selected model, indexes can be read using the selectionindexes () function. It returns an unordered list of model indexes. We can traverse it if we know which model they are associated.
Qmodelindexlist
Indexes = selectionmodel-> selectedindexes
();
Qmodelindex
Index;

Foreach
(Index, indexes ){
Qstring text = qstring ("(% 1, % 2)"). Arg (index. Row (). Arg (index. Column ());
Model-> setdata (index, text );
}
The selected model sends a signal when the selection changes. This is used to notify other components including changes to the overall and current focus items. We can connect the selectionchanged () signal to a slot to check which items are selected or deselected when the signal is generated. This slot is called with two parameters, both of which are qitemselection.
Object, one containing the newly selected items, and the other containing the recently deselected items. The following code shows how to add data content to a newly selected item. The content of the recently deselected item is cleared.
Void mainwindow: updateselection
(Const qitemselection
& Selected
,
Const qitemselection
& Deselected
)
{
Qmodelindex
Index;
Qmodelindexlist
Items = selected. Indexes
();

Foreach
(Index, items ){
Qstring text = qstring ("(% 1, % 2)"). Arg (index. Row (). Arg (index. Column ());
Model-> setdata (index, text );
}

Items = deselected. Indexes
();

Foreach
(Index, items)
Model-> setdata (index ,"");
}
You can also track the current focus item by responding to the currentchanged () signal. The corresponding slot has two receiving parameters, one representing the previous focus, and the other representing the current focus.
Void mainwindow: changecurrent
(Const qmodelindex
& Current,
Const qmodelindex
& Previous)
{
Statusbar ()-> showmessage (
TR ("moved from (% 1, % 2) to (% 3, % 4 )")
. Arg (Previous. Row (). Arg (Previous. Column ())
. Arg (current. Row (). Arg (current. Column ()));
}

Update Selection

The selection command is provided by the selection flag, which is
Defined in qitemselectionmodel: selectionflag. Commonly used include the select tag, toggle tag, and deselect tag
Mark, current mark, and clear mark. Run the following code along the result of the preceding example:
Qitemselection
Toggleselection;

Topleft = model-> index (2, 1, qmodelindex ());
Bottomright = model-> index (7, 3, qmodelindex ());
Toggleselection. Select
(Topleft, bottomright );

Selectionmodel-> select
(Toggleselection, qitemselectionmodel: toggle
);
The result is as follows:

Missing
In the case of saving, select commands only for a single item (by the Model
Specified by indexes ). However, the selection command can be combined with other tags to change the entire row and the entire column. For example, if you use only one index to call select (),
If the select tag and rows tag are combined, the entire row including that item will be selected. See the following example:
Qitemselection
Columnselection;

Topleft = model-> index (0, 1, qmodelindex ());
Bottomright = model-> index (0, 2, qmodelindex ());

Columnselection. Select
(Topleft, bottomright );

Selectionmodel-> select (columnselection,
Qitemselectionmodel
: Select | qitemselectionmodel
: Columns
);

Qitemselection
Rowselection;

Topleft = model-> index (0, 0, qmodelindex ());
Bottomright = model-> index (1, 0, qmodelindex ());

Rowselection. Select
(Topleft, bottomright );

Selectionmodel-> select (rowselection,
Qitemselectionmodel
: Select | qitemselectionmodel
: Rows
);
The result is as follows:

Select all items in the Model

To select all items in the model, you must first create a selection that includes all items at the current level:
Qmodelindex
Topleft = model-> index (0, 0, parent );
Qmodelindex
Bottomright = model-> Index
(Model-> rowcount
(Parent)-1,
Model-> columncount
(Parent)-1, parent );

Qitemselection
Selection (topleft, bottomright );
Selectionmodel-> select
(Selection, qitemselectionmodel: Select );
The top-level index can be as follows:
Qmodelindex parent = qmodelindex
();
Haschildren can be used for a hierarchical model.
() Function to determine whether a given item is the parent of another item.

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.