Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QTableView/QTableWidget Cell Span Range



  • Hello,

    Is there a way to get the range of a spanned cell from any position within the spanned cell?

    For example, I can use QTableView::rowSpan(1, 1) and QTableView::columnSpan(1, 1) to find that the cell with "B" in it has a span of 2x2; but how can I find the beginning of the spanned range if I query cell at row 2 and column 2 (the bottom right corner of cell "B")? I need a way to get back (row 1, column 1) from it.

    alt text

    Thank you!


  • Lifetime Qt Champion

    I think it should be possible through QTableView::rowSpan() and QTableView::columnSpan()



  • Thank you @Christian-Ehrlicher. Unfortunately, all QTableView::rowSpan() and QTableView::columnSpan() tells me is what the total span is, but it doesn't tell me which cell it starts at, so if I had a span of 3x3 and I checked the rowSpan and colSpan of the middle cell, it would tell me 3 and 3.


  • Lifetime Qt Champion

    From looking a little bit at the sources you have to remember it by youself. No chance to get it out of the QTableView



  • @Adam-Crowe
    I don't see there is a quicker way than: you must iterate through the columns/rows (left-to-right/top-to-bottom) looking at the current value of row/columnSpan() in each cell. From that you can obviously calculate where a given cell coordinate really lies in your terms.

    @Christian-Ehrlicher : You say "No chance to get it out of the QTableView". I agree there is not a simple call to return this, but I say you can calculate it in loops. Have I said anything incorrect here?



  • Thank you everybody,

    I wrote this little thing just now and it seems to work:

    struct Coordinates { int row; int col; };
    
    Coordinates getFirstCellInSpan(const int &row, const int &col)
    {
        Coordinates xy;
    
        int firstRow = 0;
        int firstCol = 0;
    
        while (firstRow + rowSpan(firstRow, col) <= row)
        {
            firstRow++;
        }
    
        while (firstCol + columnSpan(firstRow, firstCol) <= col)
        {
            firstCol++;
        }
    
        xy.row = firstRow;
        xy.col = firstCol;
    
        return xy;
    }
    


  • @Adam-Crowe
    Exactly, as I said you have to iterate to find out, just as you have done :) Qt could have provided such a function, but it does not, and I don't know that internally it saves the data in any way which would be quicker than this; and even if it does, there is no public API for you to access that, other than what you have done.


Log in to reply