QT表格相关设计函数

添加头文件

#include 
#include 
#include 
#include 

表格相关函数

void 类名::inittable()
{
	//设置字体
	fontBody.setFamily(QString::fromUtf8("Helvetica"));
	fontBody.setPixelSize(20);
	//设置表格参数
	int rowCount = 5;
	int colCount = 2;
	int controlWidth = ui->table->width();//设置表格宽为ui页面的值,也可以自己定义
	int controlHeight = ui->table->height();
	//设置表格属性
	ui->table->setEditTriggers(QAbstractItem::NoEditTriggers);//禁止编辑
	ui->table->setSelectionMode(QAbstactItemView::NoSelection);//禁止选择
	ui->table->setFocusPolicy(Qt::NoFocus);//禁止点击
	ui->table->setHorizontalScrollbarPolicy(Qt::ScrollBarAlwaysOff);//禁止水平滚动条
	ui->table->setVertiaclScrollbarPolicy(Qt::ScrollBarAlwaysOff);//禁止垂直滚动条
	ui->table->verticalHeader()->setVisible(false);//去除行头
	ui->table->setAlternatingRowColors(true);//设置相邻行颜色交替显示
	ui->table->horizontalHeader()->setStyleSheet("QHeaderView::section{background:skyblue;}");//设置表头颜色
	ui->table->horizontalHeader()->setStretchLastSection(true);//设置表格充满整个控件
	ui->table->setShowGrid(false); //不显示网格线
	ui->table->setFrameShape(QFrame::NoFrame);//设置无表格外边框
	//设置表格行数列数,表格大小,行高和宽
	ui->table->setRowCount(rowCount);//行数
	ui->table->setRColumnCount(colCount);//列数
	ui->table->resize(controlWidth,controlHeight);//表格大小
	ui->table->verticalHeader()->setResizeMode(QHeaderView::Strech);//行高可根据表格大小自动调整
	ui->table->horizontalHeader()->setResizeMode(QHeaderView::Strech);//列宽可根据表格大小自动调整
	//ui->table->verticalHeader()->setFixedWidth(180);//固定列宽
	//表格以特定字体填写数据
	for(int i=0;i<rowCount;i++)
	{
		for(int j=0;j<colCount;j++)
		{
			QLabel *item = new QLabel(ui->table);
			if(j != 0 )
				item->setAlignment(Qt::AlignCenter);
			item->setFont(fontbody);
			item->setStyleSheet("color:rgb(255,255,255);background-color:rgb(45,47,49);");
			item->setText("");//传qstring即可
			ui->table->setCellWidget(i,j,item);
		}
	}
	//设置列头
	QStringList header;
	header.clear();
	QStringList colname;
	colname<<"h1"<<"h2";
	for(int i=0;i<colname.size();i++)
		header<<colname[i]
	ui->table->setHorizontalHerderLabels(header);
	ui->table->horizontalHeader()->setClickable(false);//设置点击无效
	ui->table->horizontalHeader->setStyleSheet("color:rgb(255,255,255);background-color:rgb(45,47,49);font:18pt,'Helvetica'");
	//在表格中写数据
	const char* charstyle="color:rgb(240,65,85);background-color:rgb(45,47,49);font:18pt,'Helvetica'";//写入数据格式 
	((QLabel *)ui->table->cellWidget(0,0))->setText("要写入的数据")((QLabel *)ui->table->cellWidget(0,0))->setStyleSheet(charstyle);

}

一个滑块样式

    ui->table->verticalScrollBar()->setStyleSheet("QScrollBar:vertical{"        //垂直滑块整体
                                                  "background:#FFFFFF;"  //背景色
                                                  "padding-top:20px;"    //上预留位置(放置向上箭头)
                                                  "padding-bottom:20px;" //下预留位置(放置向下箭头)
                                                  "padding-left:3px;"    //左预留位置(美观)
                                                  "padding-right:3px;"   //右预留位置(美观)
                                                  "border-left:1px solid #d7d7d7;}"//左分割线
                                                  "QScrollBar::handle:vertical{"//滑块样式
                                                  "background:#dbdbdb;"  //滑块颜色
                                                  "border-radius:6px;"   //边角圆润
                                                  "min-height:80px;}"    //滑块最小高度
                                                  "QScrollBar::handle:vertical:hover{"//鼠标触及滑块样式
                                                  "background:#d0d0d0;}" //滑块颜色
                                                  "QScrollBar::add-line:vertical{"//向下箭头样式
                                                  "background:url(:/images/resource/images/checkout/down.png) center no-repeat;}"
                                                  "QScrollBar::sub-line:vertical{"//向上箭头样式
                                                  "background:url(:/images/resource/images/checkout/up.png) center no-repeat;}");
    
    ui->table->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal{"
                                                    "background:#FFFFFF;"
                                                    "padding-top:3px;"
                                                    "padding-bottom:3px;"
                                                    "padding-left:20px;"
                                                    "padding-right:20px;}"
                                                    "QScrollBar::handle:horizontal{"
                                                    "background:#dbdbdb;"
                                                    "border-radius:6px;"
                                                    "min-width:80px;}"
                                                    "QScrollBar::handle:horizontal:hover{"
                                                    "background:#d0d0d0;}"
                                                    "QScrollBar::add-line:horizontal{"
                                                    "background:url(:/images/resource/images/checkout/right.png) center no-repeat;}"
                                                    "QScrollBar::sub-line:horizontal{"
                                                    "background:url(:/images/resource/images/checkout/left.png) center no-repeat;}");


你可能感兴趣的:(QT)