📄 qlistwidget.cpp
字号:
/*! This function sets the data for a given \a role to the given \a value (see \l{Qt::ItemDataRole}). Reimplement this function if you need extra roles or special behavior for certain roles. \sa Qt::ItemDataRole, data(), itemData()*/void QListWidgetItem::setData(int role, const QVariant &value){ bool found = false; role = (role == Qt::EditRole ? Qt::DisplayRole : role); for (int i = 0; i < values.count(); ++i) { if (values.at(i).role == role) { if (values.at(i).value == value) return; values[i].value = value; found = true; break; } } if (!found) values.append(QWidgetItemData(role, value)); if (model) model->itemChanged(this);}/*! This function returns the item's data for a given \a role (see {Qt::ItemDataRole}). Reimplement this function if you need extra roles or special behavior for certain roles.*/QVariant QListWidgetItem::data(int role) const{ role = (role == Qt::EditRole ? Qt::DisplayRole : role); for (int i = 0; i < values.count(); ++i) if (values.at(i).role == role) return values.at(i).value; return QVariant();}/*! Returns true if this item's text is less then \a other item's text; otherwise returns false.*/bool QListWidgetItem::operator<(const QListWidgetItem &other) const{ return text() < other.text();}#ifndef QT_NO_DATASTREAM/*! Reads the item from stream \a in. \sa write()*/void QListWidgetItem::read(QDataStream &in){ in >> values;}/*! Writes the item to stream \a out. \sa read()*/void QListWidgetItem::write(QDataStream &out) const{ out << values;}/*! \since 4.1 Constructs a copy of \a other. Note that type() and listWidget() are not copied. This function is useful when reimplementing clone(). \sa data(), flags()*/QListWidgetItem::QListWidgetItem(const QListWidgetItem &other) : rtti(Type), values(other.values), view(0), model(0), itemFlags(other.itemFlags){}/*! Assigns \a other's data and flags to this item. Note that type() and listWidget() are not copied. This function is useful when reimplementing clone(). \sa data(), flags()*/QListWidgetItem &QListWidgetItem::operator=(const QListWidgetItem &other){ values = other.values; itemFlags = other.itemFlags; return *this;}/*! \relates QListWidgetItem Writes the list widget item \a item to stream \a out. This operator uses QListWidgetItem::write(). \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &out, const QListWidgetItem &item){ item.write(out); return out;}/*! \relates QListWidgetItem Reads a list widget item from stream \a in into \a item. This operator uses QListWidgetItem::read(). \sa {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &in, QListWidgetItem &item){ item.read(in); return in;}#endif // QT_NO_DATASTREAM/*! \fn Qt::ItemFlags QListWidgetItem::flags() const Returns the item flags for this item (see {Qt::ItemFlags}).*//*! \fn QString QListWidgetItem::text() const Returns the list item's text. \sa setText()*//*! \fn QIcon QListWidgetItem::icon() const Returns the list item's icon. \sa setIcon(), {QAbstractItemView::iconSize}{iconSize}*//*! \fn QString QListWidgetItem::statusTip() const Returns the list item's status tip. \sa setStatusTip()*//*! \fn QString QListWidgetItem::toolTip() const Returns the list item's tooltip. \sa setToolTip() statusTip() whatsThis()*//*! \fn QString QListWidgetItem::whatsThis() const Returns the list item's "What's This?" help text. \sa setWhatsThis() statusTip() toolTip()*//*! \fn QFont QListWidgetItem::font() const Returns the font used to display this list item's text.*//*! \fn int QListWidgetItem::textAlignment() const Returns the text alignment for the list item (see \l{Qt::AlignmentFlag}).*//*! \fn QColor QListWidgetItem::backgroundColor() const Returns the color used to display the list item's background. \sa setBackgroundColor() textColor()*//*! \fn QColor QListWidgetItem::textColor() const Returns the used to display the list item's text. \sa setTextColor() backgroundColor()*//*! \fn Qt::CheckState QListWidgetItem::checkState() const Returns the checked state of the list item (see \l{Qt::CheckState}. \sa flags()*//*! \fn QSize QListWidgetItem::sizeHint() const \since 4.1 Returns the size hint set for the list item.*//*! \fn void QListWidgetItem::setSizeHint(const QSize &size) \since 4.1 Sets the size hint for the list item to be \a size. If no size hint is set, the item delegate will compute the size hint based on the item data.*//*! \fn void QListWidgetItem::setFlags(Qt::ItemFlags flags) Sets the item flags for the list item to \a flags (see \l{Qt::ItemFlags}).*//*! \fn void QListWidgetItem::setText(const QString &text) Sets the text for the list widget item's to the given \a text. \sa text()*//*! \fn void QListWidgetItem::setIcon(const QIcon &icon) Sets the icon for the list item to the given \a icon. \sa icon(), text(), {QAbstractItemView::iconSize}{iconSize}*//*! \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) Sets the background color of the list item to the given \a color. \sa backgroundColor() setTextColor()*//*! \fn void QListWidgetItem::setTextColor(const QColor &color) Sets the text color for the list item to the given \a color. \sa textColor() setBackgroundColor()*//*! \fn void QListWidgetItem::setCheckState(Qt::CheckState state) Sets the check state of the list item to \a state. \sa checkState()*/class QListWidgetPrivate : public QListViewPrivate{ Q_DECLARE_PUBLIC(QListWidget)public: QListWidgetPrivate() : QListViewPrivate() {} inline QListModel *model() const { return ::qobject_cast<QListModel*>(q_func()->model()); } void setup(); void _q_emitItemPressed(const QModelIndex &index); void _q_emitItemClicked(const QModelIndex &index); void _q_emitItemDoubleClicked(const QModelIndex &index); void _q_emitItemActivated(const QModelIndex &index); void _q_emitItemEntered(const QModelIndex &index); void _q_emitItemChanged(const QModelIndex &index); void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex ¤t);};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))); // model signals QObject::connect(model(), SIGNAL(dataChanged(QModelIndex,QModelIndex)), q, SLOT(_q_emitItemChanged(QModelIndex))); // selection signals 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()));}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());}/*! \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 convenient to construct the item without a parent widget and use the insertItem() function to place it within the list: \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}*//*! \fn void QListWidget::addItem(QListWidgetItem *item) Inserts the \a item at the the end of the list widget. \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 when an item has been pressed (mouse click and release).*//*! \fn void QListWidget::itemClicked(QListWidgetItem *item) This signal is emitted when a mouse button is clicked.*//*! \fn void QListWidget::itemDoubleClicked(QListWidgetItem *item) This signal is emitted when a mouse button is double clicked.*//*! \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}).*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -