Use of QVariant data structure

In Qt, you can use the setSectionResizeMode() method of the QHeaderView class to set the stretching mode of the column width or row height of the table. This method can be used to set the automatic stretching of the table, fixed size or adjustment according to the content, etc.

The following are some commonly used methods of setting table stretching:

  1. Set the column width stretching method:
QTableView *tableView = new QTableView;

// 设置所有列宽自动调整
tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

// 设置指定列宽自动调整
tableView->horizontalHeader()->setSectionResizeMode(columnIndex, QHeaderView::Stretch);

// 设置指定列固定宽度
tableView->horizontalHeader()->setSectionResizeMode(columnIndex, QHeaderView::Fixed);
tableView->setColumnWidth(columnIndex, width);

For the case of setting column stretching, the setStretchLastSection(false) function is usually used to disable the automatic stretching of the last column.

  1. Set the row height stretch method:
QTableView *tableView = new QTableView;

// 设置所有行高自动调整
tableView->verticalHeader()->setSectionResizeMode(QHeaderView::Stretch);

// 设置指定行高自动调整
tableView->verticalHeader()->setSectionResizeMode(rowIndex, QHeaderView::Stretch);

// 设置指定行固定高度
tableView->verticalHeader()->setSectionResizeMode(rowIndex, QHeaderView::Fixed);
tableView->setRowHeight(rowIndex, height);

For the writing method of automatic line number, the table control of QT is OK after testing, but it is unknown whether it is feasible for the tree control.

In the above code, tableView is a QTableView object for displaying tabular data. By obtaining the horizontal or vertical header objects horizontalHeader() and verticalHeader(), you can use the setSectionResizeMode() method to set the stretching mode. Among them, QHeaderView::Stretch means automatic stretching, and QHeaderView::Fixed means fixed size.

Note that the automatic stretching method of the table may automatically adjust the column width or row height according to the change of the table content. The fixed size method needs to explicitly set the value of the column width or row height.

The benefits of this article, free to receive Qt development learning materials package, technical video, including (C++ language foundation, C++ design pattern, introduction to Qt programming, QT signal and slot mechanism, QT interface development-image drawing, QT network, QT database programming, QT project actual combat, QSS, OpenCV, Quick module, interview questions, etc.) ↓↓↓↓↓↓See below↓↓Click on the bottom of the article to receive the fee↓↓

Guess you like

Origin blog.csdn.net/m0_73443478/article/details/131132012