16,213
社区成员
发帖
与我相关
我的任务
分享
QTableWidgetSelectionRange Spreadsheet::selectedRange() const
{
QList<QTableWidgetSelectionRange> ranges = selectedRanges();
if (ranges.isEmpty())
return QTableWidgetSelectionRange();
return ranges.first();
}
void Spreadsheet::copy()
{
QTableWidgetSelectionRange range = selectedRange();
QString str;
for (int i = 0; i < range.rowCount(); ++i) {
if (i > 0)
str += "\n";
for (int j = 0; j < range.columnCount(); ++j) {
if (j > 0)
str += "\t";
str += formula(range.topRow() + i, range.leftColumn() + j);
}
}
QApplication::clipboard()->setText(str);
}
void Spreadsheet::paste()
{
QTableWidgetSelectionRange range = selectedRange();
QString str = QApplication::clipboard()->text();
QStringList rows = str.split('\n');
int numRows = rows.count();
int numColumns = rows.first().count('\t') + 1;
if (range.rowCount() * range.columnCount() != 1
&& (range.rowCount() != numRows
|| range.columnCount() != numColumns)) {
QMessageBox::information(this, tr("Spreadsheet"),
tr("The information cannot be pasted because the copy "
"and paste areas aren't the same size."));
return;
}
for (int i = 0; i < numRows; ++i) {
QStringList columns = rows[i].split('\t');
for (int j = 0; j < numColumns; ++j) {
int row = range.topRow() + i;
int column = range.leftColumn() + j;
if (row < RowCount && column < ColumnCount)
setFormula(row, column, columns[j]);
}
}
somethingChanged();
}
QTableWidgetSelectionRange Spreadsheet::selectedRange() const
{
QList<QTableWidgetSelectionRange> ranges = selectedRanges();
if (ranges.isEmpty())
return QTableWidgetSelectionRange();
return ranges.first();
}
QList<QTableWidgetSelectionRange> ranges = selectedRanges() ;
为啥这个函数只返回第一个节点( return ranges.first();)??
求解释![/quote]
QList<QTableWidgetSelectionRange>表示QList的元素是选定的区域, return ranges.first():表示返回第一个选定区域。
这里就可以理解”选择模式为QAbstractItemView::ContiguousSelection,所以选择范围不可能再超过1“这句话了。
因为QAbstractItemView::ContiguousSelection模式只能选定一个区域,所以return ranges.first()就够了
“选择范围不可能再超过1”改为“选择区域的个数不可能再超过1”就好理解多了
[/quote]
谢谢了,我已经懂了QAbstractItemView::ContiguousSelection在这个模式下选定的区域都可以变相的看作是一个整体,所以QList<QTableWidgetSelectionRange> ranges链表中只保存了一个对象。
QTableWidgetSelectionRange Spreadsheet::selectedRange() const
{
QList<QTableWidgetSelectionRange> ranges = selectedRanges();
if (ranges.isEmpty())
return QTableWidgetSelectionRange();
return ranges.first();
}
QList<QTableWidgetSelectionRange> ranges = selectedRanges() ;
为啥这个函数只返回第一个节点( return ranges.first();)??
求解释![/quote]
QList<QTableWidgetSelectionRange>表示QList的元素是选定的区域, return ranges.first():表示返回第一个选定区域。
这里就可以理解”选择模式为QAbstractItemView::ContiguousSelection,所以选择范围不可能再超过1“这句话了。
因为QAbstractItemView::ContiguousSelection模式只能选定一个区域,所以return ranges.first()就够了
“选择范围不可能再超过1”改为“选择区域的个数不可能再超过1”就好理解多了
QTableWidgetSelectionRange Spreadsheet::selectedRange() const
{
QList<QTableWidgetSelectionRange> ranges = selectedRanges();
if (ranges.isEmpty())
return QTableWidgetSelectionRange();
return ranges.first();
}
QList<QTableWidgetSelectionRange> ranges = selectedRanges() ;
为啥这个函数只返回第一个节点( return ranges.first();)??
求解释!