以矩形为例:
cpp
QPainter painter(this);
// painter.setPen(Qt::blue);
// painter.setFont(QFont("Arial",30));
// painter.drawText(rect(),Qt::AlignCenter,"Hello Qt!!!");
QRect rect = QRect(50, 50, 400, 400);
QRect box = QRect(100, 100, 105, 63);
QColor maskColor(255, 0, 0, 255);
QColor fontColor(0, 139, 139, 255 );
QPainterPath path;
path.addRect(rect);//添加路径
path.addRect(box); //添加路径
QPainterPath subRect;
subRect.addRect(box);
path.subtracted(subRect);//减去路径及其所包含的区域
painter.fillRect(box,fontColor);
painter.fillPath(path, maskColor);