customizing tab control in stylesheet in qt - qt

I would like to do changes only for a specific tab. How can I do it?
I tried both:
QTabBar::tab#tbGeneral{... }
QTabWidget::tab-bar#tbGeneral{... }
None worked.

You have probably looked into Customizing QTabWidget and QTabBar.
To style individual tabs based on their state (:only-one, :first, :last, :middle, :previous-selected, :next-selected, :selected) you can use stylesheet-code similar to this:
QTabBar::tab {
border: 1px solid #C4C4C3;
border-bottom-color: #C2C7CB;
border-top-left-radius: 4px;
border-top-right-radius: 4px;
padding: 1px 3px;
margin-left: 1px;
margin-bottom: 4px;
}
QTabBar::tab:selected {
background-color: #f14040;
border-bottom-style: none;
}
As the individual tabs are not widgets (or objects), they have no object name or other properties that could identify them to a stylsheet. You can only use the pseudo-classes to style tabs with a stylesheet.
You'll probably have to use (C++) code to change the style of a tab depending on the label. The recommended way for customizing styles in Qt is through the class QStyle. You can either subclass QStyle or use QProxyStyle to change the looks of specific widgets. The other alternative (probably not recommended by Qt) is though subclassing QTabBar and reimplementing the function QWidget::paintEvent( QPaintEvent *event).

Related

QDockWidget Tab Stylessheet

How I can change these tabs at the bottom from the design? I just can't find the prefix needed to color the tabs to match my design.
Assuming that those tabs are part of a QTabWidget, you can either modify the stylesheet in Designer with something like this:
QTabBar:tab { color: white; border: 2px solid #00FFFF; background-color: gray;}
Or you can do it via code like this:
ui->tabWidget->setStyleSheet(QStringLiteral ("QTabBar:tab { color: white; border: 2px solid #00FFFF; background-color: gray;}"));
You would obviously need to change the colors in the example to the colors you would want. You should also read Customizing QTabWidget for more examples and the full reference of what is possible.

Is there a way to set an imported library so that it ignores my custom CSS?

In my react project, I styled the buttons like this in an external stylesheet file called project.css:
button {
max-width: 150px;
margin: 20px 0;
padding: 12px 20px;
border-style: none;
border-radius: 5px;
color: #fff;
cursor: pointer;
outline: none;
-webkit-appearance: none;
}
This has been working fine, however I recently installed some libraries that also use buttons. My button style that you see above, is interfering with the style the libraries use.
I was wondering how I could keep my button styles, but have the libraries I use ignore my custom button styles.
Is this doable?
Thanks!
Depending on the structure of your code and the libraries code there are a few options.
Load the library styles after your own. When two selectors have the same specificity the last one loaded applies.
Scope your selector under a parent selector unique to your application
.mycode button {
...
}
Migrate your selector to a class instead of targetting the button tag.
.mybutton {
...
}
In general its more flexible to target a custom css class than the tags themselves.

QFrame's stylesheet affecting QLabel

I am using QFrame just to have colored border, since I couldn't find
the a way to change color of the QDialog. So because of tampering with
QFrame's border, it is also affecting the QLabel's look, is there any
way to avoid this?
Edit:
Here is the Stylesheet which I am using, where QLabels' doesn't have any effect. It's taking QFrames'
QWidget {
background-color: black;
}
QLabel {
color:white;
border: solid 2px black;
font: bold 19px Sans Serif;
}
QFrame {
border: solid 2px white;
border-radius: 4px;
}
Instead of using a type selector which matches all the instances of that class and its subclasses, use a class selector.
So in your stylesheet, instead of using QFrame{...}, use .QFrame{border: 1px solid red;}. Note the . before the class name.
See more about selector types here.

Qt 4: How to set outer border for QWidget so that its inner widgets are unaffected?

I have quite a strange problem. I have a QWidget with QHBoxLayout on it. The layout contains two QLabels.
I want to set a border for this whole widget. I'm using style sheet:
"padding: 10px;"
"border-style: solid;"
"border-width: 3px;"
"border-radius: 7px;"
But here's the problem: this style is applied to both QLabels and completely breaks the layout. I only need the outer window to have the border, not the labels.
Any ideas?
Thanks in advance!
Use
.QWidget
{
// your css rules
}
.QWidget will apply CSS only to classes that are EXACTLY QWidget and not inheriting QWidget
You can also use object name selector
#YourWidgetObjectName
{
// your css rules
}
Both solutions wont apply rules to other widgets (even those inside)
Style sheets will work recursively. If you apply style sheet to a Application it will be applied to all widgets within it. So you may have to specify to what you want to apply style sheet?
logic should be something like this..
QHBoxLayout#layoutbox {
background-color: red;
border-style: outset;
border-width: 2px;
border-radius: 10px;
border-color: beige;
font: bold 14px;
min-width: 10em;
padding: 6px;
}

Customizing QHeaderView resize handle

I use the cleanlooks style for my application which fits best the look and feel I want.
The annoying thing a stumbled on is that the QHeaderView (horizontal header of a QTableWidget for instance)
doesn't paint the resize handle between sections when running uner an Unix host.
what I want:
what I get:
I started to search a solution using style sheets but it seems there is no way to control the handle rendering.
Do I have to play with borders style ?
Is there anything obvious thing I am missing?
I don't want to subclass QHeaderView or QStyle for such a little (trivial ?) problem.
Any help appreciated.
Here is what i found so far:
QHeaderView::section:horizontal{
margin-top: 4px;
margin-bottom: 4px;
border-style: solid;
border-left-width: 1px;
border-left-color: white;
border-right-color: darkgray;
border-right-width: 1px;
}
QHeaderView::section:horizontal:first{
border-left-color: darkgray;
}
which gives this result:

Resources