Qt QTabWidget background color - c++

I have been trying to set the background color of a QTabWidget to black (or any other color for), but have been unsuccessful in doing so.
It seems that you need the option autoFillBackground set and then also set "background-color: black;" in the stylesheet. This then displays it properly in the Designer, but fails in the application.
This answer suggests to enclose it in another QWidget and then use the transparency, but that is a hack around the issue.
How do I set the background color of a QTabWidget via stylesheets?
EDIT
Setting QTabBar { background-color: black; } results in the following image.

As an alternative to QTreeWidget, use QTabBar + QStackedWidget and the following stylesheet
QTabBar { background-color: black; }
or use
Qt: Styling QTabWidget

Related

Changing the color of a QFrame in QT

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?
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);"
For Python (PyQt) users:
frame = QFrame(self)
frame.setStyleSheet('background-color: rgb(50,50,50)')
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 } ");
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.
This works for me:
ui->frame->setStyleSheet("background-color: rgb(251, 255, 206);");

How to set only QTabWidget background color stylesheet

I have a Qt application that, among many other widget types, uses a QTabWidget. I'm having difficulty styling the background color for this object.
I've tried some of the following lines, which I found from other forum posts, in my stylesheet with no effect on the program
QTabWidget { background-color: black; }
QTabWidget::pane { background-color: black; }
QTabWidget#tabWidget { background-color: black; }
QTabWidget#tabWidget::pane { background-color: black; }
If I use QWidget { background-color: black; }, then yes my color is properly changed, but then all of the other widgets in my program are changed as well... so this isn't what I'm looking for...
I've also tried it in code with ui->tabWidget->setStyleSheet("background-color: black"); But this too is undesirable because it changes the background color of all of its children widgets.
Does anyone have any other ideas on how to style a QTabWidgets contents background area?
About a year late but I recently ran into the same problem and got it working.
First of all QTabWidget has a child QWidget for every tab you make. That is the area that you put your other widgets into, and that is what you want to set the background color of.
Set the style-sheet by doing this.
1)Determine the name of your tab widgets from the design object window top right, they should match the currentTabName that you set when creating your tab.
2)Realize this is a QWidget not a QTabWidget this is why QTabWidget { background-color: black; } does not work.
3)Realize that by specifying the object in the style-sheet with the '#' the child object will not inherit the style-sheet.
For me I specified my style-sheet as such, repeating for each tab object name that I had:
#objectName {background-color: rgb(240,240,240);}
This provided me with the exact behavior I needed. In my case I wanted to get the natural gray background onto my Tab pages but not override the child components on the tab pages.
I hope this helps someone in the future...

Qt 5, QML 2.0 and transparent borderless window incompatibility

Tasks:
Rounded-corners window
Custom borders
QML in QWidget
Because there is no way to change border style in parent widget, I made two of them. First parent widget is transparent, it's set in QSS. Second one is the one with rounded-corners, also set in QSS. p.s. I guess it isn't smartest idea. Not even close.
To make it work, I've set up Qt::WA_TranslucentBackground attribute.
Set up Qt::FramelessWindowHint window flag.
QML 2.0 style.[in code below]
So problem is:
When WA_TranslucentBackground and FramelessWindowHint is on - NO QML
When FramelessWindowHint is on - Only QML view
When WA_TranslucentBackground or both are off - still system BORDERS
Also I've tried to set mask on MainWindow
QPixmap pixmap = QWidget::grab(this->rect());
this->setMask(pixmap.createMaskFromColor(Qt::transparent, Qt::MaskInColor));
Works well, no system borders, custom one is rounded and QML works, but whole other area became white, don't know how to fix.
MainWindow:
QFile styleFile("style.qss");
styleFile.open(QFile::ReadOnly), qApp->setStyleSheet(QLatin1String(styleFile.readAll()));
QWidget *container = QWidget::createWindowContainer(new QQuickView(QUrl("main.qml")), this);
ui->verticalLayout->addWidget(container);
//this->setAttribute(Qt::WA_TranslucentBackground, true);
//this->setWindowFlags(Qt::FramelessWindowHint | Qt::Window);
QPixmap pixmap = QWidget::grab(this->rect());
this->setMask(pixmap.createMaskFromColor(Qt::transparent, Qt::MaskInColor));
Styles:
QWidget#MainWindow{
background:transparent;
}
QWidget#MainWindowRounded{
background-color: grey;
border: 1px solid black;
border-radius: 5px;
}
Is this possible to resolve?
HERE SOURCE CODE

QWebView set border visible

I want to make QWebView widget have borders in my layout and UI when running, similar to QTableView. Now it looks borderless and hidden.
Is it even possible?
It's not possible to set border to QWebView. Instead you can place your QWebView inside another QWidget and set it's border. See example below (QtDesigner):
Widgets hierarchy:
Look inside QtDesigner:
StyleSheet for QFrame:
QFrame
{
border: 1px solid black;
background: white;
}

Problem in stylesheet of Qt APP

In my app, i have a section that is top widget, the color of the top widget is gray, and i've been put severl widget on top widget, like QComboBox, QLineEdit and 2 QButton, but i have a problem when i right click on QLineEdit as you seen in below picture, the color of default context of window is gray, or when i open the QComboBox the color of background is gray. I'll set the background color of two these widget to white but doesn't work. So, how can i fix this?
Sample for better understand:
http://0000.4.img98.net/out.php/i52512_problem.png
Please help me
The style sheet propagates to all the child widgets, so you have to limit their range by using the right selectors. Since the context menu is a child of the QLineEdit it is also affected.
// What you have probably done:
myLineEdit->setStyleSheet("background-color: gray");
// What you should have done:
myLineEdit->setStyleSheet("QLineEdit { background-color: gray }");
// What you should do if there might be child widgets of the same type
// but for which you don't want the style to apply:
myLineEdit->setObjectName("myLineEdit");
myLineEdit->setStyleSheet("QLineEdit#myLineEdit { background-color: gray }");
See "The Style Sheet Syntax - Selector Types" for details.