QT+qtablewidget自定義表頭【合併單元格】

一、把下列文件放在工程中【已上傳到個人文件中】函數

二、代碼ui

     auto *headview = new HHeadViewClass(Qt::Horizontal, ui.tableWidget);
	headview->setSectionResizeMode(QHeaderView::Interactive);
	headview->titleHigh = 50;
	headview->setFixedHeight(50);

	headview->SetRowColItemText(0, 0, QStringLiteral("千米"));
	headview->SetRowColItemText(1, 0, QStringLiteral("長度[m]"));

	headview->SetRowColItemText(2, 0, QStringLiteral("高低"));
	headview->SetRowColItemText(2, 1, QStringLiteral("1"));
	headview->SetRowColItemText(2, 2, QStringLiteral("2"));
	headview->SetRowColItemText(2, 3, "3");
	headview->SetRowColItemText(2, 4, "4");

	headview->SetRowColItemText(3, 0, QStringLiteral("軌向"));
	headview->SetRowColItemText(3, 1, "1");
	headview->SetRowColItemText(3, 2, "2");
	headview->SetRowColItemText(3, 3, "3");
	headview->SetRowColItemText(3, 4, "4");

	headview->SetRowColItemText(4, 0, QStringLiteral("軌距"));
	headview->SetRowColItemText(4, 1, "1");
	headview->SetRowColItemText(4, 2, "2");
	headview->SetRowColItemText(4, 3, "3");
	headview->SetRowColItemText(4, 4, "4");

	headview->SetRowColItemText(5, 0, QStringLiteral("水平"));
	headview->SetRowColItemText(5, 1, "1");
	headview->SetRowColItemText(5, 2, "2");
	headview->SetRowColItemText(5, 3, "3");
	headview->SetRowColItemText(5, 4, "4");

	headview->SetRowColItemText(6, 0, QStringLiteral("三角坑"));
	headview->SetRowColItemText(6, 1, "1");
	headview->SetRowColItemText(6, 2, "2");
	headview->SetRowColItemText(6, 3, "3");
	headview->SetRowColItemText(6, 4, "4");

	headview->SetRowColItemText(7, 0, QStringLiteral("垂向加速度"));
	headview->SetRowColItemText(7, 1, "1");
	headview->SetRowColItemText(7, 2, "2");
	headview->SetRowColItemText(7, 3, "3");
	headview->SetRowColItemText(7, 4, "4");

	headview->SetRowColItemText(8, 0, QStringLiteral("橫向加速度"));
	headview->SetRowColItemText(8, 1, "1");
	headview->SetRowColItemText(8, 2, "2");
	headview->SetRowColItemText(8, 3, "3");
	headview->SetRowColItemText(8, 4, "4");

	headview->SetRowColItemText(9, 0, QStringLiteral("曲率變化率"));
	headview->SetRowColItemText(9, 1, "1");
	headview->SetRowColItemText(9, 2, "2");

	headview->SetRowColItemText(10, 0, QStringLiteral("軌距變化率"));
	headview->SetRowColItemText(10, 1, "1");
	headview->SetRowColItemText(10, 2, "2");

	headview->SetRowColItemText(11, 0, QStringLiteral("速度"));

	headview->SetRowColItemText(12, 0, QStringLiteral("扣分"));

	ui.tableWidget->setHorizontalHeader(headview);

三、效果.net

注意要在ui或者代碼裏設置列數,不然界面全空 code

四、爲了配合上述,給出下列QHeaderView函數,這裏轉:https://blog.csdn.net/psujtfc/article/details/43764559blog

1 . void QTableView::​setColumnWidth(int column, int width) 設置某一列的列寬get

2. 經過QHeaderView * QTableView::​horizontalHeader() const得到QHeaderViewit

在調用QHeaderView裏面的函數進行設置io

3. QHeaderView的列寬設置函數table

3.1 設置限制值class

設置列寬最大值:void setMaximumSectionSize(int size)
設置列寬最小值:void  setMinimumSectionSize(int size)

3.2 設置自動適應

設置某一列的自動適應模式:void QHeaderView::​setSectionResizeMode(int logicalIndex, ResizeMode mode)

設置整個表頭的自適應模式:void setSectionResizeMode(ResizeMode mode)

ResizeMode:

QHeaderView::Interactive:0 用戶可設置,也可被程序設置成默認大小

QHeaderView::Fixed       :2 用戶不可更改列寬

QHeaderView::Stretch      :1 根據空間,自動改變列寬,用戶與程序不能改變列寬

QHeaderView::ResizeToContents:3 根據內容改變列寬,用戶與程序不能改變列寬

注意:ResizeMode被設置爲1,3時, void QTableView::​setColumnWidth(int column, int width)的效果不會被執行,即不能定義某一列的列寬

相關文章
相關標籤/搜索