首页 > 代码库 > QTableWidget 使用及美化_QtableWidget_QtableView滚动条宽度及样式

QTableWidget 使用及美化_QtableWidget_QtableView滚动条宽度及样式

 
//创建及属性设置
m_tableWidget = new QTableWidget(this);
m_tableWidget->setRowCount(10);
m_tableWidget->setColumnCount(3);
//m_tableWidget->setFrameShape(QFrame::NoFrame); //设置边框
//m_tableWidget->setShowGrid(false); //设置不显示格子线
//QPalette pll = m_tableWidget->palette();
//pll.setBrush(QPalette::Base,QBrush(QColor(255,255,255,0)));
//m_tableWidget->setPalette(pll);
//m_tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:lightblue;color: black;padding-left: 4px;border: 1px solid #6c6c6c;}");


m_tableWidget->horizontalHeader()->setDefaultSectionSize(150);
m_tableWidget->horizontalHeader()->setClickable(false); //设置表头不可点击(默认点击后进行排序)

//设置表头内容
QStringList header;
header<<tr("Index")<<tr("Name")<<tr("Time");
m_tableWidget->setHorizontalHeaderLabels(header);

//设置表头字体加粗
QFont font = m_tableWidget->horizontalHeader()->font();
font.setBold(true);
m_tableWidget->horizontalHeader()->setFont(font);

m_tableWidget->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
m_tableWidget->verticalHeader()->setResizeMode(QHeaderView::ResizeToContents);
m_tableWidget->verticalHeader()->setDefaultSectionSize(10); //设置行距
m_tableWidget->setFrameShape(QFrame::NoFrame); //设置无边框
m_tableWidget->setShowGrid(false); //设置不显示格子线
m_tableWidget->verticalHeader()->setVisible(false); //设置垂直头不可见
m_tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //可多选(Ctrl、Shift、 Ctrl+A都可以)
m_tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
m_tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
m_tableWidget->horizontalHeader()->resizeSection(0,20); //设置表头第一列的宽度为150
m_tableWidget->horizontalHeader()->resizeSection(2,50); //设置表头第一列的宽度为150
m_tableWidget->horizontalHeader()->setFixedHeight(25); //设置表头的高度

//m_tableWidget->setStyleSheet("background: rgb(56,56,56);alternate-background-color:rgb(48,51,55);selection-background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(56,56,56),stop:1 rgb(76,76,76));"); //设置选中背景色
//m_tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 rgb(46,46,46),stop:1 rgb(66,66,66));color: rgb(210,210,210);;padding-left: 4px;border: 1px solid #383838;}"); //设置表头背景色
m_tableWidget->setAlternatingRowColors(true);
m_tableWidget->setFocusPolicy(Qt::NoFocus); //去除选中虚线框

//设置水平、垂直滚动条样式
//m_tableWidget->horizontalScrollBar()->setStyleSheet("QScrollBar{background:red; height:10px;}"
// "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;}");
//m_tableWidget->verticalScrollBar()->setStyleSheet("QScrollBar{background:red; width: 10px;}"
// "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;}");
/*QPalette pal;
pal.setColor(QPalette::Base, QColor(255, 0, 0));
pal.setColor(QPalette::AlternateBase, QColor(0, 255, 0));
m_tableWidget->setPalette(pal);*/
m_tableWidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_tableWidget->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
m_tableWidget->setAlternatingRowColors(true);
m_tableWidget->setShowGrid(false);

QTableWidget 使用及美化_QtableWidget_QtableView滚动条宽度及样式