1.表格中鼠标选取的获取主要依靠以下两个函数:
QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const
Returns a list of all selected ranges.
QList<QTableWidgetItem *> QTableWidget::selectedItems() const
Returns a list of all selected items.
This function returns a list of pointers to the contents of the selected cells. Use theselectedIndexes() function to retrieve the complete selection including empty cells.
2.合并单元格实现
void QTableView::setSpan(int row, int column, int rowSpanCount, int columnSpanCount)
Sets the span of the table element at (row, column) to the number of rows and columns specified by (rowSpanCount, columnSpanCount).
注意事项:
(1)对于合并单元格之前已经创建的QTableWidgetItem,在合并单元格后再通过序号索引会出错;
QTableWidget* tableWidget = new QTableWidget;
tableWidget->setRowCount(11); // 设置表格的行数
tableWidget->setColumnCount(11); // 设置表格的列数
// 为表格的每一行每一列设置一个可以装载数据的item
for(int nRow = 0; nRow < 11; nRow++)
{
for(int nColumn = 0; nColumn < 11; nColumn++);
{
QTableWidgetItem* item = new QTableWidgetItem;
tableWidget->setItem(nRow, nColumn, item);
}
}