How to set cell border and background color in QTableWidgetItem?
<p>I have a <code>QTableWidget</code> with 3 columns. 2 of the columns have some text in them, but one of them is empty and I want it to have a background color. Also I want the cells to have borders.</p>

<p>If I do </p>

<pre><code>int i = 0;
foreach (tableData el, data) {
    //setting the first cell

    ui-&gt;eventTable-&gt;setItem(i, 1, new QTableWidgetItem); 
    //ui-&gt;eventTable-&gt;item(i, 1)-&gt;setBackground(el.severityColor);
    //ui-&gt;eventTable-&gt;item(i, 1)-&gt;setBackgroundColor(el.severityColor);
    ui-&gt;eventTable-&gt;item(i, 1)-&gt;setData(
        Qt::BackgroundRole,
        QBrush(el.severityColor)
    );

    //setting the third cell

    ++i;

}
</code></pre>

<p>everything works as expected.</p>

<p>But if before this code I try to add a border with </p>

<pre><code>QString style(
    "QTableWidget::item {"
        "border: 1px solid white;"
    "}"
);
ui-&gt;eventTable-&gt;setStyleSheet(style);
</code></pre>

<p>the background is not set.</p>

<p>I tried with <code>setBackground()</code>, <code>setBackgroundColor()</code> (even though it is deprecated) and <code>setData()</code> as seen in the code, but the result is the same.</p>

<p>Also I tried <code>setShowGrid(true)</code> insted of the above stylesheet, but the border didn't show.</p>

<p>You can reproduce this by creating a table with 1 row and 1 column and trying to set the background of the cell, and then adding the stylesheet for a border.</p>

<p><strong>Am I missing something? What else should I try?</strong>
<strong>As an alternative, can I target specific rows/cells in the styles, so I can build a stylesheet string that does what I want?</strong></p>

<p><strong>Edit:</strong>
I can have another styles in <code>QTableWidget::item</code> and they are applied, the problem is when I have a border.
I also tried to write the border style as:</p>

<pre><code>border-width: 1px;
border-style: solid;
border-color: white;
</code></pre>

<p>but still no luck.
Also if I set the background from styles, it works. It doesn't work if I set it in code.</p>