How can I apply a custom theme? - c++

I am using QtCreator to create a Qt Application in C++.
I know CSS and making themes for elements in my applications isn't too hard, but is there a way to make a file and apply it?
I've looked through the Qt Docs but I can't seem to find anything about such a thing.
Currently, I am styling each individual button and stuff but can I just put it all in a file and apply it to everything at once?

easy: you just create a file, e.g. style.myStyle
there you place the styles for all the widgets including events, attributes etc
then you load the file when the app starts and apply that to the app
here is an example how:
#include <QApplication>
#include <QFile>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QFile file("./style.myStyle");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
a.setStyleSheet(styleSheet);
MainWindow w;
w.show();
return a.exec();
}
now in the style.myStyle file you can don what ever you want e.g.
QPushButton
{
background-color: white;
border-style: outset;
border-width: 2px;
border-radius: 10px;
border-color: beige;
font: bold 14px;
min-width: 10em;
min-height: 20em;
padding: 6px;
}
QPushButton:pressed
{
background-color: rgb(224, 0, 0);
border-style: inset;
}
QFrame, QLabel, QToolTip
{
border: 2px solid green;
border-radius: 2px;
padding: 1px;
}
that code produce a windows like this:
note:
don't forget to validate that the file exists etc etc

Related

How to change font size and other style of jqgrid 4.15

Trying to update look and feel of jqgrid.
I am trying to increase the font size of the data in the grid as well as of the column header.
Here is my jsfiddle
https://jsfiddle.net/4ga1ekh3/69/
Using the code at
How to change the font size in jqGrid?
.ui-jqgrid {font-size:0.8em}
But this did not work.
I would also like to know how to increase the font of the various fields when of the edit form
Since no one answered here, I resolved this by using the below code:
.ui-jqgrid tr.jqgroup td {
font-weight: bold;
font-size: 12px;
}
.ui-jqgrid .ui-th-column > div.ui-jqgrid-sortable {
font-size: 15px;
}
span.ui-jqgrid-cell-wrapper {
font-size: 16px;
}
td.jqgrid-rownum {
font-size: 15px;
}

How to set :hover on QMenu?

I'm working with QT and I have a menu. I have the follwing css for the menu:
QMenu {
/* background-color: #0F7070;*/
background-color:rgb(44,63,80);
border-top: none;
border-left:none;
border-right:none;
border-bottom:4px solid rgb(44,205,112);;
color:#fff;;
}
QMenu::item {
spacing: 3px; /* spacing between menu bar items */
padding: 10px 85px 10px 20px;
background: transparent;
}
/*Does not work*/
QMenu::item:hover {
background-color: rgb(52,73,94);
border-top: none;
border-left:none;
border-bottom:none;
border-left:3px solid rgb(44,205,112);;
}
How can I set the :hover to the item? Thank you.
I had same issue few years ago with a QT project.
Even if it may look like conterintuitive i solved changing:
item:hover
to
item:selected
I was advised so on QT forum, in that contest :selected acted as :hover, i didn't get deeper in the question, maybe won't work, but it worth a try.
I intend to set the background color of the QWidget
QWidget {
background-color: white;
}
But later, when the menu QAction is selected, the font is white, and the background is also white.The menu button font and background can be reset in the following ways
QMenu::item:hover {}
to
QMenu::item:selected {}
This way works, thanks

Unable to set stylesheet properties using qss file

