QTableWidget

一、设置表单样式

  table_widget->setColumnCount(4); //设置列数

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

 

  //设置表头内容
  QStringList header;
  header<  table_widget->setHorizontalHeaderLabels(header);

 

  //设置表头字体加粗

  QFont font = this->horizontalHeader()->font();
  font.setBold(true);
  table_widget->horizontalHeader()->setFont(font);

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

  table_widget->setStyleSheet("selection-"); //设置选中背景色

  table_widget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}"); //设置表头背景色

setFocusPolicy(Qt::NoFocus); //去除选中虚线框
setColumnCount(1);   //设置列数
setColumnWidth(0, 20);//设置列宽

 

  //设置水平、垂直滚动条样式

  table_widget->horizontalScrollBar()->setStyleSheet("QScrollBar{background:transparent; 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;}");

  table_widget->verticalScrollBar()->setStyleSheet("QScrollBar{background:transparent; 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;}");

//设置样式

setStyleSheet("\
        QTableWidget{ \
            color:white; \
       background-color:#5a636d;\
            selection-background-color:skyblue;\
            border:0px;\
            gridline-color:#212426;\
            font: 12pt 'Microsoft YaHei' \
        } \
        /*这里是表格表头样式*/ \
        QHeaderView::section{ \
            color:white; \
        background-color:#5a636d;\
            border:0.5px solid #212426;; \
            height: 30px;\
            font: 12pt 'Microsoft YaHei' \
        }");

转载于:https://www.cnblogs.com/nanqiang/p/11212547.html

你可能感兴趣的:(QTableWidget)