qt 自定义控件
Qt::Alignment alignments[9] = {
Qt::AlignTop | Qt::AlignLeft, Qt::AlignTop | Qt::AlignHCenter, Qt::AlignTop | Qt::AlignRight,
Qt::AlignVCenter | Qt::AlignLeft, Qt::AlignCenter, Qt::AlignVCenter | Qt::AlignRight,
Qt::AlignBottom | Qt::AlignLeft, Qt::AlignBottom | Qt::AlignHCenter, Qt::AlignBottom | Qt::AlignRight,
};
QPixmap skiMakeCellForm::createDotPixmap(Qt::Alignment align) {
QPixmap pixmap(btnSize, btnSize);
pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setPen(QPen(Qt::black, 1));
int dotRadius = 3;
QPoint center;
int w = pixmap.width();
int h = pixmap.height();
// 矩形边距
int edge = 1;
QRect boxRect(edge, edge, w - edge * 2, h - edge * 2); // 24x24 正方形
// 先画一个矩形框
painter.drawRect(boxRect);
// 计算小圆点中心
if (align & Qt::AlignLeft) center.setX(boxRect.left());
else if (align & Qt::AlignHCenter) center.setX(boxRect.center().x());
else if (align & Qt::AlignRight) center.setX(boxRect.right());
if (align & Qt::AlignTop) center.setY(boxRect.top());
else if (align & Qt::AlignVCenter) center.setY(boxRect.center().y());
else if (align & Qt::AlignBottom) center.setY(boxRect.bottom());
// 延伸线段
int lineLength = 2;
painter.drawLine(center.x(), center.y() - dotRadius,
center.x() center.y() - dotRadius - lineLength); // 上
painter.drawLine(center.x(), center.y() + dotRadius,
center.x(), center.y() + dotRadius + lineLength); // 下
painter.drawLine(center.x() - dotRadius, center.y(),
center.x() - dotRadius - lineLength, center.y()); // 左
painter.drawLine(center.x() + dotRadius, center.y(),
center.x() + dotRadius + lineLength, center.y()); // 右
// 画小圆点
painter.setBrush(Qt::white);
painter.drawEllipse(center, dotRadius, dotRadius);
return pixmap;
}