3

I have a QTableWidget 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.

If I do

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

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

    //setting the third cell

    ++i;

}

everything works as expected.

But if before this code I try to add a border with

QString style(
    "QTableWidget::item {"
        "border: 1px solid white;"
    "}"
);
ui->eventTable->setStyleSheet(style);

the background is not set.

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

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

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.

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

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

border-width: 1px;
border-style: solid;
border-color: white;

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

N Alex
  • 1,014
  • 2
  • 18
  • 29

2 Answers2

9

Here are the properties you need to get your table styled appropriately. Note that gridline-color is the property that defines the borders of the items, which is defined in QTableView and not QTableView::item.

QTableView
{
    color: {color};
    border: 1px solid {color};
    background: {color};
    gridline-color: {color};
}
QTableView::item
{
    background: {color};
}

Obviously you would replace {color} with the appropriate colors for each property.

MildWolfie
  • 2,492
  • 1
  • 16
  • 27
  • It works, but only the `gridline-color` is enough. As I see the styles set with `setStylesheet()` have higher priority than the ones set with `setBackground()` etc, can you provide any documentation where I can read on this? Thank you. – N Alex Mar 11 '16 at 16:06
  • I just wanted to provide all the properties to be thorough. I have the [Style Sheet Documentaiton](http://doc.qt.io/qt-4.8/stylesheet-reference.html) in my bookmarks. – MildWolfie Mar 11 '16 at 16:09
0

I guess you need to style the background and border properties

QTableView {
    background-color : none;
}

QTableView::item {
    border: 1px solid white;
    background-color : none;
}
floppy12
  • 1,045
  • 6
  • 12
  • Thanks for the answer, but it doesn't fix the problem. I think that `QTableWidget::item` is valid, since it does style the cell and adds the border. Also the style provided by you does the same, adds the border, but when used, the background color does not work. – N Alex Mar 09 '16 at 15:17
  • What about trying to set the background-color both for QTableWidget and items ? Btw I'll update or remove my answer in case it's not working – floppy12 Mar 09 '16 at 15:21
  • The cell background color seems to work with the above style, but now the border isn't showing anymore. And something strange is happening, if I add an `;` affter the first `}` then the color is working and the border is not set, if I remove the `;` then the color is not working and the border is set. I am guessing that the `;` disables the `QTableView::item` style, so that's why it is working. – N Alex Mar 09 '16 at 15:49
  • Hmm strange... Do you use other stylesheets in your project which could be propagated / could overwrite the above style ? Set the background-color to red to see if you can spot something like that. – floppy12 Mar 09 '16 at 16:45
  • I tried to set to red and it worked. Also I used `table->styleSheet()` and it returned only the added styles (the ones in the answer). Also I tried creating a new project with just a table, and the results are the same. You can't reproduce the problem? – N Alex Mar 09 '16 at 16:47