[1] qtablewidget Overview
Qtablewidget is a common control used to display data tables in the QT dialog box design.
To learn qtablewidget, first look at the qtableview control (the control also has a "family! Like researchers), because qtablewidget inherits from class qtableview.
The main difference between the two is that qtableview can use a custom data model to display the content (that is, you must bind the data source through setmodel first), while qtablewidget can only use the standard data model.
The qtablewidget cell data is implemented by the qtablewidgetitem object (that is, the data source is not required, and the information in the cells must be filled one by one ).
This is mainly because the qtableview class has the setmodel member function. In the qtablewidget class, this member function becomes private.
Qtablewidget is inseparable from qtablewidgetitem. Qtablewidgetitem is used to represent one of the cells in the table. The entire table must be constructed with the cell-by-Cell Object qtablewidgetitem.
[2] qtablewidget control attributes
(1) prohibit table editing
By default, the characters in the table can be changed.
For example, you can double-click a cell to modify the original content. If you want to disable this operation, make the table read-only to the user, you can:
1 ui.qtablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
(2) set the table to select the entire row.
1 /*设置表格为整行选中*/2 ui.qtablewidget->setSelectionBehavior(QAbstractItemView::SelectRows);
(3) set a single selection and multiple selections
A single selection means that only one cell can be selected at a time. Multiple cells are equivalent to the "One Cell" mode.
1 /*设置允许多个选中*/ 2 ui.qtablewidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
(4) display and hide table Headers
For horizontal or vertical headers, you can set (hide/display) as follows if you do not want to display them:
1 ui.qtablewidget->verticalHeader()->setVisible(true); 2 ui.qtablewidget->horizontalHeader()->setVisible(false);
(5) set the font alignment of a specific cell.
1 ui.qtablewidget->item(0, 0)->setTextAlignment(Qt::AlignHCenter);
(6) set the font format in a specific cell
1 ui.qtablewidget->item(1, 0)->setBackgroundColor(QColor(0,60,10)); 2 ui.qtablewidget->item(1, 0)->setTextColor(QColor(200,111,100)); 3 ui.qtablewidget->item(1, 0)->setFont(QFont("Helvetica"));
(7) set the value of a specific cell
1 ui.qtablewidget->setItem(1, 0, new QTableWidgetItem(str));
(8) convert the content of the qtablewidgetitem object to qstring
1 QString str =ui.qtablewidget->item(0, 0)->data(Qt::DisplayRole).toString();
(9) add controls to cells
1 QComboBox *comBox = new QComboBox(); 2 comBox->addItem("F"); 3 comBox->addItem("M"); 4 ui.qtablewidget->setCellWidget(0,3,comBox);
(11) Merge Cells
1 //合并单元格的效果2 ui.qtablewidget->setSpan(2, 2, 3, 2);3 //第一个参数:要改变的单元格行数4 //第二个参数:要改变的单元格列数5 //第三个参数:需要合并的行数6 //第四个参数:需要合并的列数
(12) insert an image into a specific cell
ui.qtablewidget->setItem(3, 2, new QTableWidgetItem(QIcon("images/music.png"), "Music"));
(13) set the display grid
ui.qtablewidget->setShowGrid(true);//显示表格线
(14) set the scroll bar
ui.qtablewidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//去掉水平滚动条
(15) set column labels
1 //初始化界面 2 QStringList HStrList; 3 HStrList.push_back(QString("name")); 4 HStrList.push_back(QString("id")); 5 HStrList.push_back(QString("age")); 6 HStrList.push_back(QString("sex")); 7 HStrList.push_back(QString("department")); 8 9 10 //设置行列数(只有列存在的前提下,才可以设置列标签)11 int HlableCnt = HStrList.count();12 ui.qtablewidget->setRowCount(10);13 ui.qtablewidget->setColumnCount(HlableCnt);14 15 //设置列标签16 ui.qtablewidget->setHorizontalHeaderLabels(HStrList);
(16) set the size of rows and columns to match the content
1 ui.qtablewidget->resizeColumnsToContents(); 2 ui.qtablewidget->resizeRowsToContents();
(17) set the font
ui.qtablewidget->setFont(font); //设置字体
(18) obtain the content of a cell
1 QString strText = ui.qtablewidget->item(0, 0)->text();
Qtablewidget control summary