(14)本源代码定义于头文件 qtablewidget . h 头文件 :
#ifndef QTABLEWIDGET_H
#define QTABLEWIDGET_H#include <QtWidgets/qtableview.h>
#include <QtWidgets/qtwidgetsglobal.h>
#include <QtCore/qlist.h>
#include <QtCore/qvariant.h>QT_REQUIRE_CONFIG(tablewidget);QT_BEGIN_NAMESPACEclass QTableWidgetSelectionRange
{
public:QTableWidgetSelectionRange() = default;QTableWidgetSelectionRange(int top, int left, int bottom, int right): m_top(top), m_left(left), m_bottom(bottom), m_right(right){}inline int topRow() const { return m_top; }inline int bottomRow() const { return m_bottom; }inline int leftColumn() const { return m_left; }inline int rightColumn() const { return m_right; }inline int rowCount() const { return m_bottom - m_top + 1; }inline int columnCount() const { return m_right - m_left + 1; }
private:int m_top = -1, m_left = -1, m_bottom = -2, m_right = -2;
};class QTableWidget;
class QTableModel;
class QWidgetItemData;
class QTableWidgetItemPrivate;/*
The QTableWidgetItem class provides an item for use with the QTableWidget class.Detailed Description :
表格项用于存储表格小部件的信息。项目通常包含文本、图标或复选框。
QTableWidgetltem类是一个方便的类,它在Qt 3中替换了QTableWidgetltem类。
它提供了一个可以与QTableWidgetltem类一起使用的项目。
顶级项目在没有父项的情况下构建,然后根据行号和列号指定的位置插入:QTableWidgetItem * newItem = new QTableWidgetItem(tr("%1").arg(pow(row, column+1)));tableWidget->setItem(row, column, newItem);每个项目都可以有自己的背景画笔,这通过`setBackground()、函数来设置。
当前的背景画笔可以通过background ()、函数来获取。
每个项目的文本标签可以使用其特定的字体和画笔来呈现,这通过`setFont ()'和setForeground)函数进行指定,
并通过`font()`和`foreground()'函数来读取。默认情况下,项是启用的,可编辑的,可选择的,可检查的,并且既可以作为拖放操作的源,也可以作为落点目标。
每个项的标记可以通过调用setFlags()函数并传入相应的值(见Qt:ItemFlags)来更改。
可检查的项可以通过setCheckState()函数进行检查和取消检查。
对应的checkState()函数则指示该项当前是否被检查过。Subclassing :
当子类化 QTableWidgetltem 以提供自定义项时,可以为它们定义新类型,以便它们可以与标准项区分开来.
需要此功能的子类的构造函数需要使用等于或大于 UserType 的新类型值调用基类构造函数。*/class Q_WIDGETS_EXPORT QTableWidgetItem
{friend class QTableWidget;friend class QTableModel;
private:QTableModel * tableModel() const;private: //可见,表格里的条目还有丰富的数据成员,以描述条目里的数据与属性int rtti ;QList<QWidgetItemData> values ; //包含了条目里的数据QTableWidget * view ; //本条目属于哪个表格窗体。QTableWidgetItemPrivate * d ;Qt::ItemFlags itemFlags; //本条目的编辑属性。public:enum ItemType { Type = 0, UserType = 1000 };//Constructs a table item of the specified type that does not belong to any table.//形参 type会赋值给本类的私有数据成员 rtti,以保存本条目的类型。explicit QTableWidgetItem(int type = Type); //默认类型是 0inline int type() const { return rtti; }//Returns the type passed to the QTableWidgetItem constructor.//Constructs a table item with the given text.explicit QTableWidgetItem(const QString & text, int type = Type); //有参构造函数explicit QTableWidgetItem(const QIcon & icon,const QString & text, int type = Type);QTableWidgetItem(const QTableWidgetItem & other); //copy构造函数QTableWidgetItem & operator=(const QTableWidgetItem & other);//copy赋值运算符函数virtual ~QTableWidgetItem(); //析构函数virtual bool operator< (const QTableWidgetItem & other) const; //无注释virtual QTableWidgetItem * clone() const; //Creates a copy of the item.//Returns the table widget that contains the item.inline QTableWidget * tableWidget() const { return view; }inline int row () const{ return (view ? view->row(this) : -1); }inline int column() const{ return (view ? view->column(this) : -1); }//Returns the row of the item in the table.//If the item is not in a table, this function will return -1.virtual void read (QDataStream & in ) ; //Reads the item from stream in .virtual void write(QDataStream & out) const; //Writes the item to stream out.//Returns true if the item is selected, otherwise returns false.bool isSelected() const;void setSelected(bool select); //Sets the selected state of the item to select./*enum Qt::ItemFlag {NoItemFlags = 0,ItemIsSelectable = 1,ItemIsEditable = 2,ItemIsDragEnabled = 4,ItemIsDropEnabled = 8,ItemIsUserCheckable = 16,ItemIsEnabled = 32,ItemIsAutoTristate = 64,ItemNeverHasChildren = 128,ItemIsUserTristate = 256};Q_DECLARE_FLAGS(ItemFlags, ItemFlag)Q_DECLARE_OPERATORS_FOR_FLAGS(ItemFlags)*/inlineQt::ItemFlags flags() const { return itemFlags; } //返回条目具有的属性void setFlags(Qt::ItemFlags flags);virtual QVariant data(int role) const; //读写条目里具有的数据。virtual void setData(int role, const QVariant &value);//以下依次是读写条目里各角色对应的数据。inline QString text() const //角色 0{ return data(Qt::DisplayRole).toString(); }inline void setText(const QString & text){ setData(Qt::DisplayRole, text); }inline QIcon icon() const //角色 1{ return qvariant_cast<QIcon>(data(Qt::DecorationRole)); }inline void setIcon(const QIcon & icon){ setData(Qt::DecorationRole, icon); }inline QString toolTip() const //角色 3{ return data(Qt::ToolTipRole).toString(); }inline void setToolTip(const QString & toolTip){ setData(Qt::ToolTipRole, toolTip); }inline QString statusTip() const //角色 4{ return data(Qt::StatusTipRole).toString(); }inline void setStatusTip(const QString &statusTip){ setData(Qt::StatusTipRole, statusTip); }inline QString whatsThis() const //角色 5{ return data(Qt::WhatsThisRole).toString(); }inline void setWhatsThis(const QString & whatsThis){ setData(Qt::WhatsThisRole, whatsThis); }inline QFont font() const //角色 6{ return qvariant_cast<QFont>(data(Qt::FontRole)); }inline void setFont(const QFont & font){ setData(Qt::FontRole, font); }inline int textAlignment() const//角色 7{ return data(Qt::TextAlignmentRole).toInt(); }inline void setTextAlignment(int alignment){ setData(Qt::TextAlignmentRole, alignment); }inline QBrush background() const //角色 8{ return qvariant_cast<QBrush>(data(Qt::BackgroundRole)); }inline void setBackground(const QBrush & brush){ setData(Qt::BackgroundRole,brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }//QBrush(Qt::GlobalColor color, Qt::BrushStyle bs = Qt::SolidPattern);inline QBrush foreground() const //角色 9{ return qvariant_cast<QBrush>(data(Qt::ForegroundRole)); }inline void setForeground(const QBrush &brush){ setData(Qt::ForegroundRole,brush.style() != Qt::NoBrush ? QVariant(brush) : QVariant()); }inline //enum Qt::CheckState { Unchecked, PartiallyChecked, Checked };Qt::CheckState checkState() const //角色 10{ return static_cast<Qt::CheckState>(data(Qt::CheckStateRole).toInt()); }inlinevoid setCheckState(Qt::CheckState state){ setData(Qt::CheckStateRole, state); }inline QSize sizeHint() const //角色 13{ return qvariant_cast<QSize>(data(Qt::SizeHintRole)); }inline void setSizeHint(const QSize & size){ setData(Qt::SizeHintRole, size.isValid() ? QVariant(size) : QVariant()); }}; //完结 class QTableWidgetItem#ifndef QT_NO_DATASTREAMQ_WIDGETS_EXPORT QDataStream & operator>>(QDataStream &in , QTableWidgetItem &item);
Q_WIDGETS_EXPORT QDataStream & operator<<(QDataStream &out, const QTableWidgetItem &item);#endifclass QTableWidgetPrivate;/*
The QTableWidget class provides an item-based table view with a default model.Detailed Description :
表格小部件为应用程序提供标准的表格显示功能。QTableWidgetltem中的项目由 QTableWidgetltem 提供。如果你想使用自己的数据模型来创建表格,你应该使用QTableView而不是这个类。表格小部件可以使用所需数量的行和列进行构建:tableWidget = new QTableWidget(12, 3, this);Alternatively, tables can be constructed without a given size and resized later:tableWidget = new QTableWidget(this);tableWidget->setRowCount(10);tableWidget->setColumnCount(5);项目在表之外创建(没有父控件)并使用设置方法插入到表中。QTableWidgetItem * newItem = new QTableWidgetItem(tr("%1").arg((row+1)*(column+1)));tableWidget->setItem(row, column, newItem);如果您想在表格控件中启用排序功能,详细信息,请参阅setltem()方法.
请在填充内容后执行此操作,否则排序可能会影响插入顺序.表格可以同时拥有横向和纵向标题。创建标题的最简单方法是向`setHorizontalHeaderLabels()和
`setVerticalHeaderLabels('函数提供一系列字符串。
这些函数将为表格的列和行提供简单的文本标题。
更复杂的标题可以由现有的表格项生成,而这些表格项通常是在表格之外构建的。
例如,我们可以构建一个带有图标和对齐文本的表格项,并将其用作特定列的标题:QTableWidgetItem *cubesHeaderItem = new QTableWidgetItem(tr("Cubes"));cubesHeaderItem->setIcon(QIcon(QPixmap(":/Images/cubed.png")));cubesHeaderItem->setTextAlignment(Qt::AlignVCenter);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.
表格中的行数可以通过rowCount()找到,列数通过columncount()找到。
表格可以通过clear()函数清空。*/class Q_WIDGETS_EXPORT QTableWidget : public QTableView
{Q_OBJECT//This property holds the number of columns / rows in the table.//By default, for a table constructed without row and column counts,// this property contains a value of 0.Q_PROPERTY(int rowCount READ rowCount WRITE setRowCount)Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)friend class QTableModel; //这是 Qt3 里的老类,已被模型视图架构替代了。注释文档里这么说的。private:void setModel(QAbstractItemModel * model) override; //私有,不再被调用了。Q_DECLARE_PRIVATE(QTableWidget)Q_DISABLE_COPY(QTableWidget)Q_PRIVATE_SLOT(d_func(), void _q_sort())Q_PRIVATE_SLOT(d_func(), void _q_emitItemEntered (const QModelIndex & index))Q_PRIVATE_SLOT(d_func(), void _q_emitItemPressed (const QModelIndex & index))Q_PRIVATE_SLOT(d_func(), void _q_emitItemClicked (const QModelIndex & index))Q_PRIVATE_SLOT(d_func(), void _q_emitItemDoubleClicked (const QModelIndex & index))Q_PRIVATE_SLOT(d_func(), void _q_emitItemActivated (const QModelIndex & index))Q_PRIVATE_SLOT(d_func(), void _q_emitItemChanged (const QModelIndex & index))Q_PRIVATE_SLOT(d_func(), void _q_emitCurrentItemChanged(const QModelIndex & previous,const QModelIndex & current))Q_PRIVATE_SLOT(d_func(), void _q_dataChanged (const QModelIndex & topLeft,const QModelIndex & bottomRight))public://Creates a new table view with the given parent.explicitQTableWidget( QWidget * parent = nullptr); //有参构造函数QTableWidget(int rows, int columns, QWidget * parent = nullptr); //有参构造函数//Creates a new table view with the given rows and columns, and with the given parent.~QTableWidget(); //析构函数//Q_PROPERTY(int rowCount READ rowCount WRITE setRowCount)int rowCount() const;void setRowCount(int rows);//Q_PROPERTY(int columnCount READ columnCount WRITE setColumnCount)int columnCount() const;void setColumnCount(int columns);/* 这是表格窗体视图的基类表格视图里的构建表头的代码方法。
class QTableView : public QAbstractItemView
{
public :QHeaderView * horizontalHeader() const;void setHorizontalHeader(QHeaderView * header);QHeaderView * verticalHeader () const;void setVerticalHeader (QHeaderView * header);
}
*///Returns the horizontal header item for column, column, if one has been set;// otherwise returns nullptr.QTableWidgetItem * horizontalHeaderItem(int column) const;void setHorizontalHeaderItem(int column, QTableWidgetItem * item);//Sets the horizontal header item for column column to item.//If necessary, the column count is increased to fit the item.//The previous header item (if there was one) is deleted.之前的旧标题元素会被删除。QTableWidgetItem * takeHorizontalHeaderItem(int column);//Removes the horizontal header item at column from the header without deleting it.QTableWidgetItem * verticalHeaderItem(int row) const;void setVerticalHeaderItem(int row, QTableWidgetItem * item);QTableWidgetItem * takeVerticalHeaderItem(int row);//Sets the vertical header labels using labels.void setVerticalHeaderLabels(const QStringList & labels);void setHorizontalHeaderLabels(const QStringList & labels);//Returns the item for the given row and column if one has been set;// otherwise returns nullptr.QTableWidgetItem * item(int row, int column) const;void setItem(int row, int column, QTableWidgetItem * item);//Sets the item for the given row and column to item.//表格接管了该项的所有权。//请注意,如果已启用排序(参见`sortingEnabled`),且列是当前排序列,// 则该行将被移动到由`item`确定的已排序位置。//如果您想设置某一行中的多个项(例如,通过循环调用setltem()),可能需要在操作前关闭排序功能,//然后在操作后重新开启;这样您就可以为同一行中的所有项使用相同的行参数(即,setltem()不会移动该行)。QTableWidgetItem * takeItem(int row, int column);//Removes the item at row and column from the table without deleting it.//Returns a list of pointers to the items contained in the data object.//If the object was not created by a QTreeWidget in the same process, the list is empty.QList<QTableWidgetItem *> items(const QMimeData * data) const;//Returns the row / column for the item.int row(const QTableWidgetItem * item) const;int column(const QTableWidgetItem * item) const;//Returns the QModelIndex associated with the given item.条目索引与指针之间的相互转换。QModelIndex indexFromItem (const QTableWidgetItem * item ) const;QTableWidgetItem * itemFromIndex(const QModelIndex & index) const;//Returns a pointer to the QTableWidgetItem associated with the given index.//Returns the row / Column of the current item.int currentRow () const;int currentColumn() const;QTableWidgetItem * currentItem () const;void setCurrentItem(QTableWidgetItem * item); //以代码的方式选择条目。//Sets the current item to item.//Unless the selection mode is NoSelection, the item is also selected.void setCurrentItem(QTableWidgetItem * item,QItemSelectionModel::SelectionFlags command);void setCurrentCell(int row, int column); //直接用行列下标设定条目void setCurrentCell(int row, int column, //这样更好。QItemSelectionModel::SelectionFlags command);bool isSortingEnabled() const; //默认,表格排序是被禁止的。void setSortingEnabled(bool enable); //无注释void sortItems(int column, Qt::SortOrder order = Qt::AscendingOrder);//Sorts all the rows in the table widget based on column and order.//按某列排序,影响的不仅仅本列,而是所有的行。整行都因为某列的排序而移动了。int visualRow (int logicalRow ) const; //返回逻辑索引与视觉索引的对应关系int visualColumn(int logicalColumn) const; //隐藏并不会改变视觉索引。//Returns the widget displayed in the cell in the given row and column.//Note: The table takes ownership of the widget.//测试说明,这个窗体组件,不属于表中元素,不是先天就存在的。未设置前,是 nullptr。QWidget * cellWidget(int row, int column) const;void setCellWidget(int row, int column, QWidget * widget);inline //Removes the widget set on the cell indicated by row and column.void removeCellWidget(int row, int column){ setCellWidget(row, column, nullptr); }QList<QTableWidgetItem *> selectedItems () const; //处理多选用这个就够//Returns a list of all selected ranges.QList<QTableWidgetSelectionRange> selectedRanges() const;void setRangeSelected(const QTableWidgetSelectionRange & range, bool select);//Selects or deselects the range depending on select.//QModelIndex QAbstractItemView::indexAt(const QPoint & point)基类里还有这个成员函数//Returns a pointer to the item at the given point,// or returns nullptr if point is not covered by an item in the table widget.QTableWidgetItem * itemAt(const QPoint & p) const;inlineQTableWidgetItem * itemAt(int x, int y) const //采用了表格自己的坐标系。{ return itemAt(QPoint(x, y)); }//Returns the item at the position equivalent to QPoint(ax, ay) in the// table widget's coordinate system,QList<QTableWidgetItem *> findItems(const QString & text, Qt::MatchFlags flags) const;//Finds items that matches the text using the given flags./*//This enum describes the type of matches that can be// used when searching for items in a model.enum Qt::MatchFlag { //本枚举量用于组合框的条目搜索。本枚举量指定了搜索方式MatchExactly = 0, //Performs QVariant-based matching.MatchContains = 1, //The search term is contained in the item.MatchStartsWith = 2, //The search term matches the start of the item.MatchEndsWith = 3, //The search term matches the end of the item.MatchRegularExpression = 4,MatchWildcard = 5,MatchFixedString = 8,MatchTypeMask = 0x0F,MatchCaseSensitive = 16, //The search is case sensitive.MatchWrap = 32,MatchRecursive = 64 //Searches the entire hierarchy.};Q_DECLARE_FLAGS(MatchFlags, MatchFlag)Q_DECLARE_OPERATORS_FOR_FLAGS(MatchFlags)*/const //Returns the item prototype used by the table.QTableWidgetItem * itemPrototype() const; //似乎在继承 QTableWidgetltem时才用得上void setItemPrototype(const QTableWidgetItem *item);// Sets the item prototype for the table to the specified item.//该表格小部件在需要创建新的表格项时,将使用项原型克隆函数。//例如,当用户在空单元格中进行编辑时。这非常有用,//尤其是在您有 QTableWidgetltem 子类并且希望确保 QTableWidget 创建您的子类实例的情况下。//表格接管原型的所有权。//返回某个单元格的位置信息QRect visualItemRect(const QTableWidgetItem * item) const; //无注释using QAbstractItemView::isPersistentEditorOpen;/*class QAbstractItemView {bool isPersistentEditorOpen(const QModelIndex & index) const;void openPersistentEditor (const QModelIndex & index);void closePersistentEditor (const QModelIndex & index);}*///Returns whether a persistent editor is open for item item.bool isPersistentEditorOpen(QTableWidgetItem * item) const; //函数重载,参数类型不一样//Starts editing the item if it is editable.void editItem (QTableWidgetItem * item); //打开的是普通易失编辑框void openPersistentEditor (QTableWidgetItem * item); //打开持久型编辑框//Opens an editor for the give item. The editor remains open after editing.void closePersistentEditor (QTableWidgetItem * item); //只有调用本函数才可以关闭持久框//Closes the persistent editor for item.protected:bool event(QEvent * e ) override;void dropEvent(QDropEvent * event) override;virtual QStringList mimeTypes() const;virtual QMimeData * mimeData(const QList<QTableWidgetItem *> & items) const;virtual bool dropMimeData(int row, int column,const QMimeData * data, Qt::DropAction action);virtual Qt::DropActions supportedDropActions() const;//Returns the drop actions supported by this view./*enum Qt::DropAction { //本枚举类用于描述模型视图里的拖动操作的语义:复制、剪切或超链接。CopyAction = 0x 1, //Copy the data to the target.MoveAction = 0x 2, //Move the data from the source to the target.LinkAction = 0x 4, //Create a link from the source to the target.ActionMask = 0x ff,TargetMoveAction = 0x8002, //在 Windows上,当 D&D数据的所有权应被目标应用程序接管时,//即源应用程序不应删除这些数据时,会使用此值。//在X11上,此值用于执行移动操作。Mac上不使用TargetMoveAction。IgnoreAction = 0x 0 //Ignore the action (do nothing with the data).};Q_DECLARE_FLAGS(DropActions, DropAction)Q_DECLARE_OPERATORS_FOR_FLAGS(DropActions)*/public Q_SLOTS://Scrolls the view if necessary to ensure that the item is visible.//The hint parameter specifies more precisely where the// item should be located after the operation.void scrollToItem(const QTableWidgetItem * item,QAbstractItemView::ScrollHint hint = EnsureVisible);/*enum QAbstractItemView::ScrollHint {EnsureVisible , //Scroll to ensure that the item is visible.PositionAtTop , //Scroll to position the item at the top of the viewport.PositionAtBottom, //Scroll to position the item at the bottom of the viewport.PositionAtCenter //Scroll to position the item at the center of the viewport.};Q_ENUM(ScrollHint) //滚动屏幕到条目 index处,并指明了 index条目的新的位置virtual void QAbstractItemView::scrollTo(const QModelIndex & index,ScrollHint hint = EnsureVisible) = 0;*///Inserts an empty row into the table at row.void insertRow (int row ); //在行 row ,列 column处插入一行或一列。void insertColumn(int column);void removeRow(int row);void removeColumn(int column); //删除第 row行或第 column列。//Removes the column column and all its items from the table.//Removes all items in the view. This will also remove all selections and headers.//If you don't want to remove the headers, use QTableWidget::clearContents().//The table dimensions stay the same.void clear (); //行列框架 还会保存,表头数据没有了,改成了123数字void clearContents(); //行列框架和表头数据都会保存,只清除表体中的数据,置为空。//Removes all items not in the headers from the view.//This will also remove all selections. The table dimensions stay the same.Q_SIGNALS:void itemEntered(QTableWidgetItem * item);void cellEntered(int row, int column);void itemPressed(QTableWidgetItem * item);void cellPressed(int row, int column);void itemClicked(QTableWidgetItem * item);void cellClicked(int row, int column);//This signal is emitted when the specified item has been activatedvoid itemActivated(QTableWidgetItem * item);void cellActivated(int row, int column);void itemDoubleClicked(QTableWidgetItem * item);void cellDoubleClicked(int row, int column);//This signal is emitted whenever the data of item has changed.void itemChanged(QTableWidgetItem * item); //当条目里的数据改变时,触发本信号void cellChanged(int row, int column);//This signal is emitted whenever the current item changes.//The previous item is the item that previously had the focus,// current is the new current item. //当焦点切换时触发本信号函数。// 经测试发现,本信号函数里的 previous 形参不要使用。否则程序会闪退。void currentItemChanged(QTableWidgetItem * current, QTableWidgetItem * previous);void currentCellChanged(int currentRow, int currentColumn,int previousRow, int previousColumn);void itemSelectionChanged(); //当焦点选择变化时触发本信号,但没有形参。//This signal is emitted whenever the selection changes.}; //完结 class QTableWidget : public QTableViewQT_END_NAMESPACE#endif // QTABLEWIDGET_H
(15)
谢谢