Qt实践项目打地鼠之----背景布局(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/fly_wt/article/details/82016535

上文中我们将一个图元封装在场景中进而显示在GraphicsView的容器中.这一个节我们继续操作完成16张背景图的布局.

首先说明一下坐标系统,图元的显示默认是中心的对角线位置,只有当图元的摆放位置超过场景大小时,坐标才会从左上角初始化位置.

添加第二个图元操作(图片像素为91*91)

#include "myscene.h"

myScene::myScene(QObject *parent) : QGraphicsScene(parent)
{
    this->item = new myitem;//开辟堆空间
    this->item->setPos(0,0);//设置坐标位置
    this->addItem(this->item);//将item添加进场景中

    this->item2 = new myitem;
    this->item2->setPos(91,0);//设置第二个坐标位置
    this->addItem(this->item2);//将item2添加进场景中

}

此时

                   

我们添加16张图显然要用到循环否则一个一个写太蠢了 .首先我们为每个位置编号

                                               

当然二维数组可以构造,但是这里我们用一维数组仍然可以构造二维矩阵

i 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
i/4 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3
i%4 0 1 2 3 0 1 2 3 0 1 2 3 0 1 2 3


这是运行程序显示:

   for(int i = 0;i<16;i++)
    {
        this->item[i] = new myitem;
        this->item[i]->setPos(i/4 * this->item[i]->boundingRect().width(),
                              i%4 *this->item[i]->boundingRect().height());
        this->addItem(this->item[i]);
    }

                         

下一部分来写随机产生地鼠的程序,敬请期待. 

猜你喜欢

转载自blog.csdn.net/fly_wt/article/details/82016535