📄 qtablewidget.cpp
字号:
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), model(0), 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( 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*//*! \property QTableWidget::sortingEnabled \brief whether the items in the table can be sorted by clicking on the horizontal header.*/class QTableWidgetPrivate : public QTableViewPrivate{ Q_DECLARE_PUBLIC(QTableWidget)public: QTableWidgetPrivate() : QTableViewPrivate(), sortingEnabled(false) {} inline QTableModel *model() const { return ::qobject_cast<QTableModel*>(q_func()->model()); } void setup(); // view signals 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); // model signals void _q_emitItemChanged(const QModelIndex &index); // selection signals void _q_emitCurrentItemChanged(const QModelIndex &previous, const QModelIndex ¤t); // data bool sortingEnabled;};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()));}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);}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());}/*! \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 specidied 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.*//*! \fn QTableWidgetItem *QTableWidget::itemAt(int ax, int ay) const Returns the item at the position (\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()*//*! \variable QTableWidgetItem::Type The default type for table widget items. \sa UserType, type()*//*! \variable QTableWidgetItem::UserType The minimum value for custom types. Values below UserType are reserved by Qt. \sa Type, 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 is discarded. \sa setColumnCount()*/void QTableWidget::setRowCount(int rows){ Q_D(QTableWidget); if (rows < 0) return; d->model()->setRowCount(rows);}/*! Returns the number of rows.*/int QTableWidget::rowCount() const{ Q_D(const QTableWidget); return d->model()->rowCount();}/*! Sets the number of columns in this table's model to \a columns. If this is less than columnCount(), the data in the unwanted columns is discarded. \sa setRowCount()*/void QTableWidget::setColumnCount(int columns){ Q_D(QTableWidget); if (columns < 0) return; d->model()->setColumnCount(columns);}/*! Returns the number of columns.*/int QTableWidget::columnCount() const{ Q_D(const QTableWidget); return d->model()->columnCount();}/*! Returns the row for the \a item.*/int QTableWidget::row(const QTableWidgetItem *item) const{ Q_ASSERT(item); Q_D(const QTableWidget); return d->model()->index(item).row();}/*! Returns the column for the \a item.*/int QTableWidget::column(const QTableWidgetItem *item) const{ Q_ASSERT(item); Q_D(const QTableWidget); return d->model()->index(item).column();}/*! Returns the item for the given \a row and \a column if one has been set; otherwise returns 0. \sa setItem()*/QTableWidgetItem *QTableWidget::item(int row, int column) const{ Q_D(const QTableWidget); return d->model()->item(row, column);}/*! Sets the item for the given \a row and \a column to \a item. The table takes ownership of the item. \sa item() takeItem()*/void QTableWidget::setItem(int row, int column, QTableWidgetItem *item){ Q_D(QTableWidget); if (item) { item->view = this; d->model()->setItem(row, column, item); } else { delete takeItem(row, column); }}/*! Removes the item at \a row and \a column from the table without deleting it.*/QTableWidgetItem *QTableWidget::takeItem(int row, int column){ Q_D(QTableWidget); QTableWidgetItem *item = d->model()->takeItem(row, column); if (item) item->view = 0; return item;}/*! Returns the vertical header item for row \a row.*/QTableWidgetItem *QTableWidget::verticalHeaderItem(int row) const{ Q_D(const QTableWidget); return d->model()->verticalHeaderItem(row);}/*! Sets the vertical header item for row \a row to \a item.*/void QTableWidget::setVerticalHeaderItem(int row, QTableWidgetItem *item){ Q_D(QTableWidget); if (item) { item->view = this; item->itemFlags = Qt::ItemFlags(int(item->itemFlags) | QTableModel::ItemIsVerticalHeaderItem); d->model()->setVerticalHeaderItem(row, item); } else { delete takeVerticalHeaderItem(row); }}/*! \since 4.1 Removes the vertical header item at \a row from the header without deleting it.*/QTableWidgetItem *QTableWidget::takeVerticalHeaderItem(int row){ Q_D(QTableWidget); QTableWidgetItem *itm = d->model()->takeVerticalHeaderItem(row); if (itm) { itm->view = 0; itm->itemFlags &= ~QTableModel::ItemIsVerticalHeaderItem; } return itm;}/*! Returns the horizontal header item for column \a column.*/QTableWidgetItem *QTableWidget::horizontalHeaderItem(int column) const{ Q_D(const QTableWidget); return d->model()->horizontalHeaderItem(column);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -