QFont font;
    font.setPointSize(15);
    
    for(int i=0;i<15;i++)
    {
        for(int j=0;j<18;j++)
        {
            QPointF p(-2400+20+(RECT_WIDTH+30)*i,-1500+20+(RECT_HEIGHT+30)*j);
            QGraphicsRectItem* item = new QGraphicsRectItem(p.x(),p.y(),RECT_WIDTH,RECT_HEIGHT);
            item->setToolTip("click me");
            item->setBrush(QColor(79,136,187,255));
            scene->addItem(item);
            QGraphicsTextItem* text = new QGraphicsTextItem(item);
            text->setPlainText(QString("%1,%2").arg(i).arg(j));
            QRectF rect = text->boundingRect();
            text->setDefaultTextColor(QColor(255,255,255));
            p.setX(p.x() + RECT_WIDTH/2 - rect.width()/2);
            text->setPos(p);
            text->setFont(font);
        }
    }
按照经验以为
QGraphicsRectItem会有setText接口,之后觉得应该定制一个QGraphicsItem
搜了下原来QGraphicsRectItem是树状结构可以增加子项
因此就好办多了,代码如上