Important: Please read the Qt Code of Conduct -

QTableView focusCell - background color transparent

  • I would like to style the focusCell in a QTableView onyl with a border,
    but without a (maybe styled) background.

    So the background color of a cell with focus shall have the same color
    as the background of the cell (without focus)

    Any suggestions how to proceed ?

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should be able to achieve that either using QPalette or QStyleSheet. Another option would be to write a QStyledItemDelegate and reimplement the paint method.

  • Hi thanks for your answer...
    I render already the table cells in the derived QStyledItemDelegate.

    drawControl(QStyle::CE_ItemViewItem, &opt, painter, widget);
    My question is, how to render the selected cell (With which style items QStyle::CE_xxx???).

  • Lifetime Qt Champion

    Can you show the code of your paint function ?

  • So after some hours debugging the QStyle::drawControl code in the library I have found a solution.

    The problem were that we are using an application style sheet.
    So the used style for rendering the tablecell and the content is QStyleSheetStyle.

    If the application is working with QStyleSheetStyle it seem to be not possible to render the content of a tabelcell selection
    without a stylesheet.

    After using another style (the fusion style in this case) the styling of the selected tablecell is possible.

    If somebody is interested in the working code:

    // somewhere in the constructor
    // m_unstyledView is type of QTableView*
    // m_fusionStyle  is type of QStyle*
    m_unstyledView = new QTableView; 
    m_fusionStyle = QStyleFactory::create(s3Utf8String("fusion"));
    void DataTableDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, 
                                  const QModelIndex& index) const
        QStyleOptionViewItemV4 opt = option;
        initStyleOption(&opt, index);
        opt.palette.setColor(QPalette::All, QPalette::Highlight, QColor::fromRgb(255, 255, 0));
        opt.palette.setColor(QPalette::All, QPalette::HighlightedText, QColor::fromRgb(255, 0, 0));
        m_fusionStyle->drawControl(QStyle::CE_ItemViewItem, &opt, painter, m_unstyledView);

Log in to reply