How can I set the background color for a part of the background like in the following image:
Of course, without border frames, I want to set only the cyan color.
I need to set the length of the left part (cyan) as the percentage of the widget length, e.g 30%.
With css I would hack qlineargradient a little bit. Note that edge of cyan may be a little blurry.
QFrame
{
background-color: qlineargradient(x1:0, x2: 1, stop: 0 cyan, stop: 0.29 cyan, stop: 0.2901 white, stop: 1 white);
}
If you want it hard-coded in the application, you can overload the paintEvent function in a widget. Something like this:
void MyWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QPen pen(Qt::NoPen);
painter.setPen(pen);
painter.fillRect(0, 0, width(), height(), Qt::white);
painter.fillRect(0, 0, 0.3*width(), height(), Qt::cyan);
...
}
Related
I'm writing some C++ code to draw ellipses. Sometimes these ellipses could be stacked on top of each other in a grouping. When they are stacked on each other, I'd like to have the bounding box of the ellipse be transparent so that I don't see the white corners of the bounding box. See picture below.
Including the SetBkMode or not doesn't seem to make a difference. If I don't do the FillRect, I get a black background on the bounding box.
HBRUSH brush = CreateSolidBrush(RGB(255, 255, 255));
CDC *pDC = CDC::FromHandle(pSprite->hDCMem);
pDC->SetBkMode(TRANSPARENT);
pDC->FillRect(m_SpriteRect, CBrush::FromHandle(brush));
pDC->SelectObject(m_BackBrush);
pDC->Ellipse(m_SpriteRect);
pDC->SetBkMode(OPAQUE);
DeleteObject(brush);
Is there a way to set a transparent background?
If drawing on memory dc, fill the background with a transparent color, then use TransparentBlt to blit the memory dc on to final HDC. Example:
CDC *pDC = CDC::FromHandle(hDCMem);
//fill the background with transparent color
COLORREF clr_transparent = RGB(255, 255, 255); //<- randomly selected color
CBrush brush(clr_transparent);
pDC->FillRect(m_SpriteRect, &brush);
//any drawing
auto oldbrush = pDC->SelectObject(m_BackBrush);
pDC->Ellipse(m_SpriteRect);
pDC->SelectObject(oldbrush);
//transparent blit
TransparentBlt(final_hdc, x_dest, y_dest, width, height,
hDCMem, 0, 0, m_SpriteRect.right, m_SpriteRect.bottom, clr_transparent);
I want to make QLabel with the image circle:
Code:
QLabel *label = new QLabel(this);
QPixmap avatarPixmap(":/Icon/default_avatar.png");
label->setPixmap(avatarPixmap);
label->setStyleSheet("border: 0.5px solid red; border-radius: 50%; background-clip: padding;");
It only rounds the QLabel, not the image. How to fix it? Thanks.
Update:
The only way is to override the paintEvent for QLabel
Code:
void AccountImage::paintEvent(QPaintEvent *event)
{
QPixmap pixmap(":/Icon/default_avatar.png");
QBrush brush(pixmap);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(brush);
painter.drawRoundedRect(0, 0, width(), height(), 100, 100);
QLabel::paintEvent(event);
}
The image is rounded but not properly scaled. Any ideas?
try to set mask on the label like:
int w = // set the width here
int h = // set the height here
QRect *rct = new QRect(0, 0, w, h);
QRegion *reg = new QRegion(*rct, QRegion::Ellipse);
label->setMask(*reg);
see: http://doc.qt.io/archives/qt-4.8/qwidget.html#setMask
The solution by overriding QLabel paintEvent method.
Code:
void AccountImage::paintEvent(QPaintEvent *event)
{
QPixmap pixmap(":/Icon/my_avatar.png");
QPixmap scaled = pixmap.scaled(width(), height(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
QBrush brush(scaled);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(brush);
painter.drawRoundedRect(0, 0, width(), height(), 100, 100);
QLabel::paintEvent(event);
}
Result:
I have a responsible function for drawing a rectangle on the screen, it is inside the main loop of my program, it does this:
DrawRetangle (SDL_Renderer * Renderer, SDL_Rect MyRect) {
// Clean the renderer
SDL_RenderClear (Renderer);
// Position x, y, width and height of the rectangle.
MyRect = {x, y, width, height};
// Rectangle border color
SDL_SetRenderDrawColor (Renderer, 0, 0, 0, 255);
// Draw the edges of the rectangle
SDL_RenderDrawRect (Renderer, & MyRect);
// Color from within the rectangle
SDL_SetRenderDrawColor (Renderer, 255, 255, 255, 255);
// Fill in the rectangle
SDL_RenderFillRect (Renderer, & MyRect);
// Show in window
SDL_RenderPresent (Renderer);
}
Note that I am trying to draw a rectangle with a black border and filled with white, but it gets all black, I know I can solve this, cleaning, drawing the border, showing, cleaning, drawing the interior and showing, however if I tier many rectangle , This would be bad, is there a better way to do this?
Up 1:
To function would it be just this way, as below?
DrawRetangle (SDL_Renderer * Renderer, SDL_Rect MyRect) {
// Clean the renderer 1
SDL_RenderClear (Renderer);
// Position x, y, width and height of the rectangle.
MyRect = {x, y, width, height};
// Rectangle border color
SDL_SetRenderDrawColor (Renderer, 0, 0, 0, 255);
// Draw the edges of the rectangle
SDL_RenderDrawRect (Renderer, & MyRect);
// Show in window 1
SDL_RenderPresent (Renderer);
// Clean the renderer 2
SDL_RenderClear (Renderer);
// Color from within the rectangle
SDL_SetRenderDrawColor (Renderer, 255, 255, 255, 255);
// Fill in the rectangle
SDL_RenderFillRect (Renderer, & MyRect);
// Show in window 2
SDL_RenderPresent (Renderer);
}
Up 2:
Another solution I found was to call the function below twice, creating a rectangle on top of another, the one inside a little smaller, thus looking like a border. But I still do not think it should be the best way to do it.
DrawRetangle (SDL_Renderer * Renderer, SDL_Rect MyRect, int x, int, y, int width, int height, int r, int g, int b, int a) {
// Position x, y, width and height of the rectangle.
MyRect = {x, y, width, height};
// Color from within the rectangle
SDL_SetRenderDrawColor (Renderer, r, g, b, a);
// Clean the renderer
SDL_RenderClear (Renderer);
// Fill in the rectangle
SDL_RenderFillRect (Renderer, & MyRect);
// Show in window
SDL_RenderPresent (Renderer);
}
before drawing filled rectangle change the size and position of your rectangle.
and then try .
Myrect->x+=borderSize; //whatever size you want
Myrect->y+=borderSize;
Myrect->h-=(borderSize*2);
Myrect->w-=(borderSize*2);
// Color from within the rectangle
SDL_SetRenderDrawColor (Renderer, 255, 255, 255, 255);
// Fill in the rectangle
SDL_RenderFillRect (Renderer, & MyRect);
I have a custom class called Window that extends QWidget.
#include "window.h"
// This is the base that all (MDI) sub-windows extend off
Window::Window()
{
// Add fake icon to remove icon in all
setWindowIcon(QIcon("."));
}
void Window::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}
css:
QWidget:title
{
background-color: QLinearGradient( x1: 0, y1: 0, x2: 0, y2: 1, stop: 0 #ffa02f, stop: 1 #ca0619);
}
When I do this the buttons (minimize, maximize and close) disappear (as seen in the picture below).
So my question, how do I properly style my Window titlebar?
In a game loop that I have, part of the drawing section is:
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 100);
SDL_RenderFillRect(renderer, &blur);
renderer is my renderer and blur is my rect that takes up the whole screen:
SDL_Rect blur;
blur.x = 0;
blur.y = 0;
blur.w = 640;
blur.h = 480;
My problem is that the rect isn't semi transparent. Whenever it draws it, all there is is black.
You cant even see the text that I have underneath. How do I fix this? Does my renderer not support
alpha?
The reason the alpha value isn't affecting anything is because you need to specify which colour blending method you want to use beforehand with this function:
int SDL_SetRenderDrawBlendMode(SDL_Renderer* renderer, SDL_BlendMode blendMode)
The blendMode parameter controls how colour blending works. For alpha color blending, use SDL_BLENDMODE_BLEND:
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 100);
SDL_RenderFillRect(renderer, &blur);