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

QTableView background-color lets text disappear



  • I subclassed QTableWidget as follows:

    @MyTableWidget::MyTableWidget(QWidget *parent)
    : QTableWidget(parent)
    {
    setStyleSheet("QTableView{ color: blue; background-color: transparent; }");
    }

    void MyTableWidget::paintEvent(QPaintEvent *event)
    {
    QTableWidget::paintEvent(event);
    QStyleOption option;
    option.init(this);
    QPainter painter(viewport());
    style()->drawPrimitive(QStyle::PE_Widget, &option, &painter, this);
    }@

    This works perfectly, my table cells' text is blue on a transparent background. However, the moment I change the table view's background-color to anything but transparent, the background-color shows, but the text of the table cells disappear.

    I'm not sure how QTableView is implmented, but since transparent works, I assume that somehow the background is moved forward (and placed over the text) once the stylesheet is set (only happens if stylesheet has background-color). So if transparent is set, you can basically view the text "through" the background.

    Any suggestions on how to overcome this problem?


Log in to reply