ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); //设置不可编辑
ui->tableWidget->verticalHeader()->setVisible(false); //设置垂直头不可见
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //设置选择行为时每次选择一行
ui->tableWidget->setSelectionMode(QAbstractItemView::SingleSelection); //设置不可多选 (Ctrl、Shift和Ctrl+A)
ui->tableWidget->setFrameShape(QFrame::NoFrame); //设置无边框
ui->tableWidget->setShowGrid(false); //不显示格子线
ui->tableWidget->horizontalHeader()->setStretchLastSection(true); //设置充满表宽度
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
//ui->tableWidget->horizontalHeader()->setDefaultSectionSize(140); //默认宽度
ui->tableWidget->horizontalHeader()->setMaximumSectionSize(140);
ui->tableWidget->setItemDelegate(new NoFocusDelegate()); //虚线边框去除
ui->tableWidget->horizontalHeader()->setHighlightSections(false); //点击表时不对表头行光亮(获取焦点)
//设置表头背景色
ui->tableWidget->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");
ui->tableWidget->setStyleSheet("selection-background-color:lightblue;"); //设置选中背景色
//设置表格隔行换色
ui->tableWidget->setAlternatingRowColors(true);
ui->tableWidget->setStyleSheet("QTableView{background-color: rgb(250, 250, 115);" "alternate-background-color: rgb(50, 110, 255);}");
//某一列不能编辑
item = new QTableWidgetItem(str);
item->setFlags(item->flags() & (~Qt::ItemIsEditable));