3

I have set the color of my main window in QT to be grey.

ui(new Ui ::MainWindow)
ui-> setupUi(this)
this->setStyleSheet("background-color: grey;");

I have tried multiple ways to set the color of the QFrame, however it takes on the default grey color that I have set. One way I tried is below.

ui->frame->setStyleSheet("color:rgb(255,255,255)");

I have tried to change the color of the QFrame by using the setStyleSheet method but no matter which color I assign it remains grey. I have tried setting the background, border, and color. Is there any way to do this that I am overlooking?

Gage Haas
  • 51
  • 1
  • 2
  • 8
  • 1
    Possible duplicate of [how to change background color of Qwidget irrespective of its parents background color](https://stackoverflow.com/questions/28538345/how-to-change-background-color-of-qwidget-irrespective-of-its-parents-background) – eyllanesc Mar 13 '18 at 00:17

5 Answers5

5

You need to set the background color of the QFrame.

Set the QFrame's style sheet to the following:

"background-color: rgb(255, 255, 255);"
Nicholas Johnson
  • 1,012
  • 2
  • 12
  • 35
  • The issue I am having with this is that I want to only set the border color of the QFrame. I am attempting to set it like this ui->frame->setStyleSheet("QFrame{border: 5px solid black; }"); – Gage Haas Mar 13 '18 at 00:33
4

For Python (PyQt) users:

frame = QFrame(self)
frame.setStyleSheet('background-color: rgb(50,50,50)')  
3

Set a MainWindow (not the QFrame) StyleSheet like this:

QMainWindow{
   background-color: gray
}
QFrame { 
   border: 5px solid black 
} 

This worked for me:

mainwindow->setStyleSheet("QMainWindow{background-color: gray} QFrame { border: 5px solid black } ");
Yanet
  • 171
  • 1
  • 7
0

This troubled me quite some time. Before setting the actual stylesheet unset it first:

    ui->frame->setStyleSheet("");
    ui->frame->setStyleSheet("background-color: rgb(255,255,255)");

For more customisation options have a look at https://doc.qt.io/qt-5/stylesheet-examples.html and for larger projects you might want to think about setting up global stylesheets for your app.

MAPster
  • 68
  • 6
0

This works for me:

ui->frame->setStyleSheet("background-color: rgb(251, 255, 206);");
PeterN
  • 217
  • 1
  • 4
  • 15