Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Get all spans in QTableWidget
Forum Updated to NodeBB v4.3 + New Features

Get all spans in QTableWidget

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.3k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Alexey Serebryakov

    @SGaist

    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
    a4384a3d-f5c1-44a0-a64c-21448295d9c9-изображение.png
    see the log

    05.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:  2
    

    Oh, great! This is correct!

    Try again select and merge another range
    bd18a67c-bf7d-406b-8c10-4357434b15ab-изображение.png
    Nothing matter and see the log

    05.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 added
    

    What happened? Why 4 ranges? (Each cell as separate range. Why?)

    Thank you for your reply.

    JonBJ Offline
    JonBJ Offline
    JonB
    wrote on last edited by JonB
    #4

    @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?

    A 3 Replies Last reply
    0
    • JonBJ JonB

      @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?

      A Offline
      A Offline
      Alexey Serebryakov
      wrote on last edited by Alexey Serebryakov
      #5

      @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:

      1. 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.
      2. I doing something with sheet, insert texts in some cells and so on.
      3. 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!).
      1 Reply Last reply
      0
      • JonBJ JonB

        @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?

        A Offline
        A Offline
        Alexey Serebryakov
        wrote on last edited by
        #6

        @JonB Ok.
        How can I span region of some cells if I've already have some spanned cells on the sheet?

        1 Reply Last reply
        0
        • JonBJ JonB

          @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?

          A Offline
          A Offline
          Alexey Serebryakov
          wrote on last edited by
          #7

          @JonB that is simple functionallity as in MS Excel, MS Word and so on!

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #8

            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 ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            A 2 Replies Last reply
            1
            • SGaistS SGaist

              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 ?

              A Offline
              A Offline
              Alexey Serebryakov
              wrote on last edited by Alexey Serebryakov
              #9

              @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;
              }
              
              1 Reply Last reply
              0
              • SGaistS SGaist

                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 ?

                A Offline
                A Offline
                Alexey Serebryakov
                wrote on last edited by Alexey Serebryakov
                #10

                @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.

                JoeCFDJ 1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  Please provide a minimal compilable example so people can test your code and maybe see what's wrong.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  0
                  • A Alexey Serebryakov

                    @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.

                    JoeCFDJ Offline
                    JoeCFDJ Offline
                    JoeCFD
                    wrote on last edited by JoeCFD
                    #12

                    @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?

                    1 Reply Last reply
                    0
                    • A Alexey Serebryakov

                      @SGaist

                      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
                      a4384a3d-f5c1-44a0-a64c-21448295d9c9-изображение.png
                      see the log

                      05.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:  2
                      

                      Oh, great! This is correct!

                      Try again select and merge another range
                      bd18a67c-bf7d-406b-8c10-4357434b15ab-изображение.png
                      Nothing matter and see the log

                      05.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 added
                      

                      What happened? Why 4 ranges? (Each cell as separate range. Why?)

                      Thank you for your reply.

                      M Offline
                      M Offline
                      maihai
                      wrote on last edited by
                      #13

                      @Alexey-Serebryakov I have same proplem , Have you got a solution yet?

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved