📄 qlistwidget.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2006 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qlistwidget.h"#ifndef QT_NO_LISTWIDGET#include <qitemdelegate.h>#include <qpainter.h>#include <private/qlistview_p.h>#include <private/qwidgetitemdata_p.h>// workaround for VC++ 6.0 linker bug (?)typedef bool(*LessThan)(const QPair<QListWidgetItem*,int>&,const QPair<QListWidgetItem*,int>&);class QListWidgetMimeData : public QMimeData{ Q_OBJECTpublic: QList<QListWidgetItem*> items;};class QListModel : public QAbstractListModel{ Q_OBJECTpublic: QListModel(QListWidget *parent); ~QListModel(); void clear(); QListWidgetItem *at(int row) const; void insert(int row, QListWidgetItem *item); void insert(int row, const QStringList &items); void remove(QListWidgetItem *item); QListWidgetItem *take(int row); int rowCount(const QModelIndex &parent = QModelIndex()) const; QModelIndex index(QListWidgetItem *item) const; QModelIndex index(int row, int column = 0, const QModelIndex &parent = QModelIndex()) const; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; bool setData(const QModelIndex &index, const QVariant &value, int role); bool insertRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()); bool removeRows(int row, int count = 1, const QModelIndex &parent = QModelIndex()); Qt::ItemFlags flags(const QModelIndex &index) const; void sort(int column, Qt::SortOrder order); static bool itemLessThan(const QPair<QListWidgetItem*,int> &left, const QPair<QListWidgetItem*,int> &right); static bool itemGreaterThan(const QPair<QListWidgetItem*,int> &left, const QPair<QListWidgetItem*,int> &right); void itemChanged(QListWidgetItem *item); // dnd QStringList mimeTypes() const; QMimeData *mimeData(const QModelIndexList &indexes) const;#ifndef QT_NO_DRAGANDDROP bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent); Qt::DropActions supportedDropActions() const;#endif QMimeData *internalMimeData() const;private: QList<QListWidgetItem*> lst; // A cache must be mutable if get-functions should have const modifiers mutable QModelIndexList cachedIndexes;};#include "qlistwidget.moc"QListModel::QListModel(QListWidget *parent) : QAbstractListModel(parent){}QListModel::~QListModel(){ clear();}void QListModel::clear(){ for (int i = 0; i < lst.count(); ++i) { if (lst.at(i)) { lst.at(i)->model = 0; lst.at(i)->view = 0; delete lst.at(i); } } lst.clear(); reset();}QListWidgetItem *QListModel::at(int row) const{ return lst.value(row);}void QListModel::remove(QListWidgetItem *item){ Q_ASSERT(item); int row = lst.indexOf(item); Q_ASSERT(row != -1); beginRemoveRows(QModelIndex(), row, row); lst.at(row)->model = 0; lst.at(row)->view = 0; lst.removeAt(row); endRemoveRows();}void QListModel::insert(int row, QListWidgetItem *item){ Q_ASSERT(item); item->model = this; item->view = ::qobject_cast<QListWidget*>(QObject::parent()); if (row < 0) row = 0; else if (row > lst.count()) row = lst.count(); beginInsertRows(QModelIndex(), row, row); lst.insert(row, item); endInsertRows();}void QListModel::insert(int row, const QStringList &labels){ const int count = labels.count(); if (count <= 0) return; if (row < 0) row = 0; else if (row > lst.count()) row = lst.count(); beginInsertRows(QModelIndex(), row, row + count - 1); for (int i = 0; i < count; ++i) { QListWidgetItem *item = new QListWidgetItem(labels.at(i)); item->model = this; item->view = ::qobject_cast<QListWidget*>(QObject::parent()); lst.insert(row++, item); } endInsertRows();}QListWidgetItem *QListModel::take(int row){ Q_ASSERT(row >= 0 && row < lst.count()); beginRemoveRows(QModelIndex(), row, row); lst.at(row)->model = 0; lst.at(row)->view = 0; QListWidgetItem *item = lst.takeAt(row); endRemoveRows(); return item;}int QListModel::rowCount(const QModelIndex &parent) const{ return parent.isValid() ? 0 : lst.count();}QModelIndex QListModel::index(QListWidgetItem *item) const{ Q_ASSERT(item); int row = lst.lastIndexOf(item); Q_ASSERT(row != -1); return createIndex(row, 0, item);}QModelIndex QListModel::index(int row, int column, const QModelIndex &parent) const{ if (hasIndex(row, column, parent)) return createIndex(row, column, lst.at(row)); return QModelIndex();}QVariant QListModel::data(const QModelIndex &index, int role) const{ if (!index.isValid() || index.row() >= lst.count()) return QVariant(); return lst.at(index.row())->data(role);}bool QListModel::setData(const QModelIndex &index, const QVariant &value, int role){ if (!index.isValid() || index.row() >= lst.count()) return false; lst.at(index.row())->setData(role, value); return true;}bool QListModel::insertRows(int row, int count, const QModelIndex &){ if (count < 1 || row < 0 || row > rowCount()) return false; beginInsertRows(QModelIndex(), row, row + count - 1); QListWidget *view = ::qobject_cast<QListWidget*>(QObject::parent()); QListWidgetItem *itm = 0; if (row < rowCount()) { for (int r = row; r < row + count; ++r) { itm = new QListWidgetItem; itm->view = view; itm->model = this; lst.insert(r, itm); } } else { for (int r = 0; r < count; ++r) { itm = new QListWidgetItem; itm->view = view; itm->model = this; lst.append(itm); } } endInsertRows(); return true;}bool QListModel::removeRows(int row, int count, const QModelIndex &){ if (count < 1 || row < 0 || (row + count) > rowCount()) return false; if (row >= 0 && row < rowCount()) { beginRemoveRows(QModelIndex(), row, row + count - 1); QListWidgetItem *itm = 0; for (int r = row; r < row + count; ++r) { itm = lst.takeAt(row); itm->view = 0; itm->model = 0; delete itm; } endRemoveRows(); return true; } return false;}Qt::ItemFlags QListModel::flags(const QModelIndex &index) const{ if (!index.isValid() || index.row() >= lst.count()) return Qt::ItemIsDropEnabled; // we allow drops outside the items return lst.at(index.row())->flags();}void QListModel::sort(int column, Qt::SortOrder order){ if (column != 0) return; QVector < QPair<QListWidgetItem*,int> > sorting(lst.count()); for (int i = 0; i < lst.count(); ++i) { sorting[i].first = lst.at(i); sorting[i].second = i; } LessThan compare = (order == Qt::AscendingOrder ? &itemLessThan : &itemGreaterThan); qSort(sorting.begin(), sorting.end(), compare); for (int r = 0; r < sorting.count(); ++r) { QListWidgetItem *item = sorting.at(r).first; lst[r] = item; QModelIndex from = createIndex(sorting.at(r).second, 0, item); QModelIndex to = createIndex(r, 0, item); changePersistentIndex(from, to); } emit layoutChanged();}bool QListModel::itemLessThan(const QPair<QListWidgetItem*,int> &left, const QPair<QListWidgetItem*,int> &right){ return (*left.first) < (*right.first);}bool QListModel::itemGreaterThan(const QPair<QListWidgetItem*,int> &left, const QPair<QListWidgetItem*,int> &right){ return !((*left.first) < (*right.first));}void QListModel::itemChanged(QListWidgetItem *item){ QModelIndex idx = index(item); emit dataChanged(idx, idx);}QStringList QListModel::mimeTypes() const{ const QListWidget *view = ::qobject_cast<const QListWidget*>(QObject::parent()); return view->mimeTypes();}QMimeData *QListModel::internalMimeData() const{ return QAbstractItemModel::mimeData(cachedIndexes);}QMimeData *QListModel::mimeData(const QModelIndexList &indexes) const{ QList<QListWidgetItem*> items; for (int i = 0; i < indexes.count(); ++i) items << at(indexes.at(i).row()); const QListWidget *view = ::qobject_cast<const QListWidget*>(QObject::parent()); cachedIndexes = indexes; QMimeData *mimeData = view->mimeData(items); cachedIndexes.clear(); return mimeData;}#ifndef QT_NO_DRAGANDDROPbool QListModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &index){ Q_UNUSED(column); QListWidget *view = ::qobject_cast<QListWidget*>(QObject::parent()); if (index.isValid()) row = index.row(); else if (row == -1) { row = lst.count(); } return view->dropMimeData(row, data, action);}Qt::DropActions QListModel::supportedDropActions() const{ const QListWidget *view = ::qobject_cast<const QListWidget*>(QObject::parent()); return view->supportedDropActions();}#endif // QT_NO_DRAGANDDROP/*! \class QListWidgetItem \brief The QListWidgetItem class provides an item for use with the QListWidget item view class. \ingroup model-view QListWidgetItem is used to represent items in a list provided by the QListWidget class. Each item can hold several pieces of information, and will display these appropriately. The item view convenience classes use a classic item-based interface rather than a pure model/view approach. For a more flexible list view widget, consider using the QListView class with a standard model. List items can be automatically inserted into a list when they are constructed by specifying the list widget: \quotefile snippets/qlistwidget-using/mainwindow.cpp \skipto new QListWidgetItem(tr("Hazel \printuntil new QListWidgetItem(tr("Hazel They can also be created without a parent widget, and later inserted into a list (see \l{QListWidget::insertItem()}). List items are typically used to display text() and an icon(). These are set with the setText() and setIcon() functions. The appearance of the text can be customized with setFont(), setTextColor(), and setBackgroundColor(). List items can be aligned using the setAlignment() function. Tooltips, status tips and "What's This?" help can be added to list items with setToolTip(), setStatusTip(), and setWhatsThis(). By default, items are enabled, selectable, checkable, and can be the source of a drag and drop operation. Each item's flags can be changed by calling setFlags() with the appropriate value (see \l{Qt::ItemFlags}). Checkable items can be checked and unchecked with the setChecked() function. The corresponding checked() function indicates whether the item is currently checked. The isItemHidden() function can be used to determine whether the item is hidden. Items can be hidden with setItemHidden(). \sa QListWidget, {Model/View Programming}, QTreeWidgetItem, QTableWidgetItem*//*! \variable QListWidgetItem::Type The default type for list widget items. \sa UserType, type()*//*! \variable QListWidgetItem::UserType The minimum value for custom types. Values below UserType are reserved by Qt. \sa Type, type()*//*! \fn int QListWidgetItem::type() const Returns the type passed to the QListWidgetItem constructor.*//*! \fn QListWidget *QListWidgetItem::listWidget() const Returns the list widget that contains the item.*//*! \fn QListWidgetItem::QListWidgetItem(QListWidget *parent, int type) Constructs an empty list widget item of the specified \a type with the given \a parent. If the parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem(). \sa type()*/QListWidgetItem::QListWidgetItem(QListWidget *view, int type) : rtti(type), view(view), model(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled){ if (view) model = ::qobject_cast<QListModel*>(view->model()); if (model) model->insert(model->rowCount(), this);}/*! \fn QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *parent, int type) Constructs an empty list widget item of the specified \a type with the given \a text and \a parent. If the parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem(). \sa type()*/QListWidgetItem::QListWidgetItem(const QString &text, QListWidget *view, int type) : rtti(type), view(view), model(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled){ setData(Qt::DisplayRole, text); if (view) model = ::qobject_cast<QListModel*>(view->model()); if (model) model->insert(model->rowCount(), this);}/*! \fn QListWidgetItem::QListWidgetItem(const QIcon &icon, const QString &text, QListWidget *parent, int type) Constructs an empty list widget item of the specified \a type with the given \a icon, \a text and \a parent. If the parent is not specified, the item will need to be inserted into a list widget with QListWidget::insertItem(). \sa type()*/QListWidgetItem::QListWidgetItem(const QIcon &icon,const QString &text, QListWidget *view, int type) : rtti(type), view(view), model(0), itemFlags(Qt::ItemIsSelectable |Qt::ItemIsUserCheckable |Qt::ItemIsEnabled |Qt::ItemIsDragEnabled){ setData(Qt::DisplayRole, text); setData(Qt::DecorationRole, icon); if (view) model = ::qobject_cast<QListModel*>(view->model()); if (model) model->insert(model->rowCount(), this);}/*! Destroys the list item.*/QListWidgetItem::~QListWidgetItem(){ if (model) model->remove(this);}/*! Creates an exact copy of the item.*/QListWidgetItem *QListWidgetItem::clone() const{ return new QListWidgetItem(*this);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -