Is there way to make QPushButton background transparent, but adding icon? - qt

Is it possible to make the background of the button transparent, but at the same time to add an icon?
As I understood I should use:
button -> setStyleSheet("background-color: rgba(255, 255, 255, 0); ");
It works fine, but I want to add an icon too. I have a nice window's background and want to see it through my buttons, but buttons should have icon - black arrow.

It is possible using Qt 6 C++. I found a way how to do it:
QPixmap buttonImage("/*path*/");
QIcon buttonIcon(buttonImage);
// make the button transparent
button->setStyleSheet("background-color: rgba(255, 255, 255, 0); ");
// add icon
button->setIcon(buttonIcon);

Related

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

Background colour of child indicator in QTreeWidgetItem?

I'm using a QTreeWidget to show a list of categorized properties. I would like the top level items to be a different background colour; however, the arrow indicating it has children is always the default black on white (Windows 8.1, Qt 5.2.1). Here's how I'm adding the QTreeWidget Item:
QBrush fg(Qt::white);
QBrush bg(Qt::darkGray);
QTreeWidgetItem *header = new QTreeWidgetItem();
header->setText(0, "Sound File");
this->addTopLevelItem(header);
header->setFirstColumnSpanned(true);
header->setData(0, Qt::ForegroundRole, fg);
header->setData(0, Qt::BackgroundRole, bg);
header->setExpanded(true);
Here's a screenshot of how this gets rendered.
How can I give it a solid background across the whole row?
For the indicators you can use StyleSheets something like this:
QTreeWidget::branch::!has-children:selected {background-color: rgb(255, 255, 255);}
QTreeWidget::branch::!has-children:selected:alternate {background-color: rgb(0, 0, 0);}
and set this StyleSheet to the QTreeWidget..
If someone knows any other way to do this for the individual items feel free to share... I have similar situation where I was one of the item that have children to be different color..

Setting only background color of MainWindow Qt

So I am trying to change only the background color of my MainWindow. When I try to do this using this->setStyleSheet("background-color:black;"); for example it changes the background of everything: child widgets, QTextBoxEdit background, everything.
Is there a way to only change the background of just the main window?
As you know, every QMainWindow has a central widget and by default is named centralwidget.
So the best way of solving this issue is changing the background for that widget.
It's pretty simple when we use a style sheet. In this case would be the following one:
#centralwidget {
background-color: rgb(0, 0, 0);
}
you can use Qt class name before QSS, like
QMainWindow { background-color: rgb(0, 0, 0);}
in your example QMainWindow > QWidget { background-color: rgb(0, 0, 0);} maybe better.
please see http://doc.qt.io/qt-4.8/stylesheet-syntax.html for more information

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.

Make a pixmap transparent for a QLabel

I have a MainWindow with a QLabel and a pixmap. I want to make it transparent (or less opaque)
I am using the following code below.
ui->label->setAttribute(Qt::WA_TranslucentBackground);
ui->label->repaint();
However it does not seem to work. The image looks the same without any changes. I also tried to use to the following statement:
ui->label->setStyleSheet("background-color: rgba(255, 255, 255, 10);");
Unfortunately, this does not seem to work either.
Anyone knows how can I make an image transparent or make it less opaque?
Thank you for your time.
If your image isn't transparent as it is and you want it to be, you can do something like this:
QLabel *l = new QLabel(this);
QImage image(":/img/myimage.png");
QPainter p;
p.begin(&image);
p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
p.fillRect(image.rect(), QColor(0, 0, 0, 50));
p.end();
l->setPixmap(QPixmap::fromImage(image));
You can apply QGraphicsOpacityEffect to a label to adjust it's opacity.
You can use method fill, example:
pixmap = QPixmap(width_size_in_pixels, height_size_in_pixels)
pixmap.fill(Qt.transparent)

Resources