📄 qtablewidget.cpp
字号:
if (QTableModel::canConvertToDouble(v1) && QTableModel::canConvertToDouble(v2)) return v1.toDouble() < v2.toDouble(); return v1.toString() < v2.toString();}#ifndef QT_NO_DATASTREAM/*! Reads the item from stream \a in. \sa write()*/void QTableWidgetItem::read(QDataStream &in){ in >> values;}/*! Writes the item to stream \a out. \sa read()*/void QTableWidgetItem::write(QDataStream &out) const{ out << values;}/*! \relates QTableWidgetItem Reads a table widget item from stream \a in into \a item. This operator uses QTableWidgetItem::read(). \sa {Format of the QDataStream Operators}*/QDataStream &operator>>(QDataStream &in, QTableWidgetItem &item){ item.read(in); return in;}/*! \relates QTableWidgetItem Writes the table widget item \a item to stream \a out. This operator uses QTableWidgetItem::write(). \sa {Format of the QDataStream Operators}*/QDataStream &operator<<(QDataStream &out, const QTableWidgetItem &item){ item.write(out); return out;}#endif // QT_NO_DATASTREAM/*! \since 4.1 Constructs a copy of \a other. Note that type() and tableWidget() are not copied. This function is useful when reimplementing clone(). \sa data(), flags()*/QTableWidgetItem::QTableWidgetItem(const QTableWidgetItem &other) : rtti(Type), values(other.values), view(0), d(new QTableWidgetItemPrivate(this)), itemFlags(other.itemFlags){}/*! Assigns \a other's data and flags to this item. Note that type() and tableWidget() are not copied. This function is useful when reimplementing clone(). \sa data(), flags()*/QTableWidgetItem &QTableWidgetItem::operator=(const QTableWidgetItem &other){ values = other.values; itemFlags = other.itemFlags; return *this;}/*! \class QTableWidget \brief The QTableWidget class provides an item-based table view with a default model. \ingroup model-view \mainclass Table widgets provide standard table display facilities for applications. The items in a QTableWidget are provided by QTableWidgetItem. If you want a table that uses your own data model you should use QTableView rather than this class. Table widgets can be constructed with the required numbers of rows and columns: \quotefile snippets/qtablewidget-using/mainwindow.cpp \skipto tableWidget = new \printuntil tableWidget = new Alternatively, tables can be constructed without a given size and resized later: \quotefile snippets/qtablewidget-resizing/mainwindow.cpp \skipto tableWidget = new \printuntil tableWidget = new \skipto tableWidget->setRowCount( \printuntil tableWidget->setColumnCount( Items are created ouside the table (with no parent widget) and inserted into the table with setItem(): \skipto QTableWidgetItem *newItem \printuntil tableWidget->setItem( If you want to enable sorting in your table widget, do so after you have populated it with items, otherwise sorting may interfere with the insertion order (see setItem() for details). Tables can be given both horizontal and vertical headers. The simplest way to create the headers is to supply a list of strings to the setHorizontalHeaderLabels() and setVerticalHeaderLabels() functions. These will provide simple textual headers for the table's columns and rows. More sophisticated headers can be created from existing table items that are usually constructed outside the table. For example, we can construct a table item with an icon and aligned text, and use it as the header for a particular column: \quotefile snippets/qtablewidget-using/mainwindow.cpp \skipto QTableWidgetItem *cubesHeaderItem \printuntil cubesHeaderItem->setTextAlignment The number of rows in the table can be found with rowCount(), and the number of columns with columnCount(). The table can be cleared with the clear() function. \table 100% \row \o \inlineimage windowsxp-tableview.png Screenshot of a Windows XP style table widget \o \inlineimage macintosh-tableview.png Screenshot of a Macintosh style table widget \o \inlineimage plastique-tableview.png Screenshot of a Plastique style table widget \row \o A \l{Windows XP Style Widget Gallery}{Windows XP style} table widget. \o A \l{Macintosh Style Widget Gallery}{Macintosh style} table widget. \o A \l{Plastique Style Widget Gallery}{Plastique style} table widget. \endtable \sa QTableWidgetItem, QTableView, {Model/View Programming}*//*! \property QTableWidget::rowCount \brief the number of rows in the table*//*! \property QTableWidget::columnCount \brief the number of columns in the table*/void QTableWidgetPrivate::setup(){ Q_Q(QTableWidget); // 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())); // sorting 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 QTableWidgetPrivate::_q_emitItemPressed(const QModelIndex &index){ Q_Q(QTableWidget); if (QTableWidgetItem *item = model()->item(index)) emit q->itemPressed(item); emit q->cellPressed(index.row(), index.column());}void QTableWidgetPrivate::_q_emitItemClicked(const QModelIndex &index){ Q_Q(QTableWidget); if (QTableWidgetItem *item = model()->item(index)) emit q->itemClicked(item); emit q->cellClicked(index.row(), index.column());}void QTableWidgetPrivate::_q_emitItemDoubleClicked(const QModelIndex &index){ Q_Q(QTableWidget); if (QTableWidgetItem *item = model()->item(index)) emit q->itemDoubleClicked(item); emit q->cellDoubleClicked(index.row(), index.column());}void QTableWidgetPrivate::_q_emitItemActivated(const QModelIndex &index){ Q_Q(QTableWidget); if (QTableWidgetItem *item = model()->item(index)) emit q->itemActivated(item); emit q->cellActivated(index.row(), index.column());}void QTableWidgetPrivate::_q_emitItemEntered(const QModelIndex &index){ Q_Q(QTableWidget); if (QTableWidgetItem *item = model()->item(index)) emit q->itemEntered(item); emit q->cellEntered(index.row(), index.column());}void QTableWidgetPrivate::_q_emitItemChanged(const QModelIndex &index){ Q_Q(QTableWidget); if (QTableWidgetItem *item = model()->item(index)) emit q->itemChanged(item); emit q->cellChanged(index.row(), index.column());}void QTableWidgetPrivate::_q_emitCurrentItemChanged(const QModelIndex ¤t, const QModelIndex &previous){ Q_Q(QTableWidget); QTableWidgetItem *currentItem = model()->item(current); QTableWidgetItem *previousItem = model()->item(previous); if (currentItem || previousItem) emit q->currentItemChanged(currentItem, previousItem); emit q->currentCellChanged(current.row(), current.column(), previous.row(), previous.column());}void QTableWidgetPrivate::_q_sort(){ Q_Q(QTableWidget); if (sortingEnabled) { int column = q->horizontalHeader()->sortIndicatorSection(); Qt::SortOrder order = q->horizontalHeader()->sortIndicatorOrder(); model()->sort(column, order); }}void QTableWidgetPrivate::_q_dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight){ Q_Q(QTableWidget); if (sortingEnabled && topLeft.isValid() && bottomRight.isValid()) { int column = q->horizontalHeader()->sortIndicatorSection(); if (column >= topLeft.column() && column <= bottomRight.column()) { Qt::SortOrder order = q->horizontalHeader()->sortIndicatorOrder(); model()->ensureSorted(column, order, topLeft.row(), bottomRight.row()); } }}/*! \fn void QTableWidget::itemPressed(QTableWidgetItem *item) This signal is emitted whenever an item in the table is pressed. The \a item specified is the item that was pressed.*//*! \fn void QTableWidget::itemClicked(QTableWidgetItem *item) This signal is emitted whenever an item in the table is clicked. The \a item specified is the item that was clicked.*//*! \fn void QTableWidget::itemDoubleClicked(QTableWidgetItem *item) This signal is emitted whenever an item in the table is double clicked. The \a item specified is the item that was double clicked.*//*! \fn void QTableWidget::itemActivated(QTableWidgetItem *item) This signal is emitted when the specified \a item has been activated*//*! \fn void QTableWidget::itemEntered(QTableWidgetItem *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 QTableWidget::itemChanged(QTableWidgetItem *item) This signal is emitted whenever the data of \a item has changed.*//*! \fn void QTableWidget::currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *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 QTableWidget::itemSelectionChanged() This signal is emitted whenever the selection changes. \sa selectedItems() isItemSelected()*//*! \since 4.1 \fn void QTableWidget::cellPressed(int row, int column) This signal is emitted whenever a cell the table is pressed. The \a row and \a column specified is the cell that was pressed.*//*! \since 4.1 \fn void QTableWidget::cellClicked(int row, int column) This signal is emitted whenever a cell in the table is clicked. The \a row and \a column specified is the cell that was clicked.*//*! \since 4.1 \fn void QTableWidget::cellDoubleClicked(int row, int column) This signal is emitted whenever a cell in the table is double clicked. The \a row and \a column specified is the cell that was double clicked.*//*! \since 4.1 \fn void QTableWidget::cellActivated(int row, int column) This signal is emitted when the cell specified by \a row and \a column has been activated*//*! \since 4.1 \fn void QTableWidget::cellEntered(int row, int column) This signal is emitted when the mouse cursor enters a cell. The cell is specified by \a row and \a column. This signal is only emitted when mouseTracking is turned on, or when a mouse button is pressed while moving into an item.*//*! \since 4.1 \fn void QTableWidget::cellChanged(int row, int column) This signal is emitted whenever the data of the item in the cell specified by \a row and \a column has changed.*//*! \since 4.1 \fn void QTableWidget::currentCellChanged(int currentRow, int currentColumn, int previousRow, int previousColumn) This signal is emitted whenever the current cell changes. The cell specified by \a previousRow and \a previousColumn is the cell that previously had the focus, the cell specified by \a currentRow and \a currentColumn is the new current cell.*//*! \since 4.3 \fn void QTableWidget::removeCellWidget(int row, int column) Removes the widget set on the cell indicated by \a row and \a column.*//*! \fn QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const Returns the item at the position equivalent to QPoint(\a{ax}, \a{ay}) in the table widget's coordinate system, or returns 0 if the specified point is not covered by an item in the table widget. \sa item()*//*! \enum QTableWidgetItem::ItemType This enum describes the types that are used to describe table widget items. \value Type The default type for table widget items. \value UserType The minimum value for custom types. Values below UserType are reserved by Qt. You can define new user types in QTableWidgetItem subclasses to ensure that custom items are treated specially. \sa type()*//*! \fn int QTableWidgetItem::type() const Returns the type passed to the QTableWidgetItem constructor.*//*! Creates a new table view with the given \a parent.*/QTableWidget::QTableWidget(QWidget *parent) : QTableView(*new QTableWidgetPrivate, parent){ Q_D(QTableWidget); QTableView::setModel(new QTableModel(0, 0, this)); d->setup();}/*! Creates a new table view with the given \a rows and \a columns, and with the given \a parent.*/QTableWidget::QTableWidget(int rows, int columns, QWidget *parent) : QTableView(*new QTableWidgetPrivate, parent){ Q_D(QTableWidget); QTableView::setModel(new QTableModel(rows, columns, this)); d->setup();}/*! Destroys this QTableWidget.*/QTableWidget::~QTableWidget(){}/*! Sets the number of rows in this table's model to \a rows. If this is less than rowCount(), the data in the unwanted rows
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -