How to set cell border and background color in QTableWidgetItem?
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.
N Alex
Updated on June 30, 2022Comments
-
N Alex almost 2 years
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) andsetData()
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 about 8 yearsThanks 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. -
floppy12 about 8 yearsWhat 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
-
N Alex about 8 yearsThe 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 theQTableView::item
style, so that's why it is working. -
floppy12 about 8 yearsHmm 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.
-
N Alex about 8 yearsI 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 about 8 yearsIt works, but only the
gridline-color
is enough. As I see the styles set withsetStylesheet()
have higher priority than the ones set withsetBackground()
etc, can you provide any documentation where I can read on this? Thank you. -
MildWolfie about 8 yearsI just wanted to provide all the properties to be thorough. I have the Style Sheet Documentaiton in my bookmarks.