Qt Learning: View Selection (Qitemselectionmodel)

Source: Internet
Author: User
Tags foreach

Bo Master qq:1356438802


Select is a common operation in the view. In a list, tree, or table, you can select an item by mouse click, and the selected item will be highlighted or reversed. In Qt, selection also uses a model. In the Model/view architecture, this selection model provides a more general description of the selection operation. QT's built-in selection model is sufficient for general applications, but QT allows you to create your own selection model for special operations.

Qt uses the Qitemselectionmodel class to get the selection of items in the view. This model maintains an index of items and is independent of any view. This means that we can allow different views to share the same selection model and never achieve a synchronous operation. The selection is made up of the selection area. The model records only the starting and ending index positions of the selection, ensuring good performance for large constituencies. A discontinuous selection is made up of multiple contiguous selections.

The selection is applied directly above the selected indexes maintained by the selection model. The latest choice is the current selection. This means that even if no item is selected on the interface, it can also be useful if the selection is manipulated through certain commands.

In the view, there is always a current item and the selected item (even if there is no selection from the interface). Unlike the usual thought, the current item and the selection are two states that are independent of each other. A project can be either the current item or the selection. The following table is the difference between the current item and the selection:

Current Item Select Item
There can be only one current item. You can have multiple selections.
You can change the current item by using the keyboard or mouse click. The selection uses two states: Select And not selected, depending on the state before the project and some other settings, such as single or multiple selection. This state changes only when the user interacts.
The current item can be edited using F2 or a mouse double-click (provided the program allows it). The current item can be specified in conjunction with another anchor point to be selected or removed from the selected selection (or a combination of the two).
The current item usually has a focus frame to identify it. The selection is identified using the selection color.

When processing a selection, we can use Qitemselectionmodel as a record of the selection state of all the data items in the data model. Once the model is selected, the data items can be selected, deselected, or changed in the selected state without knowing which items are selected. The index of all selected items can be changed at any time, and other components can modify the selected information through the signal slot mechanism.


The Standard view classes (Qlistview, Qtreeview, and Qtableview) already provide a default selection model that is sufficient to meet the needs of most applications. The selection model for a view can be obtained through the Selectionmodel () function and then shared with other views using Setselectionmodel (), so it is generally not necessary to create a new selection model.

If you need to create a selection, we need to specify a model and a pair of indexes to create a Qitemselection object using this data. These two indexes should point to the data in the given model and are indexed as the upper-left and lower-right corners of a block selection. In order to apply a selection to a model, you need to submit the selection to the selection model. There are several implementations of this operation, which have different effects on the existing selection model.

Let's look at some code snippets. It is preferred to build a tabular model of a total of 32 data items and then set it to the data for a tabular view:

1 2 3 Qtablewidget Tablewidget (8, 4); Qitemselectionmodel *selectionmodel = Tablewidget.selectionmodel ();

At the end of the code, we get the Qtableview selection model for later use. Now, instead of modifying the data in the model, we select some cells in the upper-left corner of the table. Let's take a look at how the code is implemented:

1 2 Qmodelindex TopLeft = Tablewidget.model ()->index (0, 0, Qmodelindex ()); Qmodelindex bottomright = Tablewidget.model ()->index (5, 2, Qmodelindex ());

Next, we'll define the two indexes we've got as a selection. To achieve this, we first construct a Qitemselection object and then assign it to the selection model we get:

1 2 Qitemselection selection (TopLeft, bottomright); Selectionmodel->select (selection, qitemselectionmodel::select);

As we said earlier, first construct a Qitemselection object using the coordinates in the upper-left and lower-right corners, and then set this object to select the selection area of the model. The first argument to the Select () function is the selection, and the second parameter is the selection's flag bit. Qt offers a number of different operations and can refer to the following qitemselectionmodel::selectionflags documentation. In this example, we used qitemselectionmodel::select, which means that all the cells contained in the selection are selected.

Here's how we run the results:

Now we know how to set up a selection. Here's a look at how to get a selection. You need to use the selectedindexes () function to get a selection. The function returns an unordered list. We can get what is selected by traversing this list:

1 2 3 4 5 6 7 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 selection model emits a signal when the selection changes. We can connect the selectionchanged () signal to check which item has changed when the selection changes. This signal has two parameters: the first is the newly selected item, and the second is the item that has just been deselected. In the following example, we fill in the string with all newly selected items with the selectionchanged () signal, emptying all the deselected parts:

1 2 3 4 5 6 7 8 9 Ten-All-in-one Void mainwindow::updatesel Ection (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 (

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.