How to change Qt ProgressBar color? - qt

I would like to change color of my progress bar from default green to red. I have this code, but the view is "flat", I would like to achieve something like "3d effect" as on picture below:
Code for red PB:
QPalette pal = ui->pbExtractionWidget->palette();
pal.setColor(QPalette::Normal, QColor(Qt::red));
QString danger = "QProgressBar::chunk {background: QLinearGradient( x1: 0, y1: 0, x2: 1, y2: 0,stop: 0 #FF0350,stop: 0.4999 #FF0020,stop: 0.5 #FF0019,stop: 1 #FF0000 );border-bottom-right-radius: 5px;border-bottom-left-radius: 5px;border: .px solid black;}";
ui->pbExtractionWidget->setStyleSheet(danger);
This is how it looks:

This link provides a way to style the progressbar.
This link provides a way to change gradient color for widgets.
Essentially, you just need to change the stylesheet correctly. Each Chunk is a piece of the progressbar.
Or use QPalette which uses Base to color the background of a widget. Set its gradient correctly and then do the following.
palette.setBrush( QPalette::Base, gradientVariable);
ui->pbExtractionWidget->setPalette(palette);

// In main.cpp
qDebug() << QStyleFactory::keys();
// If keys contains e.g. 'Fusion' it would be possible to change color of QProgressBar.
// On windows default style is 'Windows' and color can only be change with style sheets.
auto style = QStyleFactory::create("Fusion");
if (style) {
app.setStyle(style);
}
class MyProgressBar final : public QProgressBar {
void paintEvent(QPaintEvent*) final {
QStyleOptionProgressBar option{};
initStyleOption(&option);
option.textAlignment = Qt::AlignHCenter;
option.palette.setColor(QPalette::Highlight, QColor("lightskyblue"));
option.palette.setColor(QPalette::HighlightedText, option.palette.color(QPalette::Text));
QPainter painter{this};
style()->drawControl(QStyle::CE_ProgressBar, &option, &painter, this);
}
};

You have to set the stylesheet correctly, use below stylesheet code
QString danger = "QProgressBar::chunk: horizontal {border-radius: 3px; background: QLinearGradient(X1:0, y1:0.966136, x2:0, y2:0, stop:0.609721 rgba(242, 53, 53, 255), stop:0.691923 rgba(240, 151, 141, 252));border: .px solid black;}";
ui->progressBar->setStyleSheet(danger);

Related

QTreeWidget Stylesheet Color for Childs

System: Linux Mint, QT Creator from Repo -> QT Version 5.2, C++)
I've created a Customwidget wich Im using inside a QTreeView
OwnItem *OI = new OwnItem;
QTreeWidgetItem *itemN = new QTreeWidgetItem();
ui->ProjektListe->addTopLevelItem(itemN);
ui->ProjektListe->setItemWidget(itemN, 0, OI);
What I want is to set up a Stylesheet for the QTreeWidget including a Backgroundcolor and a Textcolor in Normal Mode and Selected Mode.
So far:
QTreeWidget::item{
background-color: rgb(255, 255, 255);
color: rgb(255, 255, 0);
}
QTreeWidget::item:selected{
background-color: #157efb;
color: rgb(255, 0, 0);
}
The Problem is that The Backgroundcolor works, the Color (TextColor) not (in both Cases). Im aware that when stylesheets for childs are set separately this won't work but the widget itself and all of its children (Some Labels and Buttons) are "Sylesheet" free.
The only Case "color: .... " for TextColor works is this case
QWidget{
color: rgb(85, 0, 0);
}
But this wont work with the "selected" status
My anser is in C++ not for the CSS but you can create a QPalette then set the values that you want to with the function void QPalette::setColor ( ColorGroup group, ColorRole role, const QColor & color ) so for you it should me something like:
QTreeWidget tree(a);
QPalette palette;
palette.setColor(QPalette::Window, QColor(255, 255, 255));
palette.setColor(QPalette::WindowText, QColor(255, 255, 0));
palette.setColor(QPalette::Highlight, QColor(255, 0, 0))
palette.setColor(QPalette::HighlightedText, QColor(0, 0, 255));
QList<QTreeWidgetItem> treeItems = tree.findChildren<QTreeWidgetItem*>();
foreach (QTreeWidgetItem *w : treeItems) {
w.setPalette(palette);
}
the findChildren will return a list with all the children to the widget then you can set the palette. To find the list of the colors groups you can go here: http://qt-project.org/doc/qt-4.8/qpalette.html#setColor then click on the ColorGroup type in the parameter then you will be are here: http://qt-project.org/doc/qt-4.8/qpalette.html#ColorGroup-enum
Good luck !

Qt5 - setting background color to QPushButton and QCheckBox

