Qt圆角矩形,为什么拐角不同? [英] Qt round rectangle, why corners are different?

查看:331
本文介绍了Qt圆角矩形,为什么拐角不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试用drawRoundedRect方法直接在QPixmap中绘制一个圆形矩形(此处没有渲染引擎涉及纯Qt一个……),我仔细检查了矩形的大小与像素图的大小:

I try to draw a round rectangle with drawRoundedRect method directly in a QPixmap (no render engine involve here exept pure Qt one ...), I double check the size of the rectangle versus the size of my pixmap :

Pixmap : QSize(50, 73) 
Rectangle: QRect(0,0 48x11) 

看到足够的空间...

See plenty of space ...

一些代码

pixmap = QPixmap(50,73); //example size that match my case
QRectF rect(0,0,48,11);

QPainter painter(&pixmap);
painter.setRenderHint(QPainter::TextAntialiasing);
painter.setWorldMatrixEnabled(false);
painter.setPen(QPen()); //no pen
painter.setBrush(QBrush(color));
painter.drawRoundedRect(rect, 2.0, 2.0);

  • 我禁止了世界转型...
  • 我将转换设置为统一...
  • 我尝试了几个半径(1.0,2.0,3.0,4.0)...
  • 我改变笔的宽度,刷子的颜色...
    • I disabled world transformation ...
    • I set set transformation to unity ...
    • I tried several radius (1.0,2.0,3.0,4.0) ...
    • I change pen width, brush color ...
    • 但是它总是以四个不同的角重新结束!像这样:

      But it always ends with a rectamgle with 4 diferent corners ! Like that :

      我直接将像素图输出到文件中,以确保在显示过程中没有刮擦它……形状相同.

      I directly ouptut the pixmap to a file to be sure I wasn't scraping it during the display ... same shape.

      有人知道半径小的Qt圆形矩形吗?很久以前,我看到了一些关于它的东西,但是我不建议如何处理它!

      Anyone know about Qt round rectangle with small radius ? I saw somthing about it a long time ago but I don't remenber how to deal with it !

      推荐答案

      您似乎没有使用抗锯齿功能(即QPainter::Antialiasing渲染提示).这是一个没有它的Qt怪癖.从我所看到的/听到的情况来看,Qt开发人员并不是很在意解决此问题(无论如何,大多数人都希望进行抗锯齿).

      It looks like you're not using anti-aliasing (i.e. the QPainter::Antialiasing render hint). This is a Qt quirk that occurs without it. From what I've seen/heard, the Qt devs aren't terribly concerned with fixing this (most people want anti-aliasing anyway).

      解决方法(仅使用抗锯齿)是使用QPainter::drawLine()QPainter::drawArc()自己绘制矩形.您可能需要对数字进行操作,直到看起来正确为止-直线计算往往会得出一两个像素的误差.另外,您可能会发现,即使使用这种方法,右下角也永远不会与其他角完全相同.

      The work-around (besides just using anti-aliasing) is to draw the rect yourself with QPainter::drawLine() and QPainter::drawArc(). You might have to play with numbers until it looks right -- straight calculations tend to come out a pixel or two off. Also, you might find that even with this method the lower right corner is never exactly the same as the other corners.

      如果您有野心,可以尝试修复此问题并向Qt提交补丁.

      If you're feeling mildly ambitious, you could try fixing this and submitting a patch to Qt.

      更新:在Qt 5中更改了圆弧绘制结果.以我的经验,这是一个很大的改进.

      Update: Arc drawing results changed in Qt 5. In my experience, it's a big improvement.

      这篇关于Qt圆角矩形,为什么拐角不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