📄 qlistwidget.cpp
字号:
/*! \fn void QListWidgetItem::setStatusTip(const QString &statusTip) Sets the status tip for the list item to the text specified by \a statusTip. \sa statusTip() setToolTip() setWhatsThis()*//*! \fn void QListWidgetItem::setToolTip(const QString &toolTip) Sets the tooltip for the list item to the text specified by \a toolTip. \sa toolTip() setStatusTip() setWhatsThis()*//*! \fn void QListWidgetItem::setWhatsThis(const QString &whatsThis) Sets the "What's This?" help for the list item to the text specified by \a whatsThis. \sa whatsThis() setStatusTip() setToolTip()*//*! \fn void QListWidgetItem::setFont(const QFont &font) Sets the font used when painting the item to the given \a font.*//*! \fn void QListWidgetItem::setTextAlignment(int alignment) Sets the list item's text alignment to \a alignment (see \l{Qt::AlignmentFlag}).*//*! \fn void QListWidgetItem::setBackgroundColor(const QColor &color) \obsolete This function is deprecated. Use setBackground() instead.*//*! \fn void QListWidgetItem::setBackground(const QBrush &brush) \since 4.2 Sets the background brush of the list item to the given \a brush. \sa background() setForeground()*//*! \fn void QListWidgetItem::setTextColor(const QColor &color) \obsolete This function is deprecated. Use setForeground() instead.*//*! \fn void QListWidgetItem::setForeground(const QBrush &brush) \since 4.2 Sets the foreground brush of the list item to the given \a brush. \sa foreground() setBackground()*//*! \fn void QListWidgetItem::setCheckState(Qt::CheckState state) Sets the check state of the list item to \a state. \sa checkState()*/void QListWidgetPrivate::setup(){ Q_Q(QListWidget); q->QListView::setModel(new QListModel(q)); // view signals QObject::connect(q, SIGNAL(pressed(QModelIndex)), q, SLOT(_q_emitItemPressed(QModelIndex))); QObject::connect(q, SIGNAL(clicked(QModelIndex)), q, SLOT(_q_emitItemClicked(QModelIndex))); QObject::connect(q, SIGNAL(doubleClicked(QModelIndex)), q, SLOT(_q_emitItemDoubleClicked(QModelIndex))); QObject::connect(q, SIGNAL(activated(QModelIndex)), q, SLOT(_q_emitItemActivated(QModelIndex))); QObject::connect(q, SIGNAL(entered(QModelIndex)), q, SLOT(_q_emitItemEntered(QModelIndex))); QObject::connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), q, SLOT(_q_emitItemChanged(QModelIndex))); QObject::connect(q->selectionModel(), SIGNAL(currentChanged(QModelIndex,QModelIndex)), q, SLOT(_q_emitCurrentItemChanged(QModelIndex,QModelIndex))); QObject::connect(q->selectionModel(), SIGNAL(selectionChanged(QItemSelection,QItemSelection)), q, SIGNAL(itemSelectionChanged())); QObject::connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), q, SLOT(_q_dataChanged(QModelIndex,QModelIndex))); QObject::connect(model(), SIGNAL(columnsRemoved(QModelIndex,int,int)), q, SLOT(_q_sort()));}void QListWidgetPrivate::_q_emitItemPressed(const QModelIndex &index){ Q_Q(QListWidget); emit q->itemPressed(model()->at(index.row()));}void QListWidgetPrivate::_q_emitItemClicked(const QModelIndex &index){ Q_Q(QListWidget); emit q->itemClicked(model()->at(index.row()));}void QListWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index){ Q_Q(QListWidget); emit q->itemDoubleClicked(model()->at(index.row()));}void QListWidgetPrivate::_q_emitItemActivated(const QModelIndex &index){ Q_Q(QListWidget); emit q->itemActivated(model()->at(index.row()));}void QListWidgetPrivate::_q_emitItemEntered(const QModelIndex &index){ Q_Q(QListWidget); emit q->itemEntered(model()->at(index.row()));}void QListWidgetPrivate::_q_emitItemChanged(const QModelIndex &index){ Q_Q(QListWidget); emit q->itemChanged(model()->at(index.row()));}void QListWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex ¤t, const QModelIndex &previous){ Q_Q(QListWidget); QListWidgetItem *currentItem = model()->at(current.row()); emit q->currentItemChanged(currentItem, model()->at(previous.row())); emit q->currentTextChanged(currentItem ? currentItem->text() : QString()); emit q->currentRowChanged(current.row());}void QListWidgetPrivate::_q_sort(){ if (sortingEnabled) model()->sort(0, sortOrder);}void QListWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight){ if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) model()->ensureSorted(topLeft.column(), sortOrder, topLeft.row(), bottomRight.row());}/*! \class QListWidget \brief The QListWidget class provides an item-based list widget. \ingroup model-view \mainclass QListWidget is a convenience class that provides a list view similar to the one supplied by QListView, but with a classic item-based interface for adding and removing items. QListWidget uses an internal model to manage each QListWidgetItem in the list. For a more flexible list view widget, use the QListView class with a standard model. List widgets are constructed in the same way as other widgets: \quotefile snippets/qlistwidget-using/mainwindow.cpp \skipto = new QListWidget \printline = new The selectionMode() of a list widget determines how many of the items in the list can be selected at the same time, and whether complex selections of items can be created. This can be set with the setSelectionMode() function. There are two ways to add items to the list: they can be constructed with the list widget as their parent widget, or they can be constructed with no parent widget and added to the list later. If a list widget already exists when the items are constructed, the first method is easier to use: \skipto new QListWidgetItem \printuntil new QListWidgetItem(tr("Pine") If you need to insert a new item into the list at a particular position, it is more required to construct the item without a parent widget and use the insertItem() function to place it within the list. The list widget will take ownership of the item. \skipto QListWidgetItem *newItem \printuntil newItem->setText \skipto listWidget->insertItem \printuntil listWidget->insertItem For multiple items, insertItems() can be used instead. The number of items in the list is found with the count() function. To remove items from the list, use takeItem(). The current item in the list can be found with currentItem(), and changed with setCurrentItem(). The user can also change the current item by navigating with the keyboard or clicking on a different item. When the current item changes, the currentItemChanged() signal is emitted with the new current item and the item that was previously current. \table 100% \row \o \inlineimage windowsxp-listview.png Screenshot of a Windows XP style list widget \o \inlineimage macintosh-listview.png Screenshot of a Macintosh style table widget \o \inlineimage plastique-listview.png Screenshot of a Plastique style table widget \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} list widget. \o A \l{Macintosh Style Widget Gallery}{Macintosh style} list widget. \o A \l{Plastique Style Widget Gallery}{Plastique style} list widget. \endtable \sa QListWidgetItem, QListView, QTreeView, {Model/View Programming}, {Config Dialog Example}*//*! \fn void QListWidget::addItem(QListWidgetItem *item) Inserts the \a item at the the end of the list widget. \warning A QListWidgetItem can only be added to a QListWidget once. Adding the same QListWidgetItem multiple times to a QListWidget will result in undefined behavior. \sa insertItem()*//*! \fn void QListWidget::addItem(const QString &label) Inserts an item with the text \a label at the end of the list widget.*//*! \fn void QListWidget::addItems(const QStringList &labels) Inserts items with the text \a labels at the end of the list widget. \sa insertItems()*//*! \fn void QListWidget::itemPressed(QListWidgetItem *item) This signal is emitted with the specified \a item when a mouse button is pressed on an item in the widget. \sa itemClicked(), itemDoubleClicked()*//*! \fn void QListWidget::itemClicked(QListWidgetItem *item) This signal is emitted with the specified \a item when a mouse button is clicked on an item in the widget. \sa itemPressed(), itemDoubleClicked()*//*! \fn void QListWidget::itemDoubleClicked(QListWidgetItem *item) This signal is emitted with the specified \a item when a mouse button is double clicked on an item in the widget. \sa itemClicked(), itemPressed()*//*! \fn void QListWidget::itemActivated(QListWidgetItem *item) This signal is emitted when the \a item is activated. The \a item is activated when the user clicks or double clicks on it, depending on the system configuration. It is also activated when the user presses the activation key (on Windows and X11 this is the \gui Return key, on Mac OS X it is \key{Ctrl+0}).*//*! \fn void QListWidget::itemEntered(QListWidgetItem *item) This signal is emitted when the mouse cursor enters an item. The \a item is the item entered. This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.*//*! \fn void QListWidget::itemChanged(QListWidgetItem *item) This signal is emitted whenever the data of \a item has changed.*//*! \fn void QListWidget::currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) This signal is emitted whenever the current item changes. The \a previous item is the item that previously had the focus, \a current is the new current item.*//*! \fn void QListWidget::currentTextChanged(const QString ¤tText) This signal is emitted whenever the current item changes. The \a currentText is the text data in the current item. If there is no current item, the \a currentText is invalid.*//*! \fn void QListWidget::currentRowChanged(int currentRow) This signal is emitted whenever the current item changes. The \a currentRow is the row of the current item. If there is no current item, the \a currentRow is -1.*//*! \fn void QListWidget::itemSelectionChanged() This signal is emitted whenever the selection changes. \sa selectedItems() isItemSelected() currentItemChanged()*//*! \since 4.3 \fn void QListWidget::removeItemWidget(QListWidgetItem *item) Removes the widget set on the given \a item.*//*! Constructs an empty QListWidget with the given \a parent.*/QListWidget::QListWidget(QWidget *parent) : QListView(*new QListWidgetPrivate(), parent){ Q_D(QListWidget); d->setup();}/*! Destroys the list widget and all its items.*/QListWidget::~QListWidget(){}/*! Returns the item that occupies the given \a row in the list if one has been set; otherwise returns 0. \sa row()*/QListWidgetItem *QListWidget::item(int row) const{ Q_D(const QListWidget); if (row < 0 || row >= d->model()->rowCount()) return 0; return d->model()->at(row);}/*! Returns the row containing the given \a item. \sa item()*/int QListWidget::row(const QListWidgetItem *item) const{ Q_D(const QListWidget); return d->model()->index(const_cast<QListWidgetItem*>(item)).row();}/*! Inserts the \a item at the position in the list given by \a row. \sa addItem()*/void QListWidget::insertItem(int row, QListWidgetItem *item){ Q_D(QListWidget); if (item && !item->view) d->model()->insert(row, item);}/*! Inserts an item with the text \a label in the list widget at the position given by \a row. \sa addItem()*/void QListWidget::insertItem(int row, const QString &label){ Q_D(QListWidget); d->model()->insert(row, new QListWidgetItem(label));}/*! Inserts items from the list of \a labels into the list, starting at the given \a row. \sa insertItem(), addItem()*/void QListWidget::insertItems(int row, const QStringList &labels){ Q_D(QListWidget); d->model()->insert(row, labels);}/*! Removes and returns the item from the given \a row in the list widget; otherwise returns 0. Items removed from a list widget will not be managed by Qt, and will need to be deleted manually. \sa insertItem(), addItem()*/QListWidgetItem *QListWidget::takeItem(int row){ Q_D(QListWidget); if (row < 0 || row >= d->model()->rowCount()) return 0; return d->model()->take(row);}/*! \property QListWidget::count \brief the number of items in the list including any hidden items.*/int QListWidget::count() const
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -