自定义QPushButton外部轮廓
类声明如下:
class CHexagon : public QPushButton
{
public:
CHexagon(QWidget *parent=0);
virtual ~CHexagon() {}
protected:
void paintEvent(QPaintEvent* pEvent);
};
类的定义:
CHexagon::CHexagon(QWidget *parent):QPushButton(parent)
{
}
void CHexagon::paintEvent(QPaintEvent *pEvent)
{
Q_UNUSED(pEvent)
QStylePainter p(this);
QStyleOptionButton option;
initStyleOption(&option);
QRectF rectF = rect();
QVector<QPointF> points;
points.append(rectF.topLeft() + QPointF(0.25*rectF.width(), 0));
points.append(rectF.topRight() + QPointF(1, 0) - QPointF(0.25*rectF.width(), 0));
points.append((rectF.topRight() + QPointF(1, 0) + rectF.bottomRight() + QPointF(1, 1))*0.5);
points.append(rectF.bottomRight() + QPointF(1, 1) - QPointF(0.25*rectF.width(), 0));
points.append(rectF.bottomLeft() + QPointF(0, 1) + QPointF(0.25*rectF.width(), 0));
points.append((rectF.topLeft() + rectF.bottomLeft() + QPointF(0, 1))*0.5);
points.append(points.first());
QPointF pos = QPointF(rectF.topLeft() + QPointF(0.25*rectF.width(), 0));
QPointF pos2 = QPointF(rectF.topRight() + QPointF(1, 0) - QPointF(0.25*rectF.width(), 0));
QPointF pos3 = QPointF((rectF.topRight() + QPointF(1, 0) + rectF.bottomRight() + QPointF(1, 1))*0.5);
QPointF pos4 = QPointF(rectF.bottomRight() + QPointF(1, 1) - QPointF(0.25*rectF.width(), 0));
QPointF pos5 = QPointF(rectF.bottomLeft() + QPointF(0, 1) + QPointF(0.25*rectF.width(), 0));
QPointF pos6 = QPointF((rectF.topLeft() + rectF.bottomLeft() + QPointF(0, 1))*0.5);
QPolygonF polygonCustom(points);
QPainterPath paintPath;
paintPath.addPolygon(polygonCustom);
p.setClipPath(paintPath);
p.setRenderHint(QPainter::Antialiasing, true);
p.drawControl(QStyle::CE_PushButton, option);
QPen pen(QColor("gray"));
p.setPen(pen);
//pos
QLineF line1(pos, pos2);
QLineF line2(pos2, pos3);
QLineF line3(pos3, pos4);
QLineF line4(pos4, pos5);
QLineF line5(pos5, pos6);
QLineF line6(pos6, pos);
p.drawLine(line1);
p.drawLine(line2);
p.drawLine(line3);
p.drawLine(line4);
p.drawLine(line5);
p.drawLine(line6);
}
使用六边形对象的类 的声明以及定义如下:
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
};
Widget::Widget(QWidget *parent) :
QWidget(parent)
{
QVBoxLayout* mainlayout = new QVBoxLayout;
QGroupBox* group = new QGroupBox;
int fixSize = 430;
group->setFixedSize(fixSize,fixSize);
int fixWidth = 130;
int fixHeight = 112;
CHexagon* bt = new CHexagon(group);
bt->setFixedSize(fixWidth,fixHeight);
int x = (fixSize - fixWidth)/2;
int y = (fixSize - fixHeight)/2;
bt->move(x,y);
CHexagon* bt2 = new CHexagon(group);
bt2->setFixedSize(fixWidth,fixHeight);
bt2->move(x,y-fixHeight);
CHexagon* bt3 = new CHexagon(group);
bt3->setFixedSize(fixWidth,fixHeight);
bt3->move(x + fixWidth - sqrt(3)*fixHeight/6,y - fixHeight/2);
CHexagon* bt4 = new CHexagon(group);
bt4->setFixedSize(fixWidth,fixHeight);
bt4->move(x + fixWidth - sqrt(3)*fixHeight/6,y + fixHeight/2);
CHexagon* bt5 = new CHexagon(group);
bt5->setFixedSize(fixWidth,fixHeight);
bt5->move(x,y+fixHeight);
CHexagon* bt6 = new CHexagon(group);
bt6->setFixedSize(fixWidth,fixHeight);
bt6->move(x - fixWidth + sqrt(3)*fixHeight/6,y + fixHeight/2);
CHexagon* bt7 = new CHexagon(group);
bt7->setFixedSize(fixWidth,fixHeight);
bt7->move(x - fixWidth + sqrt(3)*fixHeight/6,y - fixHeight/2);
mainlayout->addWidget(group);
setLayout(mainlayout);
}