I'm trying to build an app using Qt Designer for Python, but I'm having a problem where the Calendar appears to be in black color making all the days invisible!
The Calendar is new, I'm simply dragging then dropping it on the Main Window, there's nothing in the styleSheet for the calendar, but I did change the background color for the Main Window by using this styleSheet:
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(45, 67, 241, 0.8), stop:1 rgba(172, 247, 255, 0.8));
border-radius:10px;
Removing the styleSheet for the Main Window solves the problem, but I need it as the background colors are required.
Here's the Python code if needed:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setStyleSheet("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:0, y2:1, stop:0 rgba(45, 67, 241, 0.8), stop:1 rgba(172, 247, 255, 0.8));border-radius:10px;")
MainWindow.showMaximized()
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.wFHWidget = QtWidgets.QWidget(self.centralwidget)
self.wFHWidget.setGeometry(QtCore.QRect(30, 570, 471, 571))
self.wFHWidget.setObjectName("wFHWidget")
self.calendarWidget = QtWidgets.QCalendarWidget(self.wFHWidget)
self.calendarWidget.setGeometry(QtCore.QRect(70, 40, 264, 183))
self.calendarWidget.setObjectName("calendarWidget")
The Widget which the calendar is part of (wFHWidget) also has some styleSheet, but when I removed it, the calendar remained in black so I assumed it's not the cause for the issue. Also, removing the styleSheet from the Main Window while keeping the styleSheet in the wFHWidget returns the calendar to its default view (not black) which is another confirmation that the issue is only with the Main Window styleSheet.
Here's what the Main Window, Widget & Calendar look like at the end:
Here's a short video of the process in case it's needed for further showing the issue:
https://photos.app.goo.gl/VZetLUv2WKV8ck46A
sorry for the video's low quality, using work's PC and I don't have a screen recorder installed.
Related
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);
I have a QFrame, titled 'bannerframe' embedded in a dialog, with other widgets.
I want to dynamically change the background of the frame. If I use a stylesheet defined in the top level dialog (MessageDlg), it appears correctly, like this: -
The stylesheet is defined as
#bannerFrame
{
background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,
stop:0 rgba(195, 40, 9, 255),
stop:0.7 rgba(225, 121, 113, 255),
stop:1 rgba(237, 154, 152, 255));
}
However, if I remove this and add the style directly to the bannerframe widget, the gradient doesn't seem to work properly: -
The same effect is seen, regardless of setting the stylesheet in the Designer, or in code:
QString style = QString("background-color: qlineargradient(spread:pad, x1:0, y1:0, x2:1, y2:0,\
stop:0 rgba(%1, %2, %3, 255),\
stop:1 rgba(%4, %5, %6, 255));").arg(red1).arg(green1).arg(blue1).arg(red2).arg(green2).arg(blue2);
ui->bannerFrame->setStyleSheet(style);
So, what's going on here?
Why does the gradient not work correctly around the frame's widgets when the stylesheet is set directly on the Frame, rather than on the top-level dialog?
That's probably because the widgets in the frame will inherit their parents stylesheet. So what you see is the QLabel widget with the icon having a gradient background. Add a selector to limit the widgets your styles should apply to.
QString style = QString("QFrame {...}");
or
QString style = QString("QFrame#bannerFrame {...}");
I've had to deal with that too until I discovered that Qt style is decided by the LAST line that sets a style. That means that if you have
something->setStyleSheet(Style1);
something->setStyleSheet(Style2);
something->setStyleSheet(Style3);
Only Style 3 will remain (If they are equal but changing values. If there are diferences, i.e style1 puts text in Bold and style2 puts text in size 14, they will both work BUT if style3 says that text size is 17, then it will be 17.
To avoid that and be able to change it for every type of widget, you need to define for who is that style:
generallayout->setStyleSheet("NAME{css_code}");
like:
myLayout->setStyleSheet("QLineEdit{background-color:#ff0066"};
with that only LineEdits will have that background. In your case, you just need to use QFrame or QFrame#bannerFrame depending on what you want.
I need to style a Popover from ControlsFX, but am failing to do so.
I have my own xxx.css stylesheet that I add to a scene, and I've (obviously) successfully styling many JavaFX Controls...
I have set this in the stylesheet (copied and modified from popover.css in ControlsFX):
.popover > .border {
-fx-stroke: linear-gradient(to bottom, rgba(0,0,0, .3), rgba(0, 0, 0, .7));
-fx-stroke-width: 0.5;
-fx-fill: rgba(30 , 30, 30, .95);
-fx-effect: dropshadow(gaussian, rgba(0,0,0,.2), 10.0, 0.5, 2.0, 2.0);
}
But the Popover never gets the border style. How do I get the Popover to pick the style up?
Since the PopOver is displayed in a different window, you can't set your style on the primary scene, but on the PopOvercontrol.
If you look at how the style is applied to the control in its skin class PopOverSkin:
stackPane = new StackPane();
stackPane.getStylesheets().add(
PopOver.class.getResource("popover.css").toExternalForm());
stackPane.getStyleClass().add("popover");
where this stackPane can be accessed with:
#Override
public Node getNode() {
return stackPane;
}
you just need to add your style sheets to that stack pane, right after you have access to the skin, that is, when the popOver is shown:
popOver.show(...);
((Parent)popOver.getSkin().getNode()).getStylesheets()
.add(getClass().getResource("MyPopOver.css").toExternalForm());
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();
}
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.