I want to know how Qt does a border when using QPainter's drawRect. The reason for this is I am trying to draw three rectangles next to each other, but I'm having trouble getting them to touch perfectly at all pen sizes.
QPainter's documentation for drawRect says:
A stroked rectangle has a size of [the input rectangle] plus the pen width.
So it goes like this:
I just wanted to add to the answer and address the question about truncation.
Truncation might be happening because you are using the QRect and not QRectF. QRectF gives you floating point precision. Similarly, you can use QPen::setWidthF(qreal width) to ensure your border is not getting truncated either.
Related
I'm working on a Slotmachine with QGraphicsScene and -View and I'm close to the end of that project. But now I want to add a small rect to the winning-lines that shows the amount of won credits in that line. The rect is a QLabel in a QGraphicsProxyWidget. I don't know if thats the best way to solve this problem but I'm not able to find a better solution at the moment. I'm setting the proxyWidget to not visible in the constructor of the label but when I'm setting this proxyWidget to visible other proxyWidgets in the scene disappear. And I just dont understand why. It's not hiding every proxyWidgets.
As you can see on the picture there's a red rect in the middle of the first square of the winning line. This is causing the problem. It is hiding the lower white border which is a QGraphicsPixmapItem and the first of the normally three QGraphicsProxyWidgets at the bottom, stake(this is not visible), last gain(letzter Gewinn), credit(Guthaben). I don't know why its not hiding the other Widgets on the bottom because they are all equal. As you can see its not hiding the complete lower border. I just don't know why this happens and why it just happens to the lower border and the left widget on the bottom and not to any other elements. I just don't know how to fix a problem like that.
It's a bit long to read but I don't know how to really describe the problem or how I could show you in the Code. I hope someone could help me.
Solved it by using an QGraphicsTextItem. totally forgot that it is existing. Thanks to the QT Forum.
Hello guys
I have a small problem while designing a iphone game with a grid using cocos2d.
The game needs a 10x10 grid in the middle of the screen (it is not covering the entire screen).
A line is drawn at runtime where the user touches two points in the grid.
Question: would tilemap be ideal for this problem? As i need to verify the co-ordinates do belong to the grid or not when the user touches a point would tilemap be useful?
Question: Is there any better way of solving this in cocos2d. Please help me out.
Thanks
I wouldn't recommend using tilemap for this. Personally I'd do it all with math.
Lets for arguments sake say your grid squares are 10px by 10px.
You now instantly know the positions of the rects for each square.
top right square would be (90, 0, 10, 10), this obviously doesn't include the positioning of your grid, but you can easily add that onto this by adding.. (90+gridPos.x, 0+gridPos.y, 10, 10).
Then you just check your touches intersect the rects of the grids.
Drawing a line is fairly simple, i imagine you'd draw it from the center of the 2 grid points.
So if the line started in the top right grid square it's initial point would start at (90+gridPos.x, 0+gridPos.y, 5, 5), or (90+gridPos.x, 0+gridPos.y, gridSquareHeight/2, gridSquareWidth/2)
Using cocos2d it's pretty easy to also make every square a touchable sprite, that can react when touched however you like, sending a message back to a delegate or even just doing a visual effect.
There are tonnes of possibilities for solving this problem.
I'm using gdiplus to "stroke" a textout. In certain circumstances, we see a "spike" appearing on the top or bottom of the graphic, and I'm not really sure why. We can minimize this by adjusting stroke width and font size, but thats not a good solution. I'm hoping someone can explain the problem to me.
And the code sample generating this 4, its outline, and the spike (unintentional)
GraphicsPath path(FillModeWinding);
path.AddString(text,wcslen(text),&fontFamily,StateInfo.TheFont.TheWeight,(REAL)minSize,PointF((REAL)ptStart.x, (REAL)ptStart.y),&sf);
// Draw the outline first
if (StateInfo.StrokeWidth > 0) {
Gdiplus::Color strokecolor(GetRValue(StateInfo.StrokeColor), GetGValue(StateInfo.StrokeColor), GetBValue(StateInfo.StrokeColor));
Pen pen(strokecolor,(REAL)StateInfo.StrokeWidth);
graphics.SetSmoothingMode(Gdiplus::SmoothingModeAntiAlias);
graphics.SetPixelOffsetMode(Gdiplus::PixelOffsetModeHighQuality);
graphics.DrawPath(&pen, &path);
}
// Draw the text by filling the path
graphics.FillPath(&solidBrush, &path);
Use Pen::SetLineJoin on the Pen you're using to draw the outline, and use something other than LineJoinMiter.
I agree that the fill mode isn't the issue, I think it is just the pen width used for drawing the outline. For characters that have enclosed spaces with pointy corners (like 4 and 'A'), as the pen width used for drawing the outline gets bigger, the size of the inner shape (the little triangle in the case of the four) gets bigger too.
Eventually the inner shape will get too big to be contained by the outer shape, and will start to poke through, resulting in the artifact you see.
Here is an illustration of a fixed font size (the Impact font again) as the outline width gets bigger. There is no fill here, just a call to graphics.DrawPath():
The fill operation doesn't care about the outline width, and uses the original shape of the letter.
This partially masks the problem by covering up some of the messy outline. Here is with the fill turned on:
Something similar will happen with the character 'A':
EDIT: calling SetLineJoin, as indicated in the other answer, is the way to stop this from happening.
How can I create a rounded rectangle with a dashed line? I've seen several routines that draw their own straight lines, but nothing for rounded rectangles.
There used to be (probably still is) a great AS 2/3 library for this but I can't find it.
This looks pretty promising:
http://blog.alegitimatebusiness.com/2007/11/07/as2-as3-draw-a-dotted-rounded-corner-box/
I have a problem on roating an Image on a canvas in gdi+, I am using the following code, however I find there are alias on the edge.
myPathMatrix.Rotate(GetDCAngle(), MatrixOrderAppend);
myPathMatrix.Translate(
GetDCX(),
GetDCY(),
MatrixOrderAppend);
canvas->SetTransform(&myPathMatrix);
canvas->Draw(XXX);
I used the following code to remove the alias, but failed.
canvas->SetInterpolationMode(InterpolationMode::InterpolationModeHighQualityBicubic);
canvas->SetSmoothingMode(SmoothingModeAntiAlias);
How can I remove the alias at rotated image's edges.
Many thanks!
It's been a while since I worked with GDI+, but I'd assume there's no effective option to remove antialiasing around the edges when rotating. The reason is basically that pixels are square. To rotate an image an amount other than a multiple of 90 degrees, you need to use some kind of interpolation to estimate pixel colours where there weren't really pixels before.
So if there's nothing in the library to specifically take away antialiasing around the edges, have you thought about drawing hard lines in the background colour along the borders? It should be easier to draw those lines without any antialiasing.