A TQIconView can display and manage a grid or other 2D layout of labelled icons. Each labelled icon is a TQIconViewItem. Items (TQIconViewItems) can be added or deleted at any time; items can be moved within the TQIconView. Single or multiple items can be selected. Items can be renamed in-place. TQIconView also supports drag and drop.
Each item contains a label string, a pixmap or picture (the icon itself) and optionally a sort key. The sort key is used for sorting the items and defaults to the label string. The label string can be displayed below or to the right of the icon (see ItemTextPos).
The simplest way to create a TQIconView is to create a TQIconView object and create some TQIconViewItems with the TQIconView as their parent, set the icon view's geometry and show it. For example:
When an item is inserted the TQIconView allocates a position for it. Existing items are rearranged if autoArrange() is TRUE. The default arrangement is LeftToRight -- the TQIconView fills up the \fIleft-most\fR column from top to bottom, then moves one column \fIright\fR and fills that from top to bottom and so on. The arrangement can be modified with any of the following approaches:
Call setArrangement(), e.g. with TopToBottom which will fill the \fItop-most\fR row from left to right, then moves one row \fIdown\fR and fills that row from left to right and so on.
The spacing between items is set with setSpacing(). Items can be laid out using a fixed grid using setGridX() and setGridY(); by default the TQIconView calculates a grid dynamically. The position of items' label text is set with setItemTextPos(). The text's background can be set with setItemTextBackground(). The maximum width of an item and of its text are set with setMaxItemWidth() and setMaxItemTextLength(). The label text will be word-wrapped if it is too long; this is controlled by setWordWrapIconText(). If the label text is truncated, the user can still see the entire text in a tool tip if they hover the mouse over the item. This is controlled with setShowToolTips().
Items which are selectable may be selected depending on the SelectionMode; the default is Single. Because TQIconView offers multiple selection it must display keyboard focus and selection state separately. Therefore there are functions to set the selection state of an item (setSelected()) and to select which item displays keyboard focus (setCurrentItem()). When multiple items may be selected the icon view provides a rubberband, too.
When in-place renaming is enabled (it is disabled by default), the user may change the item's label. They do this by selecting the item (single clicking it or navigating to it with the arrow keys), then single clicking it (or pressing F2), and entering their text. If no key has been set with TQIconViewItem::setKey() the new text will also serve as the key. (See TQIconViewItem::setRenameEnabled().)
Because the internal structure used to store the icon view items is linear, no iterator class is needed to iterate over all the items. Instead we iterate by getting the first item from the \fIicon view\fR and then each subsequent (TQIconViewItem::nextItem()) from each \fIitem\fR in turn:
TQIconView also provides currentItem(). You can search for an item using findItem() (searching by position or for label text) and with findFirstVisibleItem() and findLastVisibleItem(). The number of items is returned by count(). An item can be removed from an icon view using takeItem(); to delete an item use \fCdelete\fR. All the items can be deleted with clear().
TQIconView supports the drag and drop of items within the TQIconView itself. It also supports the drag and drop of items out of or into the TQIconView and drag and drop onto items themselves. The drag and drop of items outside the TQIconView can be achieved in a simple way with basic functionality, or in a more sophisticated way which provides more power and control.
In this example we create a TQTextDrag object, (derived from TQDragObject), containing the item's label and return it as the drag object. We could just as easily have created a TQImageDrag from the item's pixmap and returned that instead.
TQIconViews and their TQIconViewItems can also be the targets of drag and drops. To make the TQIconView itself able to accept drops connect to the dropped() signal. When a drop occurs this signal will be emitted with a TQDragEvent and a TQValueList of TQIconDragItems. To make a TQIconViewItem into a drop target subclass TQIconViewItem and reimplement TQIconViewItem::acceptDrop() and TQIconViewItem::dropped().
See iconview/simple_dd/main.h and iconview/simple_dd/main.cpp for a simple drag and drop example which demonstrates drag and drop between a TQIconView and a QListBox.
The first part is starting drags -- you should use a TQIconDrag (or a class derived from it) for the drag object. In dragObject() create the drag object, populate it with TQIconDragItems and return it. Normally such a drag should offer each selected item's data. So in dragObject() you should iterate over all the items, and create a TQIconDragItem for each selected item, and append these items with TQIconDrag::append() to the TQIconDrag object. You can use TQIconDragItem::setData() to set the data of each item that should be dragged. If you want to offer the data in additional mime-types, it's best to use a class derived from TQIconDrag, which implements additional encoding and decoding functions.
When a drag enters the icon view, there is little to do. Simply connect to the dropped() signal and reimplement TQIconViewItem::acceptDrop() and TQIconViewItem::dropped(). If you've used a TQIconDrag (or a subclass of it) the second argument to the dropped signal contains a TQValueList of TQIconDragItems -- you can access their data by calling TQIconDragItem::data() on each one.
See also TQIconViewItem::setDragEnabled(), TQIconViewItem::setDropEnabled(), TQIconViewItem::acceptDrop(), TQIconViewItem::dropped(), and Advanced Widgets.
\fCTQIconView::Single\fR - When the user selects an item, any already-selected item becomes unselected and the user cannot unselect the selected item. This means that the user can never clear the selection. (The application programmer can, using TQIconView::clearSelection().)
\fCTQIconView::Multi\fR - When the user selects an item, e.g. by navigating to it with the keyboard arrow keys or by clicking it, the selection status of that item is toggled and the other items are left alone. Also, multiple items can be selected by dragging the mouse while the left mouse button stays pressed.
\fCTQIconView::Extended\fR - When the user selects an item the selection is cleared and the new item selected. However, if the user presses the Ctrl key when clicking on an item, the clicked item gets toggled and all other items are left untouched. If the user presses the Shift key while clicking on an item, all items between the current item and the clicked item get selected or unselected, depending on the state of the clicked item. Also, multiple items can be selected by dragging the mouse while the left mouse button stays pressed.
To summarise: Single is a real single-selection icon view; Multi a real multi-selection icon view; Extended is an icon view in which users can select multiple items but usually want to select either just one or a range of contiguous items; and NoSelection mode is for an icon view where the user can look but not touch.
This variant uses \fIgrid\fR instead of (gridX(), gridY()). If \fIgrid\fR is invalid (see TQSize::isValid()), arrangeItemsInGrid() calculates a valid grid itself and uses that.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Arranges all the items in the grid given by gridX() and gridY().
.PP
Even if sorting() is enabled, the items are not sorted by this function. If you want to sort or rearrange the items, use iconview->sort(iconview->sortDirection()).
.PP
If \fIupdate\fR is TRUE (the default), the viewport is repainted as well.
This signal is emitted when the user clicks any mouse button. If \fIitem\fR is non-null, the cursor is on \fIitem\fR. If \fIitem\fR is null, the mouse cursor isn't on any item.
.PP
See also mouseButtonClicked(), rightButtonClicked(), and pressed().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
This signal is emitted when the user clicks any mouse button on an icon view item. \fIitem\fR is a pointer to the item that has been clicked.
.PP
\fIpos\fR is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). (If the click's press and release differ by a pixel or two, \fIpos\fR is the position at release time.)
.PP
See also mouseButtonClicked(), rightButtonClicked(), and pressed().
This signal is emitted when the user invokes a context menu with the right mouse button or with special system keys, with \fIitem\fR being the item under the mouse cursor or the current item, respectively.
.PP
\fIpos\fR is the position for the context menu in the global coordinate system.
Returns the TQDragObject that should be used for drag-and-drop. This function is called by the icon view when starting a drag to get the dragobject that should be used for the drag. Subclasses may reimplement this.
\fIe\fR provides all the information about the drop. If the drag object of the drop was a TQIconDrag, \fIlst\fR contains the list of the dropped items. You can get the data using TQIconDragItem::data() on each item. If the \fIlst\fR is empty, i.e. the drag was not a TQIconDrag, you have to decode the data in \fIe\fR and work with that.
Finds the first item whose bounding rectangle overlaps \fIr\fR and returns a pointer to that item. \fIr\fR is given in content coordinates. Returns 0 if no item overlaps \fIr\fR.
If you want to find all items that touch \fIr\fR, you will need to use this function and nextItem() in a loop ending at findLastVisibleItem() and test TQIconViewItem::rect() for each of these items.
Returns a pointer to the first item whose text begins with \fItext\fR, or 0 if no such item could be found. Use the \fIcompare\fR flag to control the comparison behaviour. (See TQt::StringComparisonMode.)
Finds the last item whose bounding rectangle overlaps \fIr\fR and returns a pointer to that item. \fIr\fR is given in content coordinates. Returns 0 if no item overlaps \fIr\fR.
Inserts the TQIconViewItem \fIitem\fR in the icon view's grid. \fIYou should never need to call this function.\fR Instead, insert TQIconViewItems by creating them with a pointer to the TQIconView that they are to be inserted into.
Lays out a row of icons (if Arrangement == TopToBottom this is a column). Starts laying out with the item \fIbegin\fR. \fIy\fR is the starting coordinate. Returns the last item of the row (column) and sets the new starting coordinate to \fIy\fR. The \fIchanged\fR parameter is used internally.
.PP
\fBWarning:\fR This function may be made private in a future version of Qt. We do not recommend calling it.
This signal is emitted when the user clicks mouse button \fIbutton\fR. If \fIitem\fR is non-null, the cursor is on \fIitem\fR. If \fIitem\fR is null, the mouse cursor isn't on any item.
.PP
\fIpos\fR is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). (If the click's press and release differ by a pixel or two, \fIpos\fR is the position at release time.)
.PP
See also mouseButtonPressed(), rightButtonClicked(), and clicked().
This signal is emitted when the user presses mouse button \fIbutton\fR. If \fIitem\fR is non-null, the cursor is on \fIitem\fR. If \fIitem\fR is null, the mouse cursor isn't on any item.
.PP
\fIpos\fR is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()).
This signal is emitted after successfully dropping one (or more) items of the icon view. If the items should be removed, it's best to do so in a slot connected to this signal.
This signal is emitted when the user presses any mouse button. If \fIitem\fR is non-null, the cursor is on \fIitem\fR. If \fIitem\fR is null, the mouse cursor isn't on any item.
.PP
See also mouseButtonPressed(), rightButtonPressed(), and clicked().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
This signal is emitted when the user presses any mouse button. If \fIitem\fR is non-null, the cursor is on \fIitem\fR. If \fIitem\fR is null, the mouse cursor isn't on any item.
.PP
\fIpos\fR is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). (If the click's press and release differ by a pixel or two, \fIpos\fR is the position at release time.)
.PP
See also mouseButtonPressed(), rightButtonPressed(), and clicked().
This signal is emitted when the user clicks the right mouse button. If \fIitem\fR is non-null, the cursor is on \fIitem\fR. If \fIitem\fR is null, the mouse cursor isn't on any item.
.PP
\fIpos\fR is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). (If the click's press and release differ by a pixel or two, \fIpos\fR is the position at release time.)
.PP
See also rightButtonPressed(), mouseButtonClicked(), and clicked().
This signal is emitted when the user presses the right mouse button. If \fIitem\fR is non-null, the cursor is on \fIitem\fR. If \fIitem\fR is null, the mouse cursor isn't on any item.
.PP
\fIpos\fR is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()).
If \fIs\fR is TRUE and TQIconView::selectionMode() is Extended, \fIitem\fR is selected. If \fIcb\fR is TRUE, the selection state of the icon view's other items is left unchanged. If \fIcb\fR is FALSE (the default) all other items are unselected.
Sets whether the icon view will display a tool tip with the complete text for any truncated item text to \fIb\fR. See the "showToolTips" property for details.
If \fIsort\fR is TRUE, this function sets the icon view to sort items when a new item is inserted. If \fIsort\fR is FALSE, the icon view will not be sorted.
.PP
Note that autoArrange() must be TRUE for sorting to take place.
.PP
If \fIascending\fR is TRUE (the default), items are sorted in ascending order. If \fIascending\fR is FALSE, items are sorted in descending order.
TQIconViewItem::compare() is used to compare pairs of items. The sorting is based on the items' keys; these default to the items' text unless specifically set to something else.
Returns TRUE if the icon view will display a tool tip with the complete text for any truncated item text; otherwise returns FALSE. See the "showToolTips" property for details.
The icon view is not redrawn immediately after inserting a new item but after a very small delay using a TQTimer. This means that when many items are inserted in a loop the icon view is probably redrawn only once at the end of the loop. This makes the insertions both flicker-free and faster.
Sorts and rearranges all the items in the icon view. If \fIascending\fR is TRUE, the items are sorted in increasing order, otherwise they are sorted in decreasing order.
TQIconViewItem::compare() is used to compare pairs of items. The sorting is based on the items' keys; these default to the items' text unless specifically set to something else.
Takes the icon view item \fIitem\fR out of the icon view and causes an update of the screen display. The item is not deleted. You should normally not need to call this function because TQIconViewItem::~TQIconViewItem() calls it. The normal way to delete an item is to delete it.
Note that if the icon view is not visible at the time of insertion, TQIconView defers all position-related work until it is shown and then calls arrangeItemsInGrid().
Set this property's value with setMaxItemWidth() and get this property's value with maxItemWidth().
.SH "ResizeMode resizeMode"
This property holds the resize mode of the icon view.
.PP
This can be Fixed or Adjust. The default is Fixed. See ResizeMode.
.PP
Set this property's value with setResizeMode() and get this property's value with resizeMode().
.SH "SelectionMode selectionMode"
This property holds the selection mode of the icon view.
.PP
This can be Single (the default), Extended, Multi or NoSelection.
.PP
Set this property's value with setSelectionMode() and get this property's value with selectionMode().
.SH "bool showToolTips"
This property holds whether the icon view will display a tool tip with the complete text for any truncated item text.
.PP
The default is TRUE. Note that this has no effect if setWordWrapIconText() is TRUE, as it is by default.
.PP
Set this property's value with setShowToolTips() and get this property's value with showToolTips().
.SH "bool sortDirection"
This property holds whether the sort direction for inserting new items is ascending;.
.PP
The default is TRUE (i.e. ascending). This sort direction is only meaningful if both sorting() and autoArrange() are TRUE.
.PP
To set the sort direction, use setSorting()
.PP
Get this property's value with sortDirection().
.SH "bool sorting"
This property holds whether the icon view sorts on insertion.
.PP
The default is FALSE, i.e. no sorting on insertion.
.PP
To set the sorting, use setSorting().
.PP
Get this property's value with sorting().
.SH "int spacing"
This property holds the space in pixels between icon view items.
.PP
The default is 5 pixels.
.PP
Negative values for spacing are illegal.
.PP
Set this property's value with setSpacing() and get this property's value with spacing().
.SH "bool wordWrapIconText"
This property holds whether the item text will be word-wrapped if it is too long.
.PP
The default is TRUE.
.PP
If this property is FALSE, icon text that is too long is truncated, and an ellipsis (...) appended to indicate that truncation has occurred. The full text can still be seen by the user if they hover the mouse because the full text is shown in a tooltip; see setShowToolTips().
.PP
Set this property's value with setWordWrapIconText() and get this property's value with wordWrapIconText().