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是樹狀結構可以增加子項
因此就好辦多了,代碼如上