Get all spans in QTableWidget
-
i can describe. So, just simpler
... table_->setSelectionMode(QAbstractItemView::ExtendedSelection); // May be wrong? ... void SpreadSheetWidget::mergeCells() { QList<QTableWidgetSelectionRange> ranges = selectedRanges(); qInfo() << "Selected ranges: " << ranges.count(); for (int i = 0; i < ranges.count(); i++) { qInfo() << "Top: " << ranges.at(i).topRow() << " Left: " << ranges.at(i).leftColumn() << "Span rows: " << ranges.at(i).rowCount() << " Span cols: " << ranges.at(i).columnCount(); setSpan(ranges.at(i).topRow(), ranges.at(i).leftColumn(), ranges.at(i).rowCount(), ranges.at(i).columnCount()); } }Try to select and merge

see the log05.02.23 15:30:39 INFO: Selected ranges: 1 05.02.23 15:30:39 INFO: Top: 0 Left: 0 Span rows: 2 Span cols: 2Oh, great! This is correct!
Try again select and merge another range

Nothing matter and see the log05.02.23 15:33:04 INFO: Selected ranges: 4 05.02.23 15:33:04 INFO: Top: 3 Left: 0 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be added 05.02.23 15:33:04 INFO: Top: 4 Left: 0 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be added 05.02.23 15:33:04 INFO: Top: 3 Left: 1 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be added 05.02.23 15:33:04 INFO: Top: 4 Left: 1 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be addedWhat happened? Why 4 ranges? (Each cell as separate range. Why?)
Thank you for your reply.
@Alexey-Serebryakov said in Get all spans in QTableWidget:
What happened? Why 4 ranges? (Each cell as separate range. Why?)
Your question is why your 4 selected cells which form a contiguous "block" are mapped by having 4 separate selection ranges of 1 item rather than 1 selection range of 4 items, is that it?
Well, why not? As QTableWidgetSelectionRange Class says:
The selections in the table may consist of several selection ranges.
Might depend on how you perform the selections (e.g. code vs user interaction)? You use
QAbstractItemView::ExtendedSelection. That allows any number of non-contiguous cells to be selected by using Ctrl+click. So maybe it implements multiple selection as individual ones to allow for this, and does not "merge" adjacent ones into a single selection? (You might see how it behaves with, say,QAbstractItemView::ContiguousSelection?) If you need to recognise that pre-process the selected ranges yourself to see if they can be "merged" into fewer, bigger ones? -
@Alexey-Serebryakov said in Get all spans in QTableWidget:
What happened? Why 4 ranges? (Each cell as separate range. Why?)
Your question is why your 4 selected cells which form a contiguous "block" are mapped by having 4 separate selection ranges of 1 item rather than 1 selection range of 4 items, is that it?
Well, why not? As QTableWidgetSelectionRange Class says:
The selections in the table may consist of several selection ranges.
Might depend on how you perform the selections (e.g. code vs user interaction)? You use
QAbstractItemView::ExtendedSelection. That allows any number of non-contiguous cells to be selected by using Ctrl+click. So maybe it implements multiple selection as individual ones to allow for this, and does not "merge" adjacent ones into a single selection? (You might see how it behaves with, say,QAbstractItemView::ContiguousSelection?) If you need to recognise that pre-process the selected ranges yourself to see if they can be "merged" into fewer, bigger ones?@JonB
No, I don't use Ctrl. Just left mouse button. Also I have my toolbar with "Merge cells" button.But if I use Ctrl+selection then that work correctly. But I don't need so.
Hmm... how can I describe.
I need following:- I select a region using only left mouse button. Release left mouse button. Click on my toolbar button to merge selected cells. Cells are merged ok.
- I doing something with sheet, insert texts in some cells and so on.
- Select another region of cells using only left mouse button. Release left mouse button. Click on my toolbar button to merge selected cells. Cells are merged ok. (But that don't work. Cells don't merged!).
-
@Alexey-Serebryakov said in Get all spans in QTableWidget:
What happened? Why 4 ranges? (Each cell as separate range. Why?)
Your question is why your 4 selected cells which form a contiguous "block" are mapped by having 4 separate selection ranges of 1 item rather than 1 selection range of 4 items, is that it?
Well, why not? As QTableWidgetSelectionRange Class says:
The selections in the table may consist of several selection ranges.
Might depend on how you perform the selections (e.g. code vs user interaction)? You use
QAbstractItemView::ExtendedSelection. That allows any number of non-contiguous cells to be selected by using Ctrl+click. So maybe it implements multiple selection as individual ones to allow for this, and does not "merge" adjacent ones into a single selection? (You might see how it behaves with, say,QAbstractItemView::ContiguousSelection?) If you need to recognise that pre-process the selected ranges yourself to see if they can be "merged" into fewer, bigger ones?@JonB Ok.
How can I span region of some cells if I've already have some spanned cells on the sheet? -
@Alexey-Serebryakov said in Get all spans in QTableWidget:
What happened? Why 4 ranges? (Each cell as separate range. Why?)
Your question is why your 4 selected cells which form a contiguous "block" are mapped by having 4 separate selection ranges of 1 item rather than 1 selection range of 4 items, is that it?
Well, why not? As QTableWidgetSelectionRange Class says:
The selections in the table may consist of several selection ranges.
Might depend on how you perform the selections (e.g. code vs user interaction)? You use
QAbstractItemView::ExtendedSelection. That allows any number of non-contiguous cells to be selected by using Ctrl+click. So maybe it implements multiple selection as individual ones to allow for this, and does not "merge" adjacent ones into a single selection? (You might see how it behaves with, say,QAbstractItemView::ContiguousSelection?) If you need to recognise that pre-process the selected ranges yourself to see if they can be "merged" into fewer, bigger ones?@JonB that is simple functionallity as in MS Excel, MS Word and so on!
-
Since you want to enlarge the span, shouldn't you rather calculate the final value and then apply that rather than making all these steps ?
-
Since you want to enlarge the span, shouldn't you rather calculate the final value and then apply that rather than making all these steps ?
@SGaist ok but why in my second case QList<QTableWidgetSelectionRange> ranges = selectedRanges(); does not return one of QTableWidgetSelectionRange with top 3, left 4, and span 2, 2?
Here some mistakes qtablewidget.cpp?
QList<QTableWidgetSelectionRange> QTableWidget::selectedRanges() const { const QList<QItemSelectionRange> ranges = selectionModel()->selection(); QList<QTableWidgetSelectionRange> result; const int rangesCount = ranges.count(); result.reserve(rangesCount); for (int i = 0; i < rangesCount; ++i) result.append(QTableWidgetSelectionRange(ranges.at(i).top(), ranges.at(i).left(), ranges.at(i).bottom(), ranges.at(i).right())); return result; } -
Since you want to enlarge the span, shouldn't you rather calculate the final value and then apply that rather than making all these steps ?
@SGaist seems to me you don't understand what I need. :-(((
May there is has some flag for table widget?
I need select some cells then click button merge. Select another cells then click button merge. Select another cells click button merge. etc. without using Ctrl and Shift keys.
-
Please provide a minimal compilable example so people can test your code and maybe see what's wrong.
-
@SGaist seems to me you don't understand what I need. :-(((
May there is has some flag for table widget?
I need select some cells then click button merge. Select another cells then click button merge. Select another cells click button merge. etc. without using Ctrl and Shift keys.
@Alexey-Serebryakov Could be a bug since the item indices have been changed from the first merge. You may need to save the initial index set and somehow map the new selection to the initial index set. Can be tricky. What are your OS and Qt version?
-
i can describe. So, just simpler
... table_->setSelectionMode(QAbstractItemView::ExtendedSelection); // May be wrong? ... void SpreadSheetWidget::mergeCells() { QList<QTableWidgetSelectionRange> ranges = selectedRanges(); qInfo() << "Selected ranges: " << ranges.count(); for (int i = 0; i < ranges.count(); i++) { qInfo() << "Top: " << ranges.at(i).topRow() << " Left: " << ranges.at(i).leftColumn() << "Span rows: " << ranges.at(i).rowCount() << " Span cols: " << ranges.at(i).columnCount(); setSpan(ranges.at(i).topRow(), ranges.at(i).leftColumn(), ranges.at(i).rowCount(), ranges.at(i).columnCount()); } }Try to select and merge

see the log05.02.23 15:30:39 INFO: Selected ranges: 1 05.02.23 15:30:39 INFO: Top: 0 Left: 0 Span rows: 2 Span cols: 2Oh, great! This is correct!
Try again select and merge another range

Nothing matter and see the log05.02.23 15:33:04 INFO: Selected ranges: 4 05.02.23 15:33:04 INFO: Top: 3 Left: 0 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be added 05.02.23 15:33:04 INFO: Top: 4 Left: 0 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be added 05.02.23 15:33:04 INFO: Top: 3 Left: 1 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be added 05.02.23 15:33:04 INFO: Top: 4 Left: 1 Span rows: 1 Span cols: 1 05.02.23 15:33:04 WARNING: QTableView::setSpan: single cell span won't be addedWhat happened? Why 4 ranges? (Each cell as separate range. Why?)
Thank you for your reply.
@Alexey-Serebryakov I have same proplem , Have you got a solution yet?