属性和方法
QTableWidget
有很多属性和方法,完整的可查看帮助文档。
在窗口上放置一个 QTableWidget
控件后,既可以在设计师 UI
界面来编辑属性和添加数据,也可以在代码中动态地设置
这里列出常用的属性和方法
行列数目、行表头、列表头
设置和获取行列的数目
// 获取/设置行的数目 int rowCount() const void setRowCount(int rows) // 获取/设置列的数目 int columnCount() const void setColumnCount(int columns)
设置行列表头
// 设置行表头 void setHorizontalHeaderLabels(const QStringList &labels) // 设置列表头 - 通常不设置,则默认为行号 void setVerticalHeaderLabels(const QStringList &labels)
设置列的宽度
// 获取行表头 QHeaderView *horizontalHeader() const // 设置列的宽度 void QHeaderView::setSectionResizeMode(QHeaderView::ResizeMode mode)
其中 ResizeMode
是一个枚举,取值如下:
- HeaderView::Interactive 0 用户可拖动改变列宽
- QHeaderView::Fixed 2 固定列宽
- QHeaderView::Stretch 1 拉伸自适应列宽大小
- QHeaderView::ResizeToContents 3 根据内容设置列宽
通常,先整体设置为 QHeaderView::Stretch
, 然后根据需要对单独的列进行设置,如下:
// 1、先设置自适应宽度,再单独设置某一列的宽度规则 ui->twStudent->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); //先自适应宽度 // 2、然后,单独设置某一列根据内容调整宽度,或者单独设置某一列为固定宽度 // ui->twStudent->horizontalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents); ui->twStudent->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); ui->twStudent->setColumnWidth(0, 80);
单元格
每个网格单元称为一个单元格。每个单元格有一个行号、列号。
在 QTableWidget
表格控件中,每一个单元格是一个 QTableWidgetItem
对象,可以设置其文字内容等。
获取和设置单元格
// 获取和设置指定行列位置的单元格 QTableWidgetItem *item(int row, int column) const; void setItem(int row, int column, QTableWidgetItem *item) // 构造 QTableWidgetItem QTableWidgetItem(const QIcon &icon, const QString &text, int type = Type) QTableWidgetItem(const QString &text, int type = Type)
单元格文本对齐方式
// 获取和设置单元格文本的对齐方式 int textAlignment() const void setTextAlignment(int alignment)
参数 alignment 是一个枚举类型,常用取值如下:
Qt::AlignLeft (0x0001) 水平方向-左对齐
Qt::AlignRight (0x0002) 水平方向-右对齐
Qt::AlignHCenter (0x0004) 水平方向-居中对齐
Qt::AlignTop (0x0020) 垂直方向-上对齐
Qt::AlignBottom (0x0040) 垂直方向-下对齐
Qt::AlignVCenter (0x0080) 垂直方向-居中对齐
Qt::AlignCenter (AlignVCenter | AlignHCenter) 垂直方向和水平方向-居中对齐
上面的每一个宏,都代表 16 进制中的一位,可以进行或(|)操作,来同时设置多个对齐方式。
单元格是否可编辑
// 获取和设置单元格是否可编辑 QAbstractItemView::EditTriggers editTriggers() const void setEditTriggers(QAbstractItemView::EditTriggers triggers)
这是继承自其父类 QAbstractItemView 中的方法
其中,QAbstractItemView::EditTriggers 是一个枚举,常用取值如下:
- AbstractItemView::NoEditTriggers 0 不可编辑
- QAbstractItemView::CurrentChanged 1 当切换单元格时
- QAbstractItemView::DoubleClicked 2 当双击单元格时
- QAbstractItemView::SelectedClicked 4 当单击一个已选中的单元格时
- QAbstractItemView::EditKeyPressed 8 当一个单元格获取焦点,按编辑按键时(F2)
- QAbstractItemView::AnyKeyPressed 16 当一个单元格获取焦点,按任意键时
- QAbstractItemView::AllEditTriggers 31 以上所有条件的组合。(31 = 1|2|4|8|16)
隔行交替背景色
如下的奇数行和偶数行,它们的背景色不同,便于用户浏览
// 获取和设置是否允许隔行交替背景色 bool alternatingRowColors() const void setAlternatingRowColors(bool enable)
选择模式、选择行为
所谓选择行为,是指当点击一个单元格时,是选中该单元格,还是选中一整行
// 获取和设置选择行为 QAbstractItemView::SelectionBehavior selectionBehavior() const void setSelectionBehavior(QAbstractItemView::SelectionBehavior behavior)
这是继承自其父类 QAbstractItemView 中的方法
其中 QAbstractItemView::SelectionBehavior 是一个枚举,取值为:
QAbstractItemView::SelectItems 0 选中单元格
QAbstractItemView::SelectRows 1 选中单元格所在行
QAbstractItemView::SelectColumns 2 选中单元格所在列
所谓选择模式,是指设置表格控件只可选择单行、可选择多行等。
// 获取和设置选择模式 QAbstractItemView::SelectionMode selectionMode() const void setSelectionMode(QAbstractItemView::SelectionMode mode)
这是继承自其父类 QAbstractItemView 中的方法
其中 QAbstractItemView::SelectionMode 是一个枚举,取值为:
- QAbstractItemView::NoSelection 0 不可选择
- QAbstractItemView::SingleSelection 1 单行选择,一次只允许选择一行
- QAbstractItemView::MultiSelection 2 多行选择,鼠标单击就可以选择多行
- QAbstractItemView::ExtendedSelection 3 扩展选择,按shift键选中一个范围内的行,ctrl键可以选中不相邻的行
- QAbstractItemView::ContiguousSelection 4 相邻选择,按shift键或ctrl键都可以选中一个范围内的行
设置样式表
void Widget::on_btnStyleSheet_clicked() { QString cellStyle = R"( QTableView { text-align:center; background-color: rgba(255, 255, 255, 0); alternate-background-color:#e3edf9; font:14px "微软雅黑"; color:#677483; gridline-color: #ccddf0; } )"; const QString horizontalHeaderStyle = R"( QHeaderView::section { color: black; font:bold 14px "微软雅黑"; text-align:center; height:32px; background-color: #d1dff0; border:1px solid #8faac9; border-left:none; } )"; const QString verticalHeaderStyle = R"( QHeaderView::section { color: black; font:bold 14px "微软雅黑"; width:60px; text-align:center; background-color: #d1dff0; border:1px solid #8faac9; border-left:none; } )"; ui->twStudent->setStyleSheet(cellStyle); ui->twStudent->horizontalHeader()->setStyleSheet(horizontalHeaderStyle); ui->twStudent->verticalHeader()->setStyleSheet(verticalHeaderStyle); }