QT自定义QTableWidget

目录

  • QT自定义QTableWidget
    • 1.实现效果
    • 2.具体实现:
      • (1)表格QTabelWidget
      • (2)表头QHeaderView
      • (3)插入数据

QT自定义QTableWidget


1.实现效果

QT自定义QTableWidget_第1张图片

2.具体实现:

(1)表格QTabelWidget

	QTabelWidget *table=new QTableWidget(11,6);	//设置行列个数
    table->setShowGrid(false);		//是否显示内部网格线条
    table->setFixedSize(1782,698);		//设置表格固定大小
    table->setAlternatingRowColors(1);	//是否显示交替颜色
    table->setEditTriggers(QAbstractItemView::NoEditTriggers);		//是否可编辑
    table->setSelectionMode(QAbstractItemView::NoSelection);	//是否可选中
    table->setProperty("table_31",true);		//设置分类
    for(int i=0; i<11; i++ ){
     				//设置行高,列宽
        table->setRowHeight(i,58);
    }
	for(int i=0; i<6; i++ ){
     
		table->setColumnWidth(i,200);
	}
  • qss样式表
QTableWidget[table_31="true"]{
     
    border: 1px solid  #8B8B8B;		/*表格外边框*/
	padding: 0px;		/*内边距*/
	margin: 0px;			/*外边距*/
	color: #575757;		/*表格文本的颜色*/
	background: white;	/*表格背景色*/
	alternate-background-color: lightgray;		/*表格交替色*/
	gridline-color:	#8B8B8B;		/*网格边框*/
	font-size: 24px;					/*表格文本大小*/
	border-radius: 20px;		/*表格边框弧度*/
	font-family: "Microsoft YaHei";		/*表格文本字体*/
}

QTableWidget::item[table_31="true"]{
     
    border-radius: 0px;				/*item 弧度*/
	background: transparent;	/*item 背景色*/
	border-style: none;			/*去除边框*/
    border-bottom: 1px solid  #8B8B8B;		/*底 边框*/
	border-right: 1px solid  red;		/*右 边框*/
	border-left: 1px solid  red;			/*左 边框*/
	border-top: 1px solid  #8B8B8B;		/*顶 边框*/
}
/*item 选中时*/
QTableWidget::item:selected[table_31="true"]{
     
    background:	transparent;
	color: black;
}
/*item 悬停*/
QTableWidget::item:hover[table_31="true"]{
     
    background:	transparent;
	color: black;
}
/*垂直表头和水平表头相交的单元格*/
QTableWidget[table_31="true"] QTableCornerButton::section{
     
   background: white;
   border: 1px solid  #8B8B8B;
}

(2)表头QHeaderView

	//水平表头
    table->horizontalHeader()->setVisible(true);		//设置表头是否可见
    table->horizontalHeader()->setStretchLastSection(true);	//自动调整对齐右边边界
    table->horizontalHeader()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);		//表头文本对齐方式
    table->horizontalHeader()->setObjectName("hHeader");		//表头对象名
    //垂直表头
    table->verticalHeader()->setVisible(false);
    table->verticalHeader()->setStretchLastSection(true);		//自动调整对齐底部边界
    table->verticalHeader()->setDefaultAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
    table->verticalHeader()->setObjectName("vHeader");
    //设置表头文本
	QStringList header;
    header<<"A"<<"B"<<"C"<<"D"<<"E"<<"F";
    table->setHorizontalHeaderLabels(header);
    header.clear();
	header<<"A"<<"B"<<"C"<<"D"<<"E"<<"F"<<"G"<<"H"<<"I"<<"J"<<"K";
	table->setVerticalHeaderLabels(header);
  • qss样式表
/*水平表头*/
QHeaderView#hHeader{
     
	min-height: 50px;		/*表头最小高度*/
	background-color: transparent;
	border-top-left-radius: 10px;
	border-top-right-radius: 10px;
 	border-bottom-left-radius: 0px;
	border-bottom-right-radius: 0px;
}
/*表头section*/
QHeaderView::section#hHeader{
     
   	background-color: transparent;		
   	border: 1px solid  #8B8B8B;
   	border-radius: 0px;
   	font-size: 24px;
   	color:  #1B1B1B;
   /*border-style:none;*/
	border-bottom: 1px solid  #8B8B8B;
	border-right: 1px solid blue;
	border-left: 1px solid blue;
	border-top: 1px solid blue;
}
/*垂直表头*/
QHeaderView#vHeader{
     
   min-width: 50px;
   background: white;
}
QHeaderView::section#vHeader{
     
   background-color: white;
   border: 1px solid  #8B8B8B;
   border-radius: 0px;
   border-left-color: white;
   font-size: 24px;
   font-family: "Microsoft YaHei";
}

(3)插入数据

    for(int i=0; i<11; i++){
     
        for(int j=0; j<6; j++){
     
            QTableWidgetItem *tab_item=new QTableWidgetItem("Jan");
            tab_item->setTextAlignment(Qt::AlignCenter);
            table->setItem(i,j,tab_item);			//文本
            //table->setItem(0,1,new QTableWidgetItem(QIcon(":/Image/IED.png"), "Jan's month"));		//图片
            //table->setCellWidget(0,0,new QLabel("Love"));		//label
        }
    }

你可能感兴趣的:(Qt自定义,Qt,qt,cpp)