M
M
Mercury132016-07-22 16:45:01
Qt
Mercury13, 2016-07-22 16:45:01

QTableView: how to highlight the current table row?

Set selectionBehavior = SelectRows. The effect is too rough when the entire line is blue.
What else can be done? What are the options?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2017-07-31
@Mercury13

void ForecastReport::Delegate::initStyleOption(
        QStyleOptionViewItem * option,
        const QModelIndex & index) const
{
    Super::initStyleOption(option, index);
    if (index.column() != owner.table->currentIndex().column()
                && (option->state & QStyle::State_Selected)) {
        fixupStyleOption(option, cache);
    }
}

// предок ForecastReport::Delegate
void MyTableDelegate::fixupStyleOption(
        QStyleOptionViewItem * option,
        QBlendCache& cache)
{
    QBrush& brush = option->backgroundBrush;
    QColor cl = brush.color();
    if (brush.style() == Qt::NoBrush) {
        cl = AdvancedTableModel::BgColor::back();   // это QApplication::palette().base().color();
        brush.setStyle(Qt::SolidPattern);
    }
    brush.setColor(cache(cl, Qt::black, 180));
    option->state &= ~QStyle::State_Selected;
}

QColor QBlendCache::operator()(const QColor& aFg, const QColor& aBg, int aAlpha)
{
    if (fg != aFg || bg != aBg || alpha != aAlpha) {
        fg = aFg;
        bg = aBg;
        alpha = aAlpha;
        result = qBlend(aFg, aBg, aAlpha);
    }
    return result;
}

namespace {
    float fGamma = 2.2f;
    float fInvGamma = 1.0f / 2.2f;
}

QColor qBlend(const QColor& aFg, const QColor& aBg, int aValue)
{
    qreal rf, gf, bf, rb, gb, bb, a, ca;
    aFg.getRgbF(&rf, &gf, &bf);
    aBg.getRgbF(&rb, &gb, &bb);
    a = static_cast<qreal>(aValue) / static_cast<qreal>(255);
    ca = static_cast<qreal>(1) - a;
    rf = pow(pow(rf, fGamma) * a + pow(rb, fGamma) * ca, fInvGamma);
    gf = pow(pow(gf, fGamma) * a + pow(gb, fGamma) * ca, fInvGamma);
    bf = pow(pow(bf, fGamma) * a + pow(bb, fGamma) * ca, fInvGamma);
    return QColor::fromRgbF(rf, gf, bf);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question