How can I get the pressed background color of a QPushButton?
if (isDown())
_BGColor = <pressed background color>; // how to get this???
else
_BGColor = palette().color(QPalette::Background); // this is to get the idle backcolor
Thanks in advance!!!
It's very difficult (if not impossible) to find a way to get the backgound color of a button when it's pressed, because it depends on the style, and it's not guaranteed that the style respects the palette.
However I suggest two different approaches:
You can set you own background color using style sheets (simpler) or implement the painting of the button yourself, using styles or reimplementing paintEvent(). See Customizing QPushButton
To paint over the button with the inverse color, you can set a composition mode to the painter in order to get the inverse color.
For example:
painter.setPen(QColor(255, 255, 255));
painter.setCompositionMode(QPainter::RasterOp_SourceAndNotDestination);
(note that using this example, the inverse color of middle grey (128, 128, 128) is exactly the same color)
See QPainter::CompositionMode
Related
In my paintEvent method for a custom widget for a game I'm writing, After calling the various model objects' render() methods to render them to the widget, I am trying to render the "Hi-Score" text. Here's the general code for just the text drawing:
painter.fillRect(event->rect(), QColor(0, 0, 0));
painter.drawImage(QRectF(event->rect().x(), event->rect().y() + 30, 512, 512), getGameBoardImage());
//...rendering other model components
painter.setBrush(QBrush(QColor(255, 255, 255)));
//painter.setFont(getGameFont());
painter.setFont(QFont("Times New Roman", 16, QFont::Bold));
painter.drawText(0, 0, "HI-SCORE");
I 'was' trying to draw the text in a custom font loaded from resource (I found an answer on here for that) but it wouldn't even display, even with a white brush. I thought maybe it was because it was because I didn't 'set' a font, but setting it to Times New Roman doesn't display anything either. What might I be doing wrong? As you can see the background is a black background with the game board painted on top but leaving a small buffer at the top and bottom. Do I need to do something special to display the text? Please don't suggest using a QLabel because I am trying to keep it all in one widget if possible. If I must, I will split the Hi-Score and 1-Ups into 2 other label sets with specialty fonts.
you code look ok, but you are drawing at 0,0 which is the top left corner of the widget canvas AND the text is actually there but not visible...
draw instead at
painter.drawText(margin, y+margin, "HI-SCORE");
where y is the high of the font used to draw the text and margin is you know a little margin border to make it look better something like 5 units
update
you can get the value of the text you are painting doing somthing like
QFont font("times", 25);
QFontMetrics fm(font);
int pixelsW{fm.width("HI-SCORE")};
int pixelsH{fm.height()};
Is there an efficient way of removing the frame and setting the background colour to transparent in Qt? Another thing I would like to make is to have the window being "moveable" too i.g. whenever I press and and hold the left mouse button I can move the window wherever I like.
Graphically to express the result I would like to achieve.
----------
Solution in order to make it "moveable": https://forum.qt.io/topic/34354/solved-frameless-window-dragging-issue/2
First you have to set the window flag (i do this in an overloaded QDialog::exec):
setWindowFlags(Qt::FramelessWindowHint | Qt::Dialog);
(just add Qt::FramelessWindowHint to your window flags)
Then you add transparent background:
setAttribute(Qt::WA_TranslucentBackground);
...and make sure autoFillBackground is not set (uncheck in Designer if checked)
If you need to add a shadow, simply add a DropShadowEffect to the widget which creates the backgroud rounded rectangle:
auto dropShadow = new QGraphicsDropShadowEffect;
dropShadow->setOffset(0);
dropShadow->setBlurRadius(40);
dropShadow->setColor(QColor(0, 0, 0, 180));
ui.backgroundWidget->setGraphicsEffect(dropShadow);
The shadow is painted on the widget itself, so you need additional space around your background widget. I.e. if your BlurRadius is set to 40 you should set a 40 pixel margin:
layout()->setMargin(40);
I have 2 widgets:
One of them is background(semi-transparent), second contains other widgets(without background)
Can I make a blur behind window like this?
I have 2 widgets:
One of them is background(semi-transparent), second contains other
widgets(without background)
QLCDNumber is a widget of such kind.
That is doable and one of the good examples is: C++,Qt QMainWindow Transparent Background Example. But if you need to make it semi-transparent or lower the transparency you can do something like:
// mind the last component of rgba
mainwindow->setStyleSheet("QMainWindow{background-color: rgba(255, 255, 255, 127)};");
I'm creating an application for capturing the screen or a part of it, and for that I need to select the part of the screen I want to capture.
The idea is to create a fullscreen semi-transparent window and draw thing on it so the user can see what he is doing. I have a working implementation where the selected area is dislayed as a wxPanel, but I want to rewrite it to paint everything to the main wxPanel manually.
frame = new wxFrame(NULL, -1, "", wxPoint(0,0), wxSize(0,0), wxSTAY_ON_TOP|wxFRAME_NO_TASKBAR|wxFRAME_SHAPED);
panel = new wxPanel(frame, -1);
SetTopWindow( frame );
panel->Bind(wxEVT_KEY_DOWN, &wxMiniApp::OnKeyDown, this);
panel->Bind(wxEVT_KEY_UP, &wxMiniApp::OnKeyUp, this);
panel->Bind(wxEVT_LEFT_DOWN, &wxMiniApp::OnMouseStart, this);
panel->Bind(wxEVT_LEFT_UP, &wxMiniApp::OnMouseEnd, this);
panel->Bind(wxEVT_MOTION, &wxMiniApp::OnMouseMove, this);
panel->Bind(wxEVT_PAINT, &wxMiniApp::OnPaintPanel, this);
panel->SetFocus();
panel->SetBackgroundColour(wxColor(0,0,0,100));
panel->SetBackgroundStyle(wxBG_STYLE_PAINT);
frame->SetBackgroundColour(wxColor(0,0,0,0));
frame->Show();
frame->ShowFullScreen(true);
I use panel to capture the mouse/keyboard events and I want to do the painting there too, but I don't know how to do this.
I want to have semi-transparent black background and the selected area should be transparent white or even fully transparent (without the black background).
The result of every attempt was that either it draw a solid color background or I got the famous WinXP lag effect.
Can you give me a basic OnPaintPanel(wxPaintEvent &event) implementation using x, y, with, height of the selected area (can be in wxPython too, if you're more comfortable with it) ?
You need to use SetTransparent(), wxDC doesn't support using alpha transparency anyhow (wxGraphicsContext does but it wouldn't help here).
I want to display a fullscreen transparent image anytime there is an active menu button in cocos2d. How do I do that?
If you're looking to provide a popup dialog underlay, you can use CCLayerColor:
CCLayerColor* underlay = [CCLayerColor layerWithColor:ccc4(0, 0, 0, 127)];
[self addChild:underlay z:(something less than your button's z)];
If it has to be a specific image, initialize the image and do the second line above using your CCSprite.