The style of QTableWidget is divided into several parts:
outer
frame: QTableWidget
header: QHeaderView
header field: QHeaderView::section
table: QTableWidget::item
selected table: QTableWidget::item::selected
horizontal scroll bar: QTableWidget QScrollBar::horizontal
Vertical scroll bar: QTableWidget QScrollBar::vertical
Horizontal scroll bar's slider: QTableWidget QScrollBar::handle:horizontal
Horizontal scroll bar's slider: QTableWidget QScrollBar::handle:vertical
Horizontal scroll bar's slider suspension: QScrollBar ::handle:vertical:horizontal
The slider block of the vertical scroll bar is suspended: QScrollBar::handle:vertical:hover The
upper edge block of the slider: QTableWidget QScrollBar::add-line
The lower edge block of the slider: QTableWidget QScrollBar::sub-
The upper slide of the line slider: QTableWidget QScrollBar::add-page
The lower slide of the line slider: QTableWidget QScrollBar::sub-page
Example
QTableWidget
{
color:white;
border-top:1px solid white;
gridline-color: #fffff8; //网格线
}
QHeaderView
{
border-bottom:10px solid white;
}
QHeaderView::section
{
color:white;
background:transparent;
border-top:0px solid white ;
min-height:76px;
max-height:76px;
}
QTableWidget::item
{
color:white;
background:transparent;
}
QTableWidget::item::selected
{
color:white;
background:#2d3e4b;
}
QTableWidget QScrollBar::vertical
{
background:#0c161e;
border:1 solid #bbc7d0;border-radius:8px;
}
QTableWidget QScrollBar::handle
{
border-radius:6px;
background-color:qlineargradient(x1:0, y1:0, x2:1, y2:0,stop:0 #bbc7d0,stop:0.6 #f8fbfd,stop:1 #bbc7d0);
min-height:50px;
}
QTableWidget QScrollBar::add-line,QTableWidget QScrollBar::sub-line{
border:none;}
QTableWidget QScrollBar::add-page,QTableWidget QScrollBar::sub-page{
border:#0c161e;border-radius:10px;}
Grid lines also need to be set in QTableWidget:
Set grid lines: setShowGrid(false);
qss:
QTableWidget
{
color:white;
border-top:1px solid white;
gridline-color: #fffff8; //网格线
}
But if we only need to set a part of the grid lines, we need to use a proxy. Of course, we can also override the QTableWidget
proxy method as follows:
ui->table->setShowGrid(false);
ui->table->setItemDelegate(new QLineDelegate(ui->table));
class QLineDelegate : public QStyledItemDelegate
{
public:
QLineDelegate(QTableView* tableView)
{
int gridHint = tableView->style()->styleHint(QStyle::SH_Table_GridLineColor, new QStyleOptionViewItemV4());
QColor gridColor = static_cast<QRgb>(gridHint);
pen = QPen(gridColor, 0, tableView->gridStyle());
view = tableView;
}
protected:
void paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const
{
QStyledItemDelegate::paint(painter, option, index);
QPen oldPen = painter->pen();
painter->setPen(pen);
//painter->drawLine(option.rect.topRight(), option.rect.bottomRight());//显示垂直网格线
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());//显示水平网格线
painter->setPen(oldPen);
}
private:
QPen pen;
QTableView* view;
};
Here are some additional common functions of QTableWidget:
setSelectionBehavior(QAbstractItemView::SelectRows); //Select the entire row
setSelectionMode(QTableWidget::SingleSelection); //Smartly select one row at a time
setEditTriggers(QAbstractItemView::NoEditTriggers); //Item cannot be edited
etFocusPolicy (Qt::NoFocus);//No focus
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//Hide the horizontal slider
setHorizontalHeaderLabels(QStringList()<<"Number"<<"Track"<<"Departure\nPlace"< <"Arrive\nPlace");//Set the horizontal header
this->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//Set the custom length of the horizontal header
setColumnWidth(0,50);//Set Column wideband
setShowGrid(false);//Show/hide grid lines
setItemDelegate(new QLineDelegate(ui->charttable));//Set proxy
connect(ui->table,&QTableWidget::itemChanged,ui->table,&QTableWidget::resizeRowsToContents);//Associate item to realize automatic line wrapping in the vertical direction of the table
//设置表头字体大小
for (int i = 0; i < this>columnCount(); ++i)
this->horizontalHeaderItem(i)->setFont(font);
this->setfont(font);//设置表格中字体大小。