📄 q3listbox.cpp
字号:
pixmap \a pixmap. The item gets inserted after the item \a after, or at the beginning if \a after is 0.*/Q3ListBoxPixmap::Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap &pixmap, Q3ListBoxItem *after) : Q3ListBoxItem(listbox, after){ pm = pixmap;}/*! Destroys the item.*/Q3ListBoxPixmap::~Q3ListBoxPixmap(){}/*! Constructs a new list box item in list box \a listbox showing the pixmap \a pix and the text \a text.*/Q3ListBoxPixmap::Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap &pix, const QString& text) : Q3ListBoxItem(listbox){ pm = pix; setText(text);}/*! Constructs a new list box item showing the pixmap \a pix and the text to \a text.*/Q3ListBoxPixmap::Q3ListBoxPixmap(const QPixmap & pix, const QString& text) : Q3ListBoxItem(){ pm = pix; setText(text);}/*! Constructs a new list box item in list box \a listbox showing the pixmap \a pix and the string \a text. The item gets inserted after the item \a after, or at the beginning if \a after is 0.*/Q3ListBoxPixmap::Q3ListBoxPixmap(Q3ListBox* listbox, const QPixmap & pix, const QString& text, Q3ListBoxItem *after) : Q3ListBoxItem(listbox, after){ pm = pix; setText(text);}/*! \fn const QPixmap *Q3ListBoxPixmap::pixmap() const Returns the pixmap associated with the item.*//*! Draws the pixmap using \a painter.*/void Q3ListBoxPixmap::paint(QPainter *painter){ int itemHeight = height(listBox()); int yPos; const QPixmap *pm = pixmap(); if (pm && ! pm->isNull()) { yPos = (itemHeight - pm->height()) / 2; painter->drawPixmap(3, yPos, *pm); } if (!text().isEmpty()) { QFontMetrics fm = painter->fontMetrics(); yPos = ((itemHeight - fm.height()) / 2) + fm.ascent(); painter->drawText(pm->width() + 5, yPos, text()); }}/*! Returns the height of the pixmap in list box \a lb. \sa paint(), width()*/int Q3ListBoxPixmap::height(const Q3ListBox* lb) const{ int h; if (text().isEmpty()) h = pm.height(); else h = qMax(pm.height(), lb->fontMetrics().lineSpacing() + 2); return qMax(h, QApplication::globalStrut().height());}/*! Returns the width of the pixmap plus some margin in list box \a lb. \sa paint(), height()*/int Q3ListBoxPixmap::width(const Q3ListBox* lb) const{ if (text().isEmpty()) return qMax(pm.width() + 6, QApplication::globalStrut().width()); return qMax(pm.width() + lb->fontMetrics().width(text()) + 6, QApplication::globalStrut().width());}/*! \fn int Q3ListBoxPixmap::rtti() const \reimp Returns 2. Make your derived classes return their own values for rtti(), and you can distinguish between listbox items. You should use values greater than 1000 preferably a large random number, to allow for extensions to this class.*/int Q3ListBoxPixmap::rtti() const{ return RTTI;}/*! \class Q3ListBox qlistbox.h \brief The Q3ListBox widget provides a list of selectable, read-only items. \compat This is typically a single-column list in which either no item or one item is selected, but it can also be used in many other ways. Q3ListBox will add scroll bars as necessary, but it isn't intended for \e really big lists. If you want more than a few thousand items, it's probably better to use a different widget mainly because the scroll bars won't provide very good navigation, but also because Q3ListBox may become slow with huge lists. (See Q3ListView and Q3Table for possible alternatives.) There are a variety of selection modes described in the Q3ListBox::SelectionMode documentation. The default is \l Single selection mode, but you can change it using setSelectionMode(). (setMultiSelection() is still provided for compatibility with Qt 1.x. We recommend using setSelectionMode() in all code.) Because Q3ListBox offers multiple selection it must display keyboard focus and selection state separately. Therefore there are functions both to set the selection state of an item, i.e. setSelected(), and to set which item displays keyboard focus, i.e. setCurrentItem(). The list box normally arranges its items in a single column and adds a vertical scroll bar if required. It is possible to have a different fixed number of columns (setColumnMode()), or as many columns as will fit in the list box's assigned screen space (setColumnMode(FitToWidth)), or to have a fixed number of rows (setRowMode()) or as many rows as will fit in the list box's assigned screen space (setRowMode(FitToHeight)). In all these cases Q3ListBox will add scroll bars, as appropriate, in at least one direction. If multiple rows are used, each row can be as high as necessary (the normal setting), or you can request that all items will have the same height by calling setVariableHeight(false). The same applies to a column's width, see setVariableWidth(). The Q3ListBox's items are Q3ListBoxItem objects. Q3ListBox provides methods to insert new items as strings, as pixmaps, and as Q3ListBoxItem * (insertItem() with various arguments), and to replace an existing item with a new string, pixmap or Q3ListBoxItem (changeItem() with various arguments). You can also remove items singly with removeItem() or clear() the entire list box. Note that if you create a Q3ListBoxItem yourself and insert it, Q3ListBox takes ownership of the item. You can also create a Q3ListBoxItem, such as Q3ListBoxText or Q3ListBoxPixmap, with the list box as first parameter. The item will then append itself. When you delete an item it is automatically removed from the list box. The list of items can be arbitrarily large; Q3ListBox will add scroll bars if necessary. Q3ListBox can display a single-column (the common case) or multiple-columns, and offers both single and multiple selection. Q3ListBox does not support multiple-column items (but Q3ListView and Q3Table do), or tree hierarchies (but Q3ListView does). The list box items can be accessed both as Q3ListBoxItem objects (recommended) and using integer indexes (the original Q3ListBox implementation used an array of strings internally, and the API still supports this mode of operation). Everything can be done using the new objects, and most things can be done using indexes. Each item in a Q3ListBox contains a Q3ListBoxItem. One of the items can be the current item. The currentChanged() signal and the highlighted() signal are emitted when a new item becomes current, e.g. because the user clicks on it or Q3ListBox::setCurrentItem() is called. The selected() signal is emitted when the user double-clicks on an item or presses Enter on the current item. If the user does not select anything, no signals are emitted and currentItem() returns -1. A list box has Qt::WheelFocus as a default focusPolicy(), i.e. it can get keyboard focus by tabbing, clicking and through the use of the mouse wheel. New items can be inserted using insertItem(), insertStrList() or insertStringList(). By default, vertical and horizontal scroll bars are added and removed as necessary. setHScrollBarMode() and setVScrollBarMode() can be used to change this policy. If you need to insert types other than strings and pixmaps, you must define new classes which inherit Q3ListBoxItem. \warning The list box assumes ownership of all list box items and will delete them when it does not need them any more. \inlineimage qlistbox-m.png Screenshot in Motif style \inlineimage qlistbox-w.png Screenshot in Windows style \sa Q3ListView, QComboBox, QButtonGroup*//*! \enum Q3ListBox::SelectionMode This enumerated type is used by Q3ListBox to indicate how it reacts to selection by the user. \value Single 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, even though the selection may be cleared by the application programmer using Q3ListBox::clearSelection(). \value Multi When the user selects an item the selection status of that item is toggled and the other items are left alone. \value Extended 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. And 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 is kept pressed. \value NoSelection Items cannot be selected. In other words, \c Single is a real single-selection list box, \c Multi is a real multi-selection list box, \c Extended is a list box in which users can select multiple items but usually want to select either just one or a range of contiguous items, and \c NoSelection is for a list box where the user can look but not touch.*//*! \enum Q3ListBox::LayoutMode This enum type is used to specify how Q3ListBox lays out its rows and columns. \value FixedNumber There is a fixed number of rows (or columns). \value FitToWidth There are as many columns as will fit on-screen. \value FitToHeight There are as many rows as will fit on-screen. \value Variable There are as many rows as are required by the column mode. (Or as many columns as required by the row mode.) Example: When you call setRowMode(FitToHeight), columnMode() automatically becomes \c Variable to accommodate the row mode you've set.*//*! \fn void Q3ListBox::onItem(Q3ListBoxItem *i) This signal is emitted when the user moves the mouse cursor onto an item, similar to the QWidget::enterEvent() function. \a i is the Q3ListBoxItem that the mouse has moved on.*/// ### bug here too? enter/leave event may noit considered. move the// mouse out of the window and back in, to the same item - does it// work?/*! \fn void Q3ListBox::onViewport() This signal is emitted when the user moves the mouse cursor from an item to an empty part of the list box.*//*! Constructs a new empty list box called \a name and with parent \a parent and widget attributes \a f. This constructor sets the Qt::WA_StaticContent and the Qt::WA_NoBackground attributes to boost performance when drawing Q3ListBoxItems. This may be unsuitable for custom Q3ListBoxItem classes, in which case Qt::WA_StaticContents and Qt::WA_NoBackground should be cleared on the viewport() after construction.*/Q3ListBox::Q3ListBox(QWidget *parent, const char *name, Qt::WindowFlags f) : Q3ScrollView(parent, name, f | Qt::WStaticContents | Qt::WNoAutoErase){ d = new Q3ListBoxPrivate(this); d->updateTimer = new QTimer(this, "listbox update timer"); d->visibleTimer = new QTimer(this, "listbox visible timer"); d->inputTimer = new QTimer(this, "listbox input timer"); d->resizeTimer = new QTimer(this, "listbox resize timer"); d->clearing = false; d->pressedItem = 0; d->selectAnchor = 0; d->select = false; d->rubber = 0; setMouseTracking(true); viewport()->setMouseTracking(true); connect(d->updateTimer, SIGNAL(timeout()), this, SLOT(refreshSlot())); connect(d->visibleTimer, SIGNAL(timeout()), this, SLOT(ensureCurrentVisible())); connect(d->resizeTimer, SIGNAL(timeout()), this, SLOT(adjustItems())); viewport()->setBackgroundRole(QPalette::Base); viewport()->setFocusProxy(this); viewport()->setFocusPolicy(Qt::WheelFocus); setFocusPolicy(Qt::WheelFocus); setAttribute(Qt::WA_MacShowFocusRect);}Q3ListBox * Q3ListBox::changedListBox = 0;/*! Destroys the list box. Deletes all list box items.*/Q3ListBox::~Q3ListBox(){ if (changedListBox == this) changedListBox = 0; clear(); delete d; d = 0;}/*! \fn void Q3ListBox::pressed(Q3ListBoxItem *item) This signal is emitted when the user presses any mouse button. If \a item is not 0, the cursor is on \a item. If \a item is 0, the mouse cursor isn't on any item. Note that you must not delete any Q3ListBoxItem objects in slots connected to this signal.*//*! \fn void Q3ListBox::pressed(Q3ListBoxItem *item, const QPoint &pnt) \overload This signal is emitted when the user presses any mouse button. If \a item is not 0, the cursor is on \a item. If \a item is 0, the mouse cursor isn't on any item. \a pnt is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). Note that you must not delete any Q3ListBoxItem objects in slots connected to this signal. \sa mouseButtonPressed() rightButtonPressed() clicked()*//*! \fn void Q3ListBox::clicked(Q3ListBoxItem *item) This signal is emitted when the user clicks any mouse button. If \a item is not 0, the cursor is on \a item. If \a item is 0, the mouse cursor isn't on any item. Note that you must not delete any Q3ListBoxItem objects in slots connected to this signal.*//*! \fn void Q3ListBox::clicked(Q3ListBoxItem *item, const QPoint &pnt) \overload This signal is emitted when the user clicks any mouse button. If \a item is not 0, the cursor is on \a item. If \a item is 0, the mouse cursor isn't on any item. \a pnt is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). (If the click's press and release differs by a pixel or two, \a pnt is the position at release time.) Note that you must not delete any Q3ListBoxItem objects in slots connected to this signal.*//*! \fn void Q3ListBox::mouseButtonClicked (int button, Q3ListBoxItem * item, const QPoint & pos) This signal is emitted when the user clicks mouse button \a button. If \a item is not 0, the cursor is on \a item. If \a item is 0, the mouse cursor isn't on any item. \a pos is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). (If the click's press and release differs by a pixel or two, \a pos is the position at release time.) Note that you must not delete any Q3ListBoxItem objects in slots connected to this signal.*//*! \fn void Q3ListBox::mouseButtonPressed (int button, Q3ListBoxItem * item, const QPoint & pos) This signal is emitted when the user presses mouse button \a button. If \a item is not 0, the cursor is on \a item. If \a item is 0, the mouse cursor isn't on any item. \a pos is the position of the mouse cursor in the global coordinate system (QMouseEvent::globalPos()). Note that you must not delete any Q3ListBoxItem objects in slots connected to this signal.*//*! \fn void Q3ListBox::doubleClicked(Q3ListBoxItem *item) This signal is emitted whenever an item is double-clicked. It's emitted on the second button press, not the second button release. If \a item is not 0, the cursor is on \a item. If \a item is 0, the mouse cursor isn't on any item.*//*! \fn void Q3ListBox::returnPressed(Q3ListBoxItem *item) This signal is emitted when Enter or Return is pressed. The
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -