https://www.cnblogs.com/retry/p/9329397.html
table of Contents
Qtable Widget QT control of
1, form the basis of property
- Form Definition
//方法1:
QTableWidget *tableWidget = new QTableWidget(10,5); // 构造了一个QTableWidget的对象,并且设置为10行,5列
//方法2:
QTableWidget *tableWidget = new QTableWidget;
tableWidget-> setRowCount ( 10 ); // Set the number of lines 10
tableWidget-> setColumnCount ( 5 ); // Set the number of columns 5
- Set header
QStringList header;
header<<“Month”<<“Description”;
tableWidget->setHorizontalHeaderLabels(header);
- Setting the table of contents
tableWidget->setItem(0,0,new QTableWidgetItem("Jan"));
tableWidget->show();
2, table editing properties
- By default, the form of the characters can be changed, such as double-click a cell, you can modify the original content, if you want to ban the operation of the user, so that the table read-only users, but also can be set.
原型:tableWidget->setEditTriggers(QAbstractItemView::EditTrigger);
QAbstractItemView.EditTrigger multiple parameters, QAbstractItemView.NoEditTriggers is a QAbstractItemView.EditTrigger enumeration.
parameter | XX | XX |
---|---|---|
QAbstractItemView.NoEditTriggers | 0 | Table of contents can not be modified |
QAbstractItemView.CurrentChanged | 1 | Any modifications to the cell at all times |
QAbstractItemView.DoubleClicked | 2 | Double-click cell |
QAbstractItemView.SelectedClicked | 4 | Click the selected content |
QAbstractItemView.CurrentChanged | 8 | Any modifications to the cell at all times |
QAbstractItemView.AnyKeyPressed | 16 | Double-click cell |
QAbstractItemView.AllEditTriggers | 31 | Double-click cell |
3, attributes selected cell
- Select the entire row
原型: tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows); //整行选中的方式
parameter | meaning |
---|---|
QAbstractItemView.SelectItems | Select a single cell |
QAbstractItemView.SelectRows | Select a row |
QAbstractItemView.SelectColumns | Select one |
- And select a single set of a plurality of selected
原型:tableWidget->setSelectionMode(QAbstractItemView::ExtendedSelection); //设置为可以选中多个目标
parameter | meaning |
---|---|
QAbstractItemView.NoSelection | You can not choose |
QAbstractItemView.SingleSelection | Select a single target |
QAbstractItemView.MultiSelection | Select multiple targets |
3, display and hide the header
- Horizontal or vertical header for methods, may be provided to hide / show in the following manner.
//注意:需要 #include <QHeaderView>
tableWidget->verticalHeader()->setVisible(false); //隐藏列表头
tableWidget->horizontalHeader()->setVisible(false); //隐藏行表头
4, cell font settings
QTableWidgetItem *item = new QTableWidgetItem("Apple");
item->setBackgroundColor(QColor(0,60,10));
item->setTextColor(QColor(200,111,100));
item->setFont(QFont("Helvetica"));
tableWidget->setItem(0,3,item);
// Another: If you need to use this font for all cells, you can use tableWidget-> setFont (QFont ( "Helvetica"));
5, disposed within the cell alignment of text
Use newItem.setTextAlignment () function is provided, parameters of the function as the alignment of the cell, and a character input sequence from left and right, or from right to left with, if both should be set, as long as Qt.AlignHCenter | to Qt.AlignVCenter way.
//方法
ui->tableWidget->item(i, 2)->setTextAlignment(Qt::AlignCenter);
//方法
QTableWidgetItem *t_test = new QTableWidgetItem(xList);
t_test->setTextAlignment(Qt::AlignCenter);
6, merge cells
tableWidget->setSpan(0, 0, 3, 1) //# 其参数为: 要改变单元格的 1行数 2列数 要合并的 3行数 4列数
7, a line width
- Specifies the width of the ranks
//方法一:
tableWidget->setColumnWidth(3,200);
tableWidget->setRowHeight(3,60);
//方法二:
ui->tableWidget->horizontalHeader()->setResizeMode(QHeaderView::fix); //指定列宽
ui->tableWidget->verticalHeader()->setResizeMode(QHeaderView::fix); //制定行高
- Automatically adjust the width of the ranks
//根据内容自动调整所有行的行高
//方法一:
tableWidget->resizeColumnsToContents();
tableWidget->resizeRowsToContents();
@ Method two:
ui-> tableWidget-> The horizontalHeader () -> setResizeMode (QHeaderView :: the Stretch); adaptive column width
ui-> tableWidget-> verticalHeader () - > setResizeMode (QHeaderView :: Stretch); adaptive line high
// content automatically adjusted according to a row line high
tableWidget-> resizeColumnsToContents ( 2 ); // 2 rows
tableWidget-> resizeRowsToContents ( 2 );
Alternative forms // effects:
the table-> The horizontalHeader () -> setStretchLastSection ( to true ); // other columns to keep the default width, stretched a final fill.
8, other examples provided
int rowIndex = m_pAttrbuteList->rowCount();
tableWidget->setRowCount(rowIndex + 1);//总行数增加1
tableWidget-> setRowHeight (rowIndex, 24 ); the height // set row
QTableWidget *table = new QTableWIdget(this);
the table-> setColumnCount ( . 5 ); // number of columns
the table-> setRowCount ( . 3 ); set // the number of rows /
/ Set Column Name /
QStringList headers;
<< Headers "column name 1" << "column name 2" << "column name 3" ;
table->setHorizontalHeaderLabels(headers);
/ To add the contents of the cell /
void addItemContent(int row, int column, QString content)
{
QTableWidgetItem *item = <span class="hljs-keyword">new</span> QTableWidgetItem (content);
table->setItem(row, column, item);
}
/ To increase cell icon /
* Item = QTableWidgetItem new new QTableWidgetItem (QIcon ( "myImage.jpg" ), NULL ); // add only icons, no strings
* Item = QTableWidgetItem new new QTableWidgetItem (QIcon ( "myImage.jpg" ), myString); // adding icons and strings
table->setItem(row, column, item);
/ Insert a row /
int row = table->rowCount();
table->insertRow(row);
/ Insert a /
int column = table->columnCount();
table->insertColumn(column);
// make the adaptive line width of the head, and finally a blank portion will be filled with
table->horizontalHeader()->setStretchLastSection(true);
// make the adaptive line head width, all columns filled average blank portion
table->horizontalHeader()->setResizeMode(QHeaderView::Strtch);
// make the adaptive line height, line if many words, the height of the line would not have been reduced, when a certain value will automatically generate a QScrollBar
table->verticalHeader()->setResizeMode(QHeaderView::Strtch);
// Set click to select a row
table->setSelectionBehuavior(QAbstractItemView::SelectRows);
// Set each line is not editable
table->setEditTriggers(QAbstractItemView::NoEditTriggers);
// set can only select a row, you can not select multiple rows
table->setSelectionMode(QAbstractItemView::SingleSelection);
/ Removing the line number of each line /
QHeaderView *headerView = table->verticalHeader();
headerView->setHidden(true);
/ Setting allows a cell or a row select /
Select the cell: table-> setCurrentCell (row, column, QItemSelectionModel :: Select);
Select a row: table-> setCurrentCell (row, QItemSelectionModel :: Select); (note that there is no value listed here)