样式
ui->tableView->setStyleSheet("QTableView {qproperty-showGrid:true;}"); // 显示网格线
ui->tableView->setShowGrid(true); // 隐藏网格线
ui->tableView->setGridStyle(Qt::SolidLine); // 网格线的样式
ui->tableView->setAlternatingRowColors(true); // 隔行变色
table->setFrameShape(QFrame::NoFrame); //设置无边框
table->verticalHeader()->setVisible(false); //设置垂直头不可见,即左边序号不见
table->setStyleSheet("selection-background-color:rgb(34, 170, 75);"); //设置选中行的背景色
table->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); //设置表头的背景色
// 设置水平滚动条样式
table->horizontalScrollBar()->setStyleSheet("QScrollBar{background:transparent; height:12px;}"
"QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
// 设置垂直滚动条的样式
table->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent; width: 12px;}"
"QScrollBar::handle{background:lightgray; border:2px solid transparent; border-radius:5px;}"
"QScrollBar::handle:hover{background:gray;}"
"QScrollBar::sub-line{background:transparent;}"
"QScrollBar::add-line{background:transparent;}");
表头
QStringList header; //QString类型的List容器
header<<"学号"<<"姓名"<<"性别";//设置内容是学号、姓名、性别
ui->tableView->setHorizontalHeaderLabels(header);//设置表头内容
// 字体
QFont font ;//定义一个字体变量
font.setBold(true); //设置粗体
ui->tableView->horizontalHeader()->setFont(font);//把字体变量属性加进表头中
ui->tableView->horizontalHeader()->setFixedHeight(25); //设置表头的高度为25
ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignCenter); // 文本居中
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Interactive); //根据内容自动调整列宽
ui->tableView->horizontalHeader()->setStretchLastSection(true); // 最后一列拉伸
ui->tableView->horizontalHeader()->setVisible(false); // 设置水平表头不可见
ui->tableView->verticalHeader()->setVisible(false); // 设置垂直表头不可见
ui->tableView->verticalHeader()->setStretchLastSection(true); // 最后一行拉伸
行列
ui->tableView->setColumnCount(3);//设置3列
ui->tableView->setRowCount(5);//设置5行
ui->tableView->horizontalHeader()->setDefaultSectionSize(15); // 可以设置tableview所有行的默认列宽为15。
ui->tableView->verticalHeader()->setDefaultSectionSize(15); // 可以设置tableview所有列的默认行高为15。
// 列的宽度 / 行的高度 单行单列操作
void setColumnWidth(int column, int width) / void setRowHeight(int row, int height)
int columnWidth(int column) / int rowHeight(int row)
// 选中模式-单行选中
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);// 设置选中模式为选中行
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);// 设置选中单个
// 选中模式-多行选中
ui->tableView->etSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::ExtendedSelection);
// 选择列/行
void selectColumn(int column) / void selectRow(int row)
// 隐藏、显示列/行
void hideColumn(int column) / void hideRow(int row)
void showColumn(int column) / void showRow(int row)
bool isColumnHidden(int column) / bool isRowHidden(int row)
void setColumnHidden(int column, bool hide) / void setRowHidden(int row, bool hide)
表项
// 设为不可编辑
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
// 自动换行
ui->tableView->setWordWrap(true);
//创建视图控件对应的模型
QStandardItemModel *model = new QStandardItemModel(ui->tableView);
//将模型添加到视图控件中
ui->tableView->setModel(model);
for(int row = 0; row != 3; ++row) {
for(int colum = 0; colum != 3; ++colum) {
//循环添加view中的数据
model->setItem(row, colum, new QStandardItem(QString("Row:%1, Colum:%2").arg(row).arg(colum)));
model->item(row,colum)->setForeground(QBrush(QColor(255, 0, 0)));//设置字符颜色
model->item(row,colum)->setTextAlignment(Qt::AlignCenter);//设置字符位置
}
}
//插入自定义checkbox
QWidget *widget = new QWidget(this);
QCheckBox *checkbox = new QCheckBox();
QHBoxLayout *hLayout = new QHBoxLayout();
hLayout->addWidget(checkbox);
hLayout->setMargin(0); // 必须添加, 否则CheckBox不能正常显示
hLayout->setAlignment(checkbox, Qt::AlignCenter); // 居中显示复选框
checkbox->setProperty("isSelect", i+1);
widget->setLayout(hLayout);
ui->tableView->setIndexWidget(model->index(i, 0), widget);
// 或者
ui->tableView->setCellWidget (1,2,dateTimeEdit);//加到第二行第三列中
connect(checkbox, SIGNAL(stateChanged(int)), this, SLOT(onCheckboxStateChanged(int)));
代理相关
// 设置代理
m_colorDelegate = new ColorDelegate(this);
ui->tableView->setItemDelegateForColumn(column, m_colorDelegate);
// 单击进入编辑代理,代理是双击左键激活编辑的,可以设置单击激活
ui->tableView->setEditTriggers(QAbstractItemView::CurrentChanged | QAbstractItemView::DoubleClicked);
ui->tableView->setMouseTracking(true);
// 代理保持编辑状态
ui->tableView->openPersistentEditor(m_view->model()->index(0,1));
// 根据用于呈现列中每个项目的委托的大小提示调整列/行的大小
void resizeColumnToContents(int column) / void resizeColumnsToContents()
void resizeRowToContents(int row) / void resizeRowsToContents()
获取选中行列
// 获取选中行
QItemSelectionModel *select = m_view->selectionModel(); // 获取选择模型的指针
QModelIndexList list = select->selectedRows(); // 获取被选中的指针列表
for (auto index : list) {
qDebug()<<index.row();
}
//
QItemSelectionModel *select = m_view->selectionModel();
select->hasSelection(); //是否选中
ui->tableView->selectionModel()->currentIndex(); // 获取当前选中项
select ->selectedIndexes(); // 获取所有选中项
select->selectedRows(); // 获取所有选中行项
select->selectedColumns(); // 获取所有选中列项
清空选中项
m_view->clearSelection();
信号
void clicked(const QModelIndex &index) // 单击
void doubleClicked(const QModelIndex &index) // 双击
void pressed(const QModelIndex &index) // 按下
void entered(const QModelIndex &index)
QItemSelectionModel 信号
QItemSelectionModel *select = m_view->selectionModel()获取选中数据,此类的信号有:
连接QItemSelectionModel的信号必须setModel之后,否则QTableView::selectionModel()返回值为NULL
void currentChanged(const QModelIndex ¤t, const QModelIndex &previous)
void currentColumnChanged(const QModelIndex ¤t, const QModelIndex &previous)
void currentRowChanged(const QModelIndex ¤t, const QModelIndex &previous)
void selectionChanged(const QItemSelection &selected, const QItemSelection &deselected)
接收鼠标的移动事件
setMouseTracking(true);
设置QTableView可以随时接收鼠标的移动事件
connect(this, SIGNAL(entered(const QModelIndex &)), this,SLOT(showToolTip(const QModelIndex &)));
右键菜单
当在函数中并未使用setContextMenuPolicy函数设置对应值时,右键操作通常会触发contextMenuEvent事件;
反之使用setContextMenuPolicy函数将值设置为Qt::CustomContextMenu时,才会发出对应的信号。
// 调用setContextMenuPolicy并设置其值(如果不添加这行,对应的控件则不会发出对应的信号)
m_view->setContextMenuPolicy(Qt::CustomContextMenu);
connect(m_view, &QTableView::customContextMenuRequested, this, &MyWidget::RightMenu);
void MyWidget::RightMenu(const QPoint &pos)
{
//此处的pos函数可以使用QTableView的indexAt()函数获取当前鼠标下控件的QModelIndex对象
Q_UNUSED(pos);
QMenu* popMenu = new QMenu(this);
QAct* action = new QAct("菜单", popMenu);
popMenu->addAction(action);
//显示menu菜单并设置其显示位置为鼠标位置
popMenu->exec(QCursor::pos());
}