⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 qtableview.cpp

📁 qt-x11-opensource-src-4.1.4.tar.gz源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
        // get the vertical start and end sections (visual indexes)        int bottom = verticalHeader->visualIndexAt(area.bottom());        bottom = (bottom == -1 ? verticalHeader->count() - 1 : bottom);        if (bottom == -1)            return; // empty model        int top = 0;        bool alternateBase = false;        if (verticalHeader->sectionsHidden()) {            int verticalOffset = verticalHeader->offset();            int row = verticalHeader->logicalIndex(top);            for (int y = 0;                 ((y += verticalHeader->sectionSize(top)) <= verticalOffset) && (top < bottom);                 ++top) {                row = verticalHeader->logicalIndex(top);                if (alternate && !verticalHeader->isSectionHidden(row))                    alternateBase = !alternateBase;            }        } else {            top = verticalHeader->visualIndexAt(area.top());            if (top == -1)                return; // no visible rows inside area            alternateBase = (top & 1) && alternate;        }        Q_ASSERT(top >= 0 && top <= bottom);        // do the actual painting        for (int v = top; v <= bottom; ++v) {            int row = verticalHeader->logicalIndex(v);            if (verticalHeader->isSectionHidden(row))                continue;            int rowp = rowViewportPosition(row) + offset.y();            int rowh = rowHeight(row) - gridSize;            for (int h = left; h <= right; ++h) {                int col = horizontalHeader->logicalIndex(h);                if (horizontalHeader->isSectionHidden(col))                    continue;                int colp = columnViewportPosition(col) + offset.x();                int colw = columnWidth(col) - gridSize;                QModelIndex index = model()->index(row, col, rootIndex());                if (index.isValid()) {                    option.rect = QRect(colp, rowp, colw, rowh);                    option.state = state;                    if (sels && sels->isSelected(index))                        option.state |= QStyle::State_Selected;                    if (index == hover)                        option.state |= QStyle::State_MouseOver;                    else                        option.state &= ~QStyle::State_MouseOver;                    if (enabled) {                        QPalette::ColorGroup cg;                        if ((model()->flags(index) & Qt::ItemIsEnabled) == 0) {                            option.state &= ~QStyle::State_Enabled;                            cg = QPalette::Disabled;                        } else {                            cg = QPalette::Normal;                        }                        option.palette.setCurrentColorGroup(cg);                    }                    if (focus && index == current)                        option.state |= QStyle::State_HasFocus;                    QBrush fill;                    if (alternate) {                        fill = alternateBase                               ? option.palette.brush(QPalette::AlternateBase)                               : option.palette.brush(QPalette::Base);                    } else {                        fill = option.palette.brush(QPalette::Base);                    }                    painter.fillRect(colp, rowp, colw, rowh, fill);                    itemDelegate()->paint(&painter, option, index);                }                if (v == top && showGrid) {                    QPen old = painter.pen();                    painter.setPen(gridPen);                    painter.drawLine(colp + colw, area.top(), colp + colw, area.bottom() + 1);                    painter.setPen(old);                }            }            if (showGrid) {                QPen old = painter.pen();                painter.setPen(gridPen);                painter.drawLine(area.left(), rowp + rowh, area.right(), rowp + rowh);                painter.setPen(old);            }            alternateBase = !alternateBase && alternate;        }        option.palette.setCurrentColorGroup(state & QStyle::State_Enabled                                            ? QPalette::Normal : QPalette::Disabled);        int w = d->viewport->width();        int h = d->viewport->height();        int x = horizontalHeader->length();        int y = verticalHeader->length();        QRect b(0, y, w, h - y);        if (y < h && area.intersects(b))            painter.fillRect(b, option.palette.brush(QPalette::Base));        if (isRightToLeft()) {            QRect r(0, 0, w - x, h);            if (x > 0 && area.intersects(r))                painter.fillRect(r, option.palette.brush(QPalette::Base));        } else {            QRect l(x, 0, w - x, h);            if (x < w && area.intersects(l))                painter.fillRect(l, option.palette.brush(QPalette::Base));        }    }#ifndef QT_NO_DRAGANDDROP    // Paint the dropIndicator    d_func()->paintDropIndicator(&painter);#endif}/*!    Returns the index position of the model item corresponding to the    table item at position \a pos in contents coordinates.*/QModelIndex QTableView::indexAt(const QPoint &pos) const{    d_func()->executePostedLayout();    int r = rowAt(pos.y());    int c = columnAt(pos.x());    if (r >= 0 && c >= 0)        return model()->index(r, c, rootIndex());    return QModelIndex();}/*!    Returns the horizontal offset of the items in the table view.    Note that the table view uses the horizontal header section    positions to determine the positions of columns in the view.    \sa verticalOffset()*/int QTableView::horizontalOffset() const{    return d_func()->horizontalHeader->offset();}/*!    Returns the vertical offset of the items in the table view.    Note that the table view uses the vertical header section    positions to determine the positions of rows in the view.    \sa horizontalOffset()*/int QTableView::verticalOffset() const{    return d_func()->verticalHeader->offset();}/*!    \fn QModelIndex QTableView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers)    Moves the cursor in accordance with the given \a cursorAction, using the    information provided by the \a modifiers.    \sa QAbstractItemView::CursorAction*/QModelIndex QTableView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers){    Q_D(QTableView);    Q_UNUSED(modifiers);    if (!model())        return QModelIndex();    int bottom = model()->rowCount(rootIndex()) - 1;    // make sure that bottom is the bottommost *visible* row    while (bottom >= 0 && isRowHidden(bottom)) --bottom;    int right = model()->columnCount(rootIndex()) - 1;    // make sure that right is the rightmost *visible* column    while (right >= 0 && isColumnHidden(right)) --right;    if (bottom == -1 || right == -1)        return QModelIndex(); // model is empty    QModelIndex current = currentIndex();    if (!current.isValid()) {        int row = 0;        int column = 0;        while (column < right && isColumnHidden(column))            ++column;        while (isRowHidden(row) && row < bottom)            ++row;        return model()->index(row, column, rootIndex());    }    int visualRow = verticalHeader()->visualIndex(current.row());    Q_ASSERT(visualRow != -1);    int visualColumn = horizontalHeader()->visualIndex(current.column());    Q_ASSERT(visualColumn != -1);    if (isRightToLeft()) {        if (cursorAction == MoveLeft)            cursorAction = MoveRight;        else if (cursorAction == MoveRight)            cursorAction = MoveLeft;    }    switch (cursorAction) {    case MoveUp:        --visualRow;        while (visualRow > 0 && isRowHidden(verticalHeader()->logicalIndex(visualRow)))            --visualRow;        break;    case MoveDown:        ++visualRow;        while (visualRow < bottom && isRowHidden(verticalHeader()->logicalIndex(visualRow)))            ++visualRow;        break;    case MovePrevious: {        int left = 0;        while (isColumnHidden(left) && left < right)            ++left;        if (visualColumn == left) {            visualColumn = right;            int top = 0;            while (top < bottom && isRowHidden(verticalHeader()->logicalIndex(top)))                ++top;            if (visualRow == top)                visualRow = bottom;            else                --visualRow;            while (visualRow > 0 && isRowHidden(verticalHeader()->logicalIndex(visualRow)))                --visualRow;            break;        } // else MoveLeft    }    case MoveLeft:        --visualColumn;        while (visualColumn > 0 && isColumnHidden(horizontalHeader()->logicalIndex(visualColumn)))            --visualColumn;        break;    case MoveNext:        if (visualColumn == right) {            visualColumn = 0;            while (visualColumn < right                   && isColumnHidden(horizontalHeader()->logicalIndex(visualColumn)))                ++visualColumn;            if (visualRow == bottom)                visualRow = 0;            else                ++visualRow;            while (visualRow < bottom && isRowHidden(verticalHeader()->logicalIndex(visualRow)))                ++visualRow;            break;        } // else MoveRight    case MoveRight:        ++visualColumn;        while (visualColumn < right               && isColumnHidden(horizontalHeader()->logicalIndex(visualColumn)))            ++visualColumn;        break;    case MoveHome:        visualColumn = 0;        while (visualColumn < right               && isColumnHidden(horizontalHeader()->logicalIndex(visualColumn)))            ++visualColumn;        if (modifiers & Qt::ControlModifier) {            visualRow = 0;            while (visualRow < bottom && isRowHidden(verticalHeader()->logicalIndex(visualRow)))                ++visualRow;        }        break;    case MoveEnd:        visualColumn = right;        if (modifiers & Qt::ControlModifier)            visualRow = bottom;        break;    case MovePageUp: {        int newRow = rowAt(visualRect(current).top() - d->viewport->height());        return model()->index(qBound(0, newRow, bottom), current.column(), rootIndex());    }    case MovePageDown: {        int newRow = rowAt(visualRect(current).bottom() + d->viewport->height());        if (newRow < 0)            newRow = bottom;        return model()->index(qBound(0, newRow, bottom), current.column(), rootIndex());    }}    int logicalRow = verticalHeader()->logicalIndex(visualRow);    int logicalColumn = horizontalHeader()->logicalIndex(visualColumn);    if (!model()->hasIndex(logicalRow, logicalColumn, rootIndex()))        return QModelIndex();    QModelIndex result = model()->index(logicalRow, logicalColumn, rootIndex());    if (!isIndexHidden(result))        return model()->index(logicalRow, logicalColumn, rootIndex());    return QModelIndex();}/*!    \fn void QTableView::setSelection(const QRect &rect,    QItemSelectionModel::SelectionFlags flags)    Selects the items within the given \a rect and in accordance with    the specified selection \a flags.*/void QTableView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command){    QModelIndex tl = indexAt(QPoint(isRightToLeft() ? rect.right() : rect.left(), rect.top()));    QModelIndex br = indexAt(QPoint(isRightToLeft() ? rect.left() : rect.right(), rect.bottom()));    if (!selectionModel() || !tl.isValid() || !br.isValid())        return;    if (!tl.isValid() || !br.isValid())        return;    bool verticalMoved = verticalHeader()->sectionsMoved();    bool horizontalMoved = horizontalHeader()->sectionsMoved();    QItemSelection selection;    if (verticalMoved && horizontalMoved) {        int top = verticalHeader()->visualIndex(tl.row());        int left = horizontalHeader()->visualIndex(tl.column());        int bottom = verticalHeader()->visualIndex(br.row());        int right = horizontalHeader()->visualIndex(br.column());        for (int horizontal = left; horizontal <= right; ++horizontal) {            int column = horizontalHeader()->logicalIndex(horizontal);            for (int vertical = top; vertical <= bottom; ++vertical) {                int row = verticalHeader()->logicalIndex(vertical);                QModelIndex index = model()->index(row, column, rootIndex());                selection.append(QItemSelectionRange(index));            }        }    } else if (horizontalMoved) {        int left = horizontalHeader()->visualIndex(tl.column());        int right = horizontalHeader()->visualIndex(br.column());        for (int visual = left; visual <= right; ++visual) {            int column = horizontalHeader()->logicalIndex(visual);            QModelIndex topLeft = model()->index(tl.row(), column, rootIndex());            QModelIndex bottomRight = model()->index(br.row(), column, rootIndex());            selection.append(QItemSelectionRange(topLeft, bottomRight));        }    } else if (verticalMoved) {        int top = verticalHeader()->visualIndex(tl.row());        int bottom = verticalHeader()->visualIndex(br.row());        for (int visual = top; visual <= bottom; ++visual) {            int row = verticalHeader()->logicalIndex(visual);            QModelIndex topLeft = model()->index(row, tl.column(), rootIndex());            QModelIndex bottomRight = model()->index(row, br.column(), rootIndex());            selection.append(QItemSelectionRange(topLeft, bottomRight));        }    } else { // nothing moved        selection.append(QItemSelectionRange(tl, br));    }    selectionModel()->select(selection, command);}/*!    \internal    Returns the rectangle from the viewport of the items in the given    \a selection.*/QRegion QTableView::visualRegionForSelection(const QItemSelection &selection) const{    Q_D(const QTableView);    if (selection.isEmpty())        return QRegion();    QRegion selectionRegion;    bool verticalMoved = verticalHeader()->sectionsMoved();    bool horizontalMoved = horizontalHeader()->sectionsMoved();    if (verticalMoved && horizontalMoved) {        for (int i = 0; i < selection.count(); ++i) {            QItemSelectionRange range = selection.at(i);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -