Qt⾃定义tablewidget(背景⾊,标题箭头)
/********QTableWidget*********/
QHeaderView{      /*设置标题(包括垂直+⽔平的)*/
font-size: 19px;    /*11榜*/
border: 1px solid rgb(255, 255, 255);
/* border-bottom: 2px solid rgb(35, 100, 224);  下边框深蓝⾊*/
background: rgb(100, 188, 238);    /*背景浅蓝⾊*/
min-height:40px;
}
QHeaderView::section:horizontal {      /*设置标题(⽔平的)*/
border: 1px solid rgb(255, 255, 255);    /*⽩⾊间隔*/
border-bottom: 0px;      /*下边框不需要颜⾊*/
color: rgb(2, 65, 132);
background: transparent;
padding-left: 2px;
min-width:60px;
}
QHeaderView::section:horizontal:hover {  /*设置⿏标停留状态*/
color: white;      /*字体⽩⾊*/
background: rgb(11,82,202);    /*背景深蓝⾊*/
}
QHeaderView::section:horizontal:pressed {  /*设置⿏标按下状态*/
color: white;
background: rgb(39,106,220);    /*背景深蓝⾊减⼀点*/
}
QHeaderView::section:vertical {      /*设置标题(垂直的)*/
border: 1px solid rgb(255, 255, 255);    /*⽩⾊间隔*/
border-bottom: 0px;      /*下边框不需要颜⾊*/
color: rgb(2, 65, 132);
background: rgb(255, 255, 255,180);
padding-top: 3px;
min-width:60px;
}
QHeaderView::section:vertical:hover {  /*设置⿏标停留状态*/
color: white;      /*字体⽩⾊*/
background: rgb(11,82,202);    /*背景深蓝⾊*/
}
QHeaderView::section:vertical:pressed {  /*设置⿏标按下状态*/
color: white;
background: rgb(39,106,220);    /*背景深蓝⾊减⼀点*/
}
QHeaderView::up-arrow {      /*设置向上排序指针*/
htmlborderwidth: 13px;
height: 11px;
padding-right: 10px;      /*设置右内边距*/
image: url(:/arrow_up.png);
subcontrol-position: center right;
}
QHeaderView::up-arrow:hover, QHeaderView::up-arrow:pressed {
}
QHeaderView::down-arrow {      /*设置向下排序指针*/
width: 13px;
height: 11px;
height: 11px;
padding-right: 10px;
image: url(:/arrow_down.png);
subcontrol-position: center right;
}
QHeaderView::down-arrow:hover, QHeaderView::down-arrow:pressed {
}
QTableWidget,QTableView {
font-size: 17px;    /*10榜*/
color : rgb(1,37,116);
border: 2px solid rgb(100, 188, 238);
background: rgb(248,248,248);
gridline-color: rgb(196,226,255);
text-align: center;
outline:0px;  /*禁⽌焦点*/
}
QTableWidget::item,QTableView::item {      /*设置视图项*/
padding-left: 5px;
padding-right: 5px;
border: none;
background: rgba(251,251,253,200);
/* border-right: 1px solid rgb(100, 188, 238); */
/*border-bottom: 1px solid rgb(100, 188, 238);*/
}
QTableWidget::item:selected,QTableView::item:selected {        /*设置选中的视图项*/
background: rgba(207,230,253,200);
color : rgb(1,37,116);
}
QTableView::item:alternate:!selected,QTableWidget::item:alternate:!selected,QListView::item:alternate:!selected {
background: rgb(250,250,250);
}
QTableView::item:!alternate:!selected,QTableWidget::item:!alternate:!selected
{
background: rgb(240,247,254);
}
效果
1.QTableWidget不能在mainwindow中随主窗⼝的⼤⼩变化?
解决:在表格外部添加布局。
代码:tableWidget = new QTableWidget;
tableWidget ->setObjectName(QString::fromUtf8("tableWidget"));
QVBoxLayout *verticalLayout;
verticalLayout->addWidget(tableWidget );
2.将表格变为禁⽌编辑:
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
(参数含义:QAbstractItemView.NoEditTriggers--不能对表格内容进⾏修改
QAbstractItemView.CurrentChanged--任何时候都能对单元格修改
QAbstractItemView.DoubleClicked--双击单元格
QAbstractItemView.SelectedClicked--单击已选中的内容
QAbstractItemView.EditKeyPressed--
QAbstractItemView.AnyKeyPressed--按下任意键就能修改
QAbstractItemView.AllEditTriggers--以上条件全包括)
3.设置表格为整⾏选择
tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);  //整⾏选中的⽅式  (参数含义:AbstractItemView.SelectItems--选中单个单元格
QAbstractItemView.SelectRows--选中⼀⾏
QAbstractItemView.SelectColumns--选中⼀列)
4.单个选中和多个选中的设置:
tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);  //设置为可以选中多个⽬标
(参数含义:QAbstractItemView.NoSelection--不能选择
QAbstractItemView.SingleSelection--选中单个⽬标
QAbstractItemView.MultiSelection--选中多个⽬标
QAbstractItemView.ExtendedSelection/QAbstractItemView.ContiguousSelection 的区别不明显,主要功能是正常情况下是单选,但按下Ctrl或Shift键后,可以多选)
5.表格表头的显⽰与隐藏
对于⽔平或垂直⽅法的表头,可以⽤以下⽅式进⾏ 隐藏/显⽰ 的设置:
tableWidget->verticalHeader()->setVisible(false);  //隐藏列表头
tableWidget->horizontalHeader()->setVisible(false); //隐藏⾏表头
注意:需要 #include <QHeaderView>
6.对表头⽂字的字体、颜⾊进⾏设置
QTableWidgetItem *columnHeaderItem0 = tableWidget->horizontalHeaderItem(0); //获得⽔平⽅向表头的Item对象
columnHeaderItem0->setFont(QFont("Helvetica")); //设置字体
columnHeaderItem0->setBackgroundColor(QColor(0,60,10)); //设置单元格背景颜⾊
columnHeaderItem0->setTextColor(QColor(200,111,30)); //设置⽂字颜⾊
注意:需要 #include <QHeaderView>
7.在单元格⾥加⼊控件:
QComboBox *comBox = new QComboBox();
comBox->addItem("Y");
comBox->addItem("N");
tableWidget->setCellWidget(0,2,comBox);
8.单元格中添加图⽚:
tableWidget->setItem(row, 0, new QTableWidgetItem(QIcon(":/new/images/kingdemo.ico"),tr("")));
9设置单元格字体颜⾊、背景颜⾊和字体字符:
QTableWidgetItem *item = new QTableWidgetItem("Apple");
item->setBackgroundColor(QColor(0,60,10));
item->setTextColor(QColor(200,111,100));
item->setFont(QFont("Helvetica"));
tableWidget->setItem(0,3,item);
另:如果需要对所有的单元格都使⽤这种字体,则可以使⽤  tableWidget->setFont(QFont("Helvetica"));
10.设置单元格内⽂字的对齐⽅式
⽔平对齐⽅式有:
Constant Value Description
Qt.AlignLeft 0x0001 Aligns with the left edge.
Qt.AlignRight 0x0002 Aligns with the right edge.
Qt.AlignHCenter 0x0004 Centers horizontally in the available space.
Qt.AlignJustify 0x0008 Justifies the text in the available space.
垂直对齐⽅式:
Constant Value Description
Qt.AlignTop 0x0020 Aligns with the top.
Qt.AlignBottom 0x0040 Aligns with the bottom.
Qt.AlignVCenter 0x0080 Centers vertically in the available space.
如果两种都要设置,只要⽤ Qt.AlignHCenter |  Qt.AlignVCenter 的⽅式即可
11.合并单元格:
tableWidget->setSpan(0, 0, 3, 1)  # 其参数为: 要改变单元格的1⾏数、2列数,要合并的3⾏数、4列数
12.设置单元格的⼤⼩
⾸先,可以指定某个⾏或者列的⼤⼩
tableWidget->setColumnWidth(3,200);
tableWidget->setRowHeight(3,60);
还可以将⾏和列的⼤⼩设为与内容相匹配
tableWidget->resizeColumnsToContents();
tableWidget->resizeRowsToContents();
13.获得单击单元格的内容
通过实现 itemClicked (QTableWidgetItem *) 信号的槽函数,就可以获得⿏标单击到的单元格指针,进⽽获得其中的⽂字信息connect(tableWidget,SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),this,SLOT(getItem(QTreeWidgetItem*,int))); //将itemClicked信号与函数getItem绑定
14.QTableWidget要调整表格⾏宽主要涉及以下函数
tableWidget->horizontalHeader()->setResizeMode(QHeaderView::Stretch);//使列完全填充并平分
tableWidget->verticalHeader()->setResizeMode(QHeaderView::Stretch);//⾏⾃适应宽
tableWidget->resizeColumnsToContents(); //根据内容调整列宽
tableWidget->resizeColumnToContents(int col);//根据内容⾃动调整给定列宽
tableWidget->horizontalHeader()->setResizeMode//把给定列设置为给定模式
主要模式有Stretch和Fixed

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。