QTreeWidget Stylesheet Color for Childs - css

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 !

Related

QComboBox retains mouse hover highlight if you close popup

I am having some issues styling my QComboBoxes. I want to have them highlight when mouse over, but it seems they retain the highlight if I click on them and then close the pop-up (either by clicking outside or by selecting something, does not matter). I am using Qt 5.13.1 and I see this issue on macOS and Linux. Haven't tested on others but I guess it would be the same.
My style is this simple, maybe I am missing something:
QComboBox:hover
{
background-color: rgba(0, 0, 0, 0.2);
}
Is this what you're looking for? Or do you mean the hover color of the QComboBox when you did not yet press to view all the other options?
QComboBox QAbstractItemView
{
selection-color: white;
selection-background-color: black;
}
EDIT (after reading the comments):
Right click on the combobox in the designer space and chose go to slot.
Then press CurrentIndexChanged(int) and change the code of the slot to this:
void MainWindow::on_comboBox_currentIndexChanged(int index)
{
ui->comboBox->setStyleSheet("QComboBox:!editable, QComboBox::drop-down:editable {"
"background: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,"
"stop: 0 #E1E1E1, stop: 0.4 #DDDDDD,"
"stop: 0.5 #D8D8D8, stop: 1.0 #D3D3D3);}");
}
Ok, I managed to work around this issue, will write it here in case anybody else has this problem. I quit trying to use qss and did it in the code. Subclassed the QComboBox and added the hover events:
bool TrackableComboBox::event(QEvent *event)
{
switch (event->type())
{
case QEvent::Enter:
this->setStyleSheet("background-color: rgba(0, 0, 0, 0.2);");
break;
case QEvent::Leave:
case QEvent::MouseButtonPress:
this->setStyleSheet("background-color: -1;");
break;
default:
break;
}
return QWidget::event(event);
}
The secret here is to add Leave and MouseButtonPress reset the color, otherwise you will have the same issue.

How to change Qt ProgressBar color?

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);

Mfc Dialog change color button

Thanks for your help In solving my problem
I try to add a button that can change the background of the dialog to a different color i using visual studio 2010 but i think its might be wrong way to do that
void PainterDlg::OnBnClickedButton7()
{
CBrush m_brush;
m_brush.CreateSolidBrush(RGB(255, 255, 255));
return m_brush;
}
Or it should look like this
void PainterDlg::OnBnClickedButton7()
{
CBrush m_brush;
m_brush.CreateSolidBrush(RGB(255, 255, 255));
return m_brush;
}
both ways are not work for me
thankS in advance
That is not so easy with CButton. (you have to draw all yourself in OnDrawItem, OnCtlColor)
A simpler way is to use CMFCButton.
Add a Member Variable for your Button (with MFC-ClassWizzard) and change it to CMFCButton.
Here an example to change the color button in green.
void CColorButtonSimpleDlg::OnBnClickedMyColorbtn()
{
// add a Member Variable for your Button
// Change it to CMFC Button
// CMFCButton m_myBtn; declared in Header-File *.h
m_myBtn.EnableWindowsTheming(FALSE); // (important!)
m_myBtn.SetFaceColor(RGB(0, 255, 0)); // Change to your desired Background Color
m_myBtn.SetTextColor(RGB(255, 255, 255)); // Change it to your desired Foreground Color
}
Nvm found it
int r,b,g;
r=rand()%255;
b=rand()%255;
g=rand()%255;
CBrush myb;
myb.CreateSolidBrush(RGB(r,b,g));
dc2.FillRect(&rect,&myb);

Styling JavaFX Popover

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());

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