I'm trying to change the background color of a QAbstractButton (either a QPushButton or QCheckBox) in Qt5 and having zero luck.
This does nothing:
pButton->setAutoFillBackground(true);
QPalette palette = pButton->palette();
palette.setColor(QPalette::Window, QColor(Qt::blue));
pButton->setPalette(palette);
pButton->show();
and if I try changing the style sheet:
pButton->setStyleSheet("background-color: rgb(255,255,0);");
then Qt throws up its hands and draws an afwul-looking blocky button.
There is a page titled "How to change the background color of QWidget" but it just talks about those two methods.
There is also a page "Qt Style Sheets Examples" that implies that if you want to change the background color, you have to take over all aspects of drawing the button, which just seems like overkill.
I need this to run on Mac, Windows, and Ubuntu Linux, and it's really not a happy thing if I have to manually draw everything about the button 3 times (once for each platform).
Am I missing something obvious?
p.s. By "background color" I mean the area surrounding the button, not the color under the text on the face of the button.
I had the same issue, but finally got this to work. I'm using Qt 5 with the Fusion color theme:
QPalette pal = button->palette();
pal.setColor(QPalette::Button, QColor(Qt::blue));
button->setAutoFillBackground(true);
button->setPalette(pal);
button->update();
Try these commands in the exact order as above, and if that still doesn't work, set your theme to Fusion and try again.
Good luck!
Add propoerty border:none; to the stylesheet
For some reason this removes the default background color also.
Example: background-color: rgba(46, 204, 113, 0.4); border: none;
Try this:
QColor col = QColor(Qt::blue);
if(col.isValid()) {
QString qss = QString("background-color: %1").arg(col.name());
button->setStyleSheet(qss);
}
as mentioned at the QT Forum by #goetz.
I used some different definition of Qcolor col as QColor col = QColor::fromRgb(144,238,144); and this works for me.
Changing the Dialog styleSheet Property works for me, set this property to:
QPushButton:pressed {
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(60, 186, 162, 255), stop:1 rgba(98, 211, 162, 255))
}
QPushButton {
background-color: #3cbaa2; border: 1px solid black;
border-radius: 5px;
}
QPushButton:disabled {
background-color: rgb(170, 170, 127)
}
I found a stupid way, tried every attribute in palette, and it works for me when changing 'QPalette::Base'.
Maybe you can have a try.
pButton->setAutoFillBackground(true);
QPalette palette = pButton->palette();
//palette.setColor(QPalette::Window, QColor(Qt.blue));
//palette.setColor(QPalette::Foreground, QColor(Qt.blue));
palette.setColor(QPalette::Base, QColor(Qt.blue));
//palette.setColor(QPalette::AlternateBase, QColor(Qt.blue));
//palette.setColor(QPalette::ToolTipBase, QColor(Qt.blue));
//palette.setColor(QPalette::ToolTipText, QColor(Qt.blue));
//palette.setColor(QPalette::Text, QColor(Qt.blue));
//palette.setColor(QPalette::Button, QColor(Qt.blue));
//palette.setColor(QPalette::ButtonText, QColor(Qt.blue));
//palette.setColor(QPalette::BrightText, QColor(Qt.blue));
pButton->setPalette(palette);
pButton->show();
reference link : How to get a stylesheet property?
I've struggled with same problem. You need to choose QPalette::Button instead of QPalette::Window. Qt reference says this: QPaletteButton - The general button background color. This background can be different from Window as some styles require a different background color for buttons.
So I solved it this way:
QPalette pal=this->palette(); \\"this" is my derived button class
pal.setColor(QPalette::Window, style.background);
QColor col=style.background; \\ my style wrapper, returns QColor
this->setAutoFillBackground(true);
this->setPalette(pal);
I want to toggle the color of a button On/Off.
This works for me ...
QPalette pal = ui->pushButton->palette();
if (bIn)
pal.setColor(QPalette::Button, QColor(Qt::green));
else
pal.setColor(QPalette::Button, QColor(Qt::red));
ui->pushButton->setPalette(pal);
ui->pushButton->setAutoFillBackground(true);
ui->pushButton->repaint();
I flip the value of bIn on a Clicked Signal/Slot.
void BtnFrame::on_pushButton_clicked()
{
if (bIn)
bIn=false;
else
bIn=true;
setColor();
}

Is it possible to "tint" a button in JavaFX 2

I have a JavaFX 2 application, where all my buttons use the default style (grey gradient). In some special areas of the application, the background color is red, yellow or green. In these areas, I also have buttons.
Instead of re-styling all the different states (normal, hover, pressed) of the button in all three colors, I'd like to just give the button the tint of the background. Is this possible, and how?
If not, is there a way to easily re-style the base button style, and have the hover and pressed states (pseudo-selectors) automatically derived from this style?
If that's not possible, I'm open for suggestions.. My most important goal is to avoid redundant/duplicate declarations (especially of gradients), in case someone wants to add a different color panel later, or just change the shade of one of the background colors.
CSS for the red panel/button:
#my-red-panel {
-fx-border-width: 1;
-fx-border-radius: 5;
-fx-background-radius: 5;
-fx-smooth: true;
-fx-border-color: rgb(209, 65, 42);
-fx-background-color: rgba(255, 78, 50, 0.89);
}
#my-red-panel .button {
-fx-background: rgba(0, 0, 0, 0); /* Now borders look good, but button is still grey*/
}
My best bet so far, is to use a semi-transparent gradient, like so:
#my-red-panel .button {
-fx-background-color: linear-gradient(rgba(255, 255, 255, 0.3), rgba(0, 0, 0, 0.2));
}
I still have to declare each state, but at least I can change the underlying colors without having to modify each state. The main problem is that this overrides the entire look of the button, so I was hoping for something better... :-/
Not tested, but try experimenting with:
#my-red-panel {
-fx-base: rgba(255, 78, 50, 0.89);
}
or perhaps:
#my-red-panel .button {
-fx-base: ... ;
}
depending on the exact effects you want.
The trick here is that the default css (caspian.css for JavaFX2.2 or modena.css for JavaFX8) use some pre-defined lookup colors. You can dig out the source for these to see how they are used. If you redefine these lookups for a node in the scene graph, the new definition is propagated to all child nodes.

