How to add a CSS Style Class to a QWidget - c++

In JavaFx I can easily add a CSS style class as follows:
Scene scene = new Scene(new Group(), 500, 400);
scene.getStylesheets().add("path/stylesheet.css");
......
Label label = new Label("Cool Looking Styled Label");
label.getStyleClass().add("my-label-style");
css
.my-label-style {
-fx-font: 16px "Serif";
-fx-padding: 10;
-fx-background-color: #CCFF99;
}
How can I go about adding a style class to a QWidget, QLabel, for example?

label->setProperty("class", "my-label-style");
Then in a CSS you can call it normally by:
.my-label-style {
[..]
}

The method is in the QWidget base class; it's QWidget::setStyleSheet.

you have two ways to do that
coding
widget.setStyleSheet("css code");
Form ui
right click on the widget
select change stylesheet
a dialog box appears where you need to write css codes
but all css code are not supported ,you need to see documentation of stylesheet

Related

How to set QToolButton fill in the side bar(QDockWidget) in Qt?

I wrote a minimal example which has a sidebar which contains a QToolButton on it. I set setAutoRaise(true) for the QToolButton, so when hover on it, the button will raise. But currently I have a minor issue. As you can see from the picture below, when hover on the button, the border on the right and left are not fully take the whole screen.
Here is how that looks like:
And this example what I want to button to look like:
And here is my code:
sidebarDock = new QDockWidget(this);
addDockWidget(Qt::LeftDockWidgetArea, sidebarDock);
//hide dock widget title bar
QWidget *titleBarWidget = new QWidget(sidebarDock);
sidebarDock->setTitleBarWidget(titleBarWidget);
sidebarDock->titleBarWidget()->hide();
dockWidget = new QWidget(sidebarDock);
dockWidget->setObjectName("DockWidget");
dockWidget->setStyleSheet("#DockWidget { background-color: #F7DC6F; }");
dockVLayout = new QVBoxLayout(dockWidget);
overviewBtn = new QToolButton(dockWidget);
overviewBtn->setAutoRaise(true);
overviewBtn->setIcon(QIcon(":/Icons/overview.png"));
overviewBtn->setText("Overview");
overviewBtn->setToolButtonStyle(Qt::ToolButtonTextUnderIcon);
dockVLayout->addWidget(overviewBtn);
dockWidget->setLayout(dockVLayout);
sidebarDock->setWidget(dockWidget);
So can someone tell me which part I missed to set the QQToolButton right and left border completely to side? Or are there some better ways to achieve this? Thanks.
Now I solved this problem.
Just need to add one line to the code snippet to set the layout's margin to 0, using: dockVLayout->setMargin(0)

How can I style a QTabWidget without affecting the tabs/widgets inside?

