找回密码
 立即注册
收起左侧

请教大家一小段代码,拜托了

7
回复
7855
查看
[复制链接]
累计签到:10 天
连续签到:1 天
来源: 2016-3-31 13:07:39 显示全部楼层 |阅读模式
10Qter豆

class OneBox : public QGraphicsObject{public:    OneBox(const QColor &color = Qt::red);    QRectF boundingRect() const;    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);    QPainterPath shape() const;private:    QColor brushColor;};//该函数为指定后面的绘图区域的外边框QRectF OneBox::boundingRect() const {    qreal pen_width = 1;    //小方块的边长为20.5像素    return QRectF(-10-pen_width/2, -10-pen_width/2, 20+pen_width, 20+pen_width);}

看不懂这一句:

-10-pen_width/2, -10-pen_width/2, 20+pen_width, 20+pen_width

回复

使用道具 举报

累计签到:10 天
连续签到:1 天
2016-3-31 13:08:47 显示全部楼层
  1. class OneBox : public QGraphicsObject
  2. {
  3. public:
  4.     OneBox(const QColor &color = Qt::red);
  5.     QRectF boundingRect() const;
  6.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  7.     QPainterPath shape() const;

  8. private:
  9.     QColor brushColor;

  10. };
复制代码
  1. QRectF OneBox::boundingRect() const {

  2.     qreal pen_width = 1;
  3.     //小方块的边长为20.5像素
  4.     return QRectF(-10-pen_width/2, -10-pen_width/2, 20+pen_width, 20+pen_width);

  5. }
复制代码
回复

使用道具 举报

累计签到:595 天
连续签到:1 天
2016-3-31 14:01:53 显示全部楼层
定义一个从(-10.5 ,-10.5)坐标开始的一个21宽,21高的矩形
回复

使用道具 举报

累计签到:10 天
连续签到:1 天
2016-3-31 16:45:20 显示全部楼层
  1. // 小方块类
  2. class OneBox : public QGraphicsObject
  3. {
  4. public:
  5.     OneBox(const QColor &color = Qt::red);
  6.     QRectF boundingRect() const;
  7.     void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget);
  8.     QPainterPath shape() const;

  9. private:
  10.     QColor brushColor;

  11. };
  12. [code]OneBox::OneBox(const QColor &color):brushColor(color)
  13. {
  14.    
  15. }
  16. QRectF OneBox::boundingRect()const
  17. {
  18.     qreal penWidth=1;
  19.     return QRectF(-10-penWidth/2,-10-penWidth/2,
  20.                   20+penWidth,20+penWidth);
  21.    
  22. }
  23. void OneBox::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)

  24. {
  25.     painter->drawPixmap(-10,-10,20,20,QPixmap(":/images/box.gif"));
  26.     painter->setBrush(brushColor);
  27.     QColor penColor=brushColor;
  28.     penColor.setAlpha(20);
  29.     painter->setPen(penColor);
  30.     painter->drawRect(-10,-10,20,20);
  31. }
  32.    
  33. QPainterPath OneBox::shape()const
  34. {
  35.     QPainterPath path;
  36.     path.addRect(-9.5,-9.5,19,19);
  37.     return path;
  38. }
复制代码
[/code]
回复

使用道具 举报

累计签到:10 天
连续签到:1 天
2016-3-31 16:51:42 显示全部楼层
sywh 发表于 2016-3-31 14:01
定义一个从(-10.5 ,-10.5)坐标开始的一个21宽,21高的矩形

麻烦你在看一下我发的代码
那个QPaintPath shape()const 函数
在整个程序都没有 OneBox对象使用他
为什么要创建这个函数?
回复

使用道具 举报

累计签到:595 天
连续签到:1 天
2016-3-31 18:04:49 显示全部楼层
估计是设计的时候以为用的到这个函数就写了,后面发现没用上也没删掉吧,我们的代码里经常有这样的,写了就放那了,万一用的上呢
回复

使用道具 举报

累计签到:10 天
连续签到:1 天
2016-3-31 20:44:14 显示全部楼层
sywh 发表于 2016-3-31 18:04
估计是设计的时候以为用的到这个函数就写了,后面发现没用上也没删掉吧,我们的代码里经常有这样的,写了就 ...

可是作者写了注释
  1. // 形状比边框矩形小0.5像素,这样方块组中的小方块才不会发生碰撞
  2. QPainterPath OneBox::shape() const
  3. {
  4.     QPainterPath path;
  5.     path.addRect(-9.5, -9.5, 19.5, 19.5);
  6.     return path;
  7. }
复制代码
/ 形状比边框矩形小0.5像素,这样方块组中的小方块才不会发生碰撞
这是为什么?
回复

使用道具 举报

尚未签到

2016-4-11 14:04:39 显示全部楼层
由于OneBox是从QGraphicsObject类中继承的,而shape是QGraphicsObject类中的虚函数,类似与boundingRect(),内部机制不需要外部调用触发;具体可参考qt助手中QGraphicsItem::shape() 解释
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

公告
可以关注我们的微信公众号yafeilinux_friends获取最新动态,或者加入QQ会员群进行交流:190741849、186601429(已满) 我知道了