The settings of QTableView are relatively complicated, and I always forget them when working on projects. This article will record its style, layout and other settings.
Table of contents
1. Set item layout
1.1. Average coverage
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
1.2. The last item is full
ui->tableView->horizontalHeader()->setStretchLastSection(true);
It can also be set by the UI designer.
2. Alternate background color
2.1. Alternate rows
ui->tableView->setAlternatingRowColors(true);
Alternate colors can be set via stylesheets.
QTableView::item:!alternate:!selected{
background: yellow;
}
QTableView::item:alternate:!selected{
background: green;
}
2.2. Item alternation
Qt does not provide a method for item background color alternation. You need to implement it yourself. Here is an example.
auto model = new QStandardItemModel;
model->setRowCount(3);
model->setColumnCount(3);
for (int r = 0; r < 3; ++r) {
for (int c = 0; c < 3; ++c) {
auto index = model->index(r, c);
if ((!(r % 2) && c % 2) ||
(r % 2 && !(c % 2))) {
model->setData(index, QColor(243, 243, 243), Qt::BackgroundRole);
} else {
model->setData(index, QColor(Qt::white), Qt::BackgroundRole);
}
model->setData(index, "aaa");
}
}
ui->tableView->setModel(model);
3. Remove the selected dotted line
ui->tableView->setFocusPolicy(Qt::NoFocus);
4. Remove grid lines
ui->tableView->setShowGrid(false);
5. Set the selection method
5.1. Select a single item
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
5.2. Select a single row
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
5.3. Select a single column
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
6. Style settings
For easy identification, use some simple and conspicuous colors to write examples.
QTableView {
border: 1px solid red;
background: black;
color: white;
font-size:18px;
}
QTableView::item {
border: 2px solid blue;
}
QTableView::item:selected {
background-color: green;
color: yellow;
}
7. Text is centered
Text centering requires setting items one by one.
model->setData(index, Qt::AlignCenter, Qt::TextAlignmentRole);
8. Set icon
Icons need to be set item by item.
model->setData(index, QIcon("a.png"), Qt::DecorationRole);
9. Header style setting
QHeaderView {
height: 40px;
}
QHeaderView::section {
/* 表头的项 */
background: red;
border: none;
}
9. Summary
Finally, paste these commonly used settings together for easy copying and use.
ui->tableView->setFocusPolicy(Qt::NoFocus);
ui->tableView->setSelectionBehavior(QAbstractItemView::SelectItems);
ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
ui->tableView->setShowGrid(false);
ui->tableView->setAlternatingRowColors(true);
ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// ui->tableView->horizontalHeader()->setStretchLastSection(true);
ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);