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);
Related
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);
Trying to style qt widget, I need different colors for hover, while the mouse is pressed, disabled and normal state. I m not able to change color for the pressed state. Please, help?
QWidget
{
background-color: rgb(170, 170, 255);
}
QWidget:hover
{
background-color: rgb(0, 170, 0);
}
QWidget:pressed
{
background-color: rgb(255, 0, 0);
}
QWidget does not have ":hover" and ":pressed" pseudo-states supported. However, you can simulate these pseudo-states through dynamic properties. Still, in order to use such a mechanism, you would have to do the following things first (you can choose whichever you like):
Install event filter on your QWidget instance. This event filter would have to react to the following events: QEvent::Enter and QEvent::Leave for hover state. And then, QEvent::MouseButtonPress would need to be used for the mouse press state. Regarding event types, you can read more here. Also, there might be circumstances where you would need to react to the current mouse position. However, by not knowing your exact use case it is hard to tell if you need that or not.
Implement custom class that would be a child of QWidget. By doing so, you would need to override the following methods:
virtual void enterEvent(QEvent *event)
virtual void leaveEvent(QEvent *event)
virtual void mousePressEvent(QMouseEvent *event)
Now, in each implementation you would have to set your own dynamic property. The following example illustrates just how to set a dynamic property and ensure it gets styled on run-time:
ui->someWidget->setProperty("property_applied", true);
// This is the limitationn. You can read more about it here: https://wiki.qt.io/Dynamic_Properties_and_Stylesheets
ui->someWidget->style()->unpolish(ui->someWidget);
ui->someWidget->style()->polish(ui->someWidget);
When styling by dynamic properties, you can set the following stylesheet:
#someWidget[property_applied=true] {
background-color: #ff0000;
}
#someWidget[property_applied=false] {
background-color: #00ff00;
}
In other words, it would be not too hard to implement the following styling mechanism through usage of event filters or custom widget implementation and dynamic properties:
QWidget {
background-color: rgb(170, 170, 255);
}
QWidget[hover=true] {
background-color: rgb(0, 170, 0);
}
QWidget[pressed=true] {
background-color: rgb(255, 0, 0);
}
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 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)
I'm changing grid's row color to red according to my needs and when it gets colored the text is invisible, under backroung color...
How to change the text color or make it transparent ???
void __fastcall TfRegular::tvFillOutCustomDrawCell(TcxCustomGridTableView *Sender,
TcxCanvas *ACanvas, TcxGridTableDataCellViewInfo *AViewInfo,
bool &ADone)
{
TRect ARec;
ARec=AViewInfo->Bounds;
//AViewInfo->GridRecord->DisplayTexts[7]="+" + AViewInfo->GridRecord->DisplayTexts[7];
// ShowMessage(AViewInfo->GridRecord->Values[13]);
if (AViewInfo->GridRecord->Values[13]>1) {
ACanvas->Canvas->Brush->Color = clRed;
ACanvas->Canvas->Font->Color = clBlack;
ACanvas->Canvas->Pen->Color = clRed;
ACanvas->Canvas->FillRect(ARec);
SetBkMode(ACanvas->Canvas->Handle,TransparentColor);
//InflateRect(AViewInfo->Bounds,2,2);
//ACanvas->DrawTexT("",AViewInfo->Bounds,0);
ADone=true;
}
}
It was really easy than I thought.
I solved this problem using OnGetContentStyle event.