Set background color only partially with stylesheets

How can I set the background color for a part of the background like in the following image:
Of course, without border frames, I want to set only the cyan color.
I need to set the length of the left part (cyan) as the percentage of the widget length, e.g 30%.
With css I would hack qlineargradient a little bit. Note that edge of cyan may be a little blurry.
QFrame
{
background-color: qlineargradient(x1:0, x2: 1, stop: 0 cyan, stop: 0.29 cyan, stop: 0.2901 white, stop: 1 white);
}
If you want it hard-coded in the application, you can overload the paintEvent function in a widget. Something like this:
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen(Qt::NoPen);
painter.setPen(pen);
painter.fillRect(0, 0, width(), height(), Qt::white);
painter.fillRect(0, 0, 0.3*width(), height(), Qt::cyan);
...
}

Can QMessageBox::about adjust size to title length?

I wanted to create a simple About dialog, but noticed that the QMessageBox::about does not adjust its size to the length of the title (which is usually longer due to the larger font ...at least in my desktop environment), only to the content. Is there a way to make sure that the dialog is made big enough to show all of the title as well? I could of course add white space to the aboutText, but I am hoping for a less hackish solution.
Example:
QString titleText("Some title which is slightly longer");
QString aboutText("Short about text");
QMessageBox::about(this,titleText,aboutText);
Currently the above code only gives me "Some ..." as the title string. I have built the program in Eclipse on Ubuntu with Qt 4.7.
Use "setStyleSheet()" function of "QMessageBox". Here is an example.
background-color: QLinearGradient(x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #787878, stop: 0.5 #404040, stop: 0.6 #303030, stop: 0.8 #252525, stop: 1 #151515);
border: 2px solid #05b8cc;
border-radius: 8px;
color: white;
min-width: 300px;
min-height: 80px;
It will also affect the children of the "QMessageBox" whose stylesheets can be reverted by iterating through them. To access the children use "findChildren(QWidget)".
I believe QMessageBox does adjust size to fit the window title, but for some reason it doesn't work right on my system also, not sure if it's a bug or a feature, this is done in the qmessagabox.cpp QMessageBoxPrivate::updateSize() method.
Another thing I've noticed is that you're using an instance of the QMessageBox class to call about() method, which is static and you can execute it by using just the class name: QMessageBox::about(..).
What you could do to adjust the window size is creating your own subclass of the QMessageBox and adjusting min width of the window in the showEvent method, see the example below for details:
class MyMessageBox : public QMessageBox
{
public:
explicit MyMessageBox(QWidget *parent = 0) : QMessageBox(parent) { }
MyMessageBox(const QString &title, const QString &text, Icon icon,
int button0, int button1, int button2,
QWidget *parent = 0,
Qt::WindowFlags f = Qt::Dialog | Qt::MSWindowsFixedSizeDialogHint) :
QMessageBox(title, text, icon, button0, button1, button2, parent, f) { }
static void about(QString title, QString text)
{
MyMessageBox aboutBox(title, text, QMessageBox::Information, 0, 0, 0, NULL);
aboutBox.setText(title);
aboutBox.setText(text);
QIcon icon = aboutBox.windowIcon();
QSize size = icon.actualSize(QSize(64, 64));
aboutBox.setIconPixmap(icon.pixmap(size));
aboutBox.exec();
}
void showEvent(QShowEvent *event)
{
QMessageBox::showEvent(event);
QWidget *textField = findChild<QWidget *>("qt_msgbox_label");
if (textField != NULL)
{
// getting what ever my system has set for the window title font
QFont font = QFont("Ubuntu Bold", 11);
// you might want to make it more generic by detecting the actuall font
// or using smth like this:
//QFont font = QApplication::font("QWorkspaceTitleBar");
QFontMetrics fm(font);
int width = qMax(fm.width(windowTitle()) + 50, textField->minimumWidth());
textField->setMinimumWidth(width);
}
}
};
here's how you can call it:
QString titleText("Some title which is slightly longer");
QString aboutText("Short about text");
MyMessageBox::about(titleText, aboutText);
hope this helps, regards

Resources