I have a QTabWidget with two tabs inside. I want to set the background of both tabs to be transparent to see the color of the main window underneath, but when I set
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("background-color: transparent;");
it makes all the background of the widgets I have inside the tabs transparent, not the tab itself.
Album showing before stylesheet change:
after change,
and what I'm trying to achieve.
I can provide more code if needed. Thanks in advance!
Stylesheets are applied to touched widgets and all its children.
Currently your style definitions
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("background-color: transparent;");
says something like "Set background color for all widgets to transparent".
... but you can restrict stylesheet applicability.
E.g. you can say, that style definitions should be applied only to instances of certain classes like QLabel:
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("QLabel{ background-color: transparent; }");
There are even more possibilietes to define applicability in more details:
The Qt Style Sheet Syntax, Selector Types
Relevant for your case is the possibility to restrict the applicability of style definitions to objects with certain name:
QTabWidget *tabWidget = new QTabWidget(this);
tabWidget-setStyleSheet("QWidget#custom_tab, QWidget#templates_tab{"
" background-color: transparent; "
"}");
You must make sure, that object names match (here: 'custom_tab' and 'templates_tab'.

QLabel & QComboBox setFont does NOT work

I'm using Qt Framework to build an App supporting multiple languages.
The default font is loaded from StyleSheet.
I override paintEvent() method, and setFont() method works OK for all widgets except for QLabel and QComboBox.
For QComboBox, the selected item has the correct font, the but the dropdown list items are using the default font. The Qt manual says setFont will set the font for both the comboBox button and the comboBox popup list to font.
Anyone happens to see this problem and have an idea to fix that? Thanks.
Answer is so long, because I wrote different approaches, choose the best for you.
Try to do next:
Create QListView, customize it (with stylesheet for example)
Set model with your data and set view to QComboBox with special methods:
setModel() and setView()
http://qt-project.org/doc/qt-4.8/qcombobox.html#setView
setStyleSheet("font-family: Arial;font-style: normal;font-size: 12pt");
For label you can use stylesheet too, setFont or just set HTML code with suitable font:
QFont f( "Arial", 14, QFont::Bold);
label->setFont(f);
With ComboBox you can use this for example:
QStringList stringList;
stringList << "#hello" << "#quit" << "#bye";
QStringListModel *mdl = new QStringListModel(stringList);
QFont comboFont("Arial",16,-1,true);
QListView *vw = new QListView;
vw->setFont(comboFont);
ui->comboBox->setModel(mdl);
ui->comboBox->setView(vw);
But it will install font to your data in popup menu, not in the header, so you can use also next:
QFont comboFont("Arial",16,-1,true);
for(int i = 0; i< ui->comboBox->count(); i++)
{
ui->comboBox->setItemData(i,QVariant(comboFont),Qt::FontRole);
}
ui->comboBox->setFont(comboFont);
Wityh this code snippet you'll get popup menu and header with this font and you don't need create models and views.
My dear, it is enough to do hereunder:
ui->CboxOpisBaza->lineEdit()->setFont(QFont("MS Shell Dlg 2", 12));

Internal QWidgets of QTabBar's tab?

As a follow up of " Hide label text for Qt tabs without setting text to empty string " :
Can I directly access the widgets within the tabs of the QTabBar. I do not mean the corresponding widget which is shown when I select a tab, but the tab's widgets (so in the screenshot below the log label and log icon).
I have tried QTabBar::findChildren, but with no success. Any idea?
QTabBar header sections are not actually widgets. They are drawn by QStylePainter inside QTabBar::paintEvent. Thus you can't get access to them.
As a workaround you can add a tab with an empty text and set a custom widget to it:
QTabBar *bar = new QTabBar;
bar->addTab("");
QLabel *label = new QLabel("my label");
bar->setTabButton(0, QTabBar::LeftSide, label);

Removing borders from QWidgets in a QGraphicsGridLayout

I currently have a QGraphicsScene that I am using with a QGraphicsGridLayout. I am trying to align QWidgets (QLabels and a custom graph QWidget) on this grid layout, and then export it to a QPrinter for pdf export.
The problem I'm having is that I have these grey divider lines between the QLabels that I can't seem to get rid of. I have tried settings spacing in the layout to 0, margins to 0, all the different properties of the QLabel palette, etc. all to no avail. Here's the relevant code:
main class:
QLabel lbl("some text");
lbl.setAutoFillBackground(true);
QPalette pal = lbl.palette();
pal.setColor(QPalette::Window, Qt::white);
lbl.setPalette(pal);
lbl.setFrameStyle(QFrame::NoFrame);
reportlayout->addWidget(&lbl);
reportlayout->generatePDF(reportfilename);
reportlayout class:
gridlayout->setContentsMargins(0,0,0,0);
gridlayout->setSpacing(0);
QGraphicsWidget* page = new QGraphicsWidget();
page->setLayout(gridlayout);
scene->addItem(page);
printer->setOutputFileName(filename);
painter->begin(printer);
scene->render(painter);
painter->end();
I have a feeling that it is the layout doing this, as the lines are between cells in the grid - but the layout doesn't have any color properties and I couldn't find anything to do with divider lines.
Thanks a bunch!
Have you tried stylesheets?
For example,
setStylesheet("QLabel { border:0px solid black; }");
You must investigate all possible selectors till find which one introduces the border.