I am trying to set some styles to all the QLineEdits in my application. Following is the code:
QLineEdit {
border: none;
padding-bottom: 2px;
border-bottom: 1px solid black;
color: #000000;
background-color:rgba(0,0,0,0);
}
QLineEdit:focus{
border: 0px solid white;
border-bottom: 2px solid #2196F3;
color: #000000;
}
When I input this style using the GUI i.e by setting the stylesheet option in form editor for each individual lineEdit, it works.
However when I try to add the same code using a qss file in resources, it doesn't work. I use the following code for applying stylesheet:
#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <conio.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// QFile styleFile( ":/Stylesheets/QLineEdit.qss" );
// styleFile.open( QFile::ReadOnly );
// std::printf("hi0");
// // Apply the loaded stylesheet
// QString style( styleFile.readAll() );
// a.setStyleSheet( style );
QFile file(":/Stylesheets/QLineEdit.qss");
file.open(QFile::ReadOnly);
QString styleSheet = QLatin1String(file.readAll());
a.setStyleSheet(styleSheet);
MainWindow w;
w.show();
return a.exec();
}
What could be the problem here?
Edit: Adding code for the QPushButton:
QPushButton, QPushButton:focus {
background-color:#2196F3;
border: none;
color: white;
padding: 3px 20px;
}
QPushButton:hover, QPushButton:hover:focus {
background-color: #1976D2;
border-color: #ffffff;
}
QPushButton:pressed,
QPushButton:pressed:focus {
background-color: #388E3C;
border: none;
color: white;
}
QPushButton:disabled {
color: #cccccc;
background-color: #cccccc;
border: none;
}
Let's summarize the outcome of the discussion.
Replace the file.open(QFile::ReadOnly); with file.open(QFile::ReadOnly | QFile::Text); QFile::Text is important, because:
The QIODevice::Text flag passed to open() tells Qt to convert
Windows-style line terminators ("\r\n") into C++-style terminators
("\n"). By default, QFile assumes binary, i.e. it doesn't perform any
conversion on the bytes stored in the file.
Furthermore, when setting the stylesheet globally, there are some specifics which should be taken into account:
A stylesheet affects the widget and everything below it in the widget's hierarchy. If set for a widget explicitly (from the code or using the form editor) the parents of the widget are not affected, as if it were set for the whole application. E.g. if you set the following: QWidget { background-color: red; } for a particular widget, this widget and all of its children will have a red background. If you set the same stylesheet from the qss file for the whole application, all the widgets will have a red background. So a great deal of care should be taken about the inheritance between the widgets. Using the right selector types is then crucial.

Ionic 2 - Slides how to change pagination progress color?

I have slide where I set the paginationStyle="progress" how can I change the color of the progressbar?
<ion-slides #exercisesSlider pager paginationType="progress">
Could somebody provide me a way to change the color of the progressbar?
Ionic uses Swiper API slides. So you can select using class names swiper-pagination-progress and swiper-pagination-progressbar like this:
.swiper-pagination-progress .swiper-pagination-progressbar {
background:red;
}
Using only the CSS borders worked for me :
.swiper-pagination-progressbar-fill {
border: 2px solid rgba(175, 240, 122, 0.719);
border-radius: 5px;
}
.swiper-pagination-progressbar {
background-color: rgba(255, 255, 255, 0.3);
border-radius: 5px;
}
Simple and maybe obvious follow up from the answer from #Surya Teja . If you separate the classes you can control which color for each part of the progress bar.
.swiper.pagination-progress {
background-color: red
}
.swiper-pagination-progressbar {
background-color: white
}

Adding constraint to css file for a QWidget

I use a css file in my Qt Projets, with Visual Studio 2010.
main.cpp :
QApplication app(argc, argv);
// Mise en place du style CSS
QFile File("Resources/style.css");
File.open(QFile::ReadOnly);
QString styleSheet = File.readAll();
File.close();
app.setStyleSheet(styleSheet);
A part of my css file :
QWidget#contenuDescription {
background-color: rgb(0, 150, 255);
border: 2px solid rgb(0, 0, 255);
border-top-left-radius: 10px;
border-top-right-radius: 10px;
border-bottom-right-radius: 10px;
border-bottom-left-radius: 10px;
}
QLabel#nom1 {
background-color: rgb(0, 150, 255);
font-size: 36px;
}
QLabel#nom2 {
background-color: rgb(0, 150, 255);
font-size: 24px;
}
QLabel#nom3 {
background-color: rgb(0, 150, 255);
font-size: 20px;
}
I want to change the color to my QLabels : nom1, nom2 and nom3 when bool m_changeColor == true.
I know we can use ::hover if we want to change the style sheet when the mouse is on the QLabel. Something like this does it exist for my problem ?
Thank you in advance for your answer.
You need to use properties:
Q_PROPERTY(bool changeColor ...)
Os set a property dynamically:
nom1Label->setProperty("changeColor", true);
Then in CSS:
QLabel#nom1[changeColor="true"] {
...
}
Also note:
Warning: If the value of the Qt property changes after the style sheet
has been set, it might be necessary to force a style sheet
recomputation. One way to achieve this is to unset the style sheet and
set it again.