Code for the beginning:
QColor yellow("#f0d048");
Qt::BrushStyle style = Qt::SolidPattern;
QBrush brush(yellow, style);
painter.setBrush(brush);
painter.drawEllipse(10,10,10,10);
Everytime I do this, I get a yellow circle surrounded by a black 1-pixel-sized border. In total the circle will have the same size like if I draw with black colour, so what shall I do to just get a single-coloured yellow circle without black border?
Best regards
Set a pen on painter
painter.setPen(Qt::NoPen);
Qt has 'brush' for filling figures, and 'pen' for drawing lines and outlines.
Related
Simply put, when I draw a border of a certain shape using QPainter with a pen of width e.g. penWidth = 10.0 then one half of the border width is actually painted outside of the shape area and one half is drawn inside.
I would however like to paint a shape with a pen such that the border is only on the inside of the shape area.
I probably can use this trick: I set the pen's width twice as big and I also set the clip path so that the outer half of the border line is clipped away and only the inner half of the border line remains.
Example:
QColor penColor(Qt::red);
qreal penWidth = 5.0;
QPainterPath shape;
// ...here I define the shape
QPainter painter(device);
// the trick comes here
QPen p(penColor, penWidth * 2); // we make the border pen twice as thick
painter.setClipPath(path); // and we clip the outer half of the border away
// now let's paint it
painter.drawPath(shape);
I think this is probably not the most efficient way since clipping is probably quite expensive operation.
Isn't there any other more elegant way?
I'm trying to draw a rectangle with a border.
RECT rect;
GetClientRect(hwnd, &rect);
FillRect((HDC)wParam, &rect, CreateSolidBrush(RGB(0,0,0))); //black fill
FrameRect((HDC)wParam, &rect, CreateSolidBrush(RGB(255,0,0))); //red border
The above code works, though I'm not actually sure if that's the proper way to do it.
Anyway, I would like to set the thickness of the border. But I'm having a hard time figuring how. Can anyone can point me in the right direction or much better give a working example?
If GDI+ is allowed then this is how I'll do it
int width = 100; //set the width.
int height = 100; //set the height.
int borderSize = 5; //set border size.
HDC hdc = GetDC(hwnd); //don't forget to release later when done.
Graphics g(hdc);
SolidBrush brush((Color(255, 241, 103, 210))); //Set the fill color.
Pen pen(Color(255,255, 0, 0), borderSize); //Set the border color and border size.
width = width-borderSize; //deduct the border size to width to get the same point we wanted, for border only.
height = height-borderSize; //deduct the border size to height to get the same point we wanted, for border only
g.DrawRectangle(&pen, (borderSize / 2), (borderSize / 2), width, height); //draw the border
g.FillRectangle(&brush, borderSize, borderSize, width-borderSize, height - borderSize); //draw the fill shape, and again deduct the border size to the width and height to get the same point we wanted.
DeleteObject(&brush); //always delete object
DeleteObject(&pen); //always delete object
ReleaseDC(hwnd, hdc); //always release hdc
It's a little tricky, refer to the comments for the explanation.
FrameRect documentation says
The width and height of the border are always one logical unit.
So, one option is to change the mapping between logical units and physical units. If you don't want to mess up subsequent drawing operations, you'll have to save the old mapping and restore it after drawing your rectangle and frame. And the rectangle dimensions will need to change when the mapping changes. What a mess!
A better and simpler option is to stop using FrameRect. Simply use FillRect and the border brush, then DeflateRect to obtain a rectangle representing the interior, and FillRect that rectangle with your fill brush. The parameters to DeflateRect will determine the border thickness.
This doesn't work for creating hollow borders the way that FrameRect does, but for a border on a filled rectangle it is an elegant solution.
I am new to QT and I want to draw some image inside a shape and image should be croped by a shape.
I use the following code to draw a rectangle for example. How to draw image only inside shape?
QPen pen(Qt::black,penWidth);
pen.setStyle(Qt::DashLine);
QPicture picture;
picture.load("drawing.pic");
painter->setPen(pen);
painter->drawPicture(0,0, picture);
painter->drawRoundedRect( (QRectF(x, y, height, width),radius,radius);
PS. Can I load an image to brush?
Just set a QBrush for painter.
QBrush brush(QPixmap("file.png"));
painter->setBrush(brush);
It will draw the rounded rect filled with file.png image.
I am changing the color of my sprite using the following code
sprite.color = ccc3(255, 0, 0);
it changes the color to red..
How can I change the color to what it was??
Thanks..
You can return to original color by using
sprite.color = ccc3(255, 255, 255);
the original color was not lost. the tint methods (ccc3 in this case) are not adding-color, but darkening the individual RGB channels. That's the reason you cant tint a black image to any other color.
In your example, you didn't paint your sprite Red. You've just cut all channels except the red one
Here's an example:
SDL_Color textcol = {255, 255, 255};
SDL_Color backtextcol = {0, 0, 0};
SDL_Surface *mes = TTF_RenderText_Shaded(font, "The game has begun", textcol, backtextcol);
apply_surface(40, 40, mes, screen);
SDL_Flip(screen);
Thanks to that example, we draw a black rectangle with white letters. Is it possible to draw just white letters with black inside without the whole black rectangle?
Yes it is possible. There are 3 main text-drawing functions in SDL_TTF:
TTF_RenderText_Solid - this one renders basic text(whitout background) and its fast, but its low-quality, and will look pixellated.
TTF_RenderText_Shaded - this one draws nice blended characters, but to a pre-defined background color
TTF_RenderText_Blended - this one draws a nice blended text, but it uses alpha-blending and its slow, but gives very nice results