QT自定義QTableWidget

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;
}
/*表頭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
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章