//应沙雕要求绘制开关,备用
void SwitchBtn::paintEvent(QPaintEvent *event)
{
QPainter m_painter = QPainter(this);
QPoint LCC;
LCC.setX(this->rect().topLeft().x() + this->height() / 2);
LCC.setY(this->rect().topLeft().y() + this->height() / 2);
//设置反锯齿
m_painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::Qt4CompatiblePainting);
QPoint LC;
LC.setX(LCC.x());
LC.setY(LCC.y() - this->height() / 2);
QPoint RCC;
RCC.setY(LCC.y());
RCC.setX(LCC.x() + (this->width() - 3 * this->height()));
QPoint textPoint;
m_painter.save();
if (m_checkState) {
m_painter.setPen(Qt::NoPen);
m_painter.setBrush(QColor(61, 113, 250));
m_painter.drawRect(LC.x(), LC.y(), this->width() - 3 * this->height(), this->height());
m_painter.drawEllipse(LCC, this->height() / 2, this->height() / 2);
m_painter.setBrush(QColor(250, 250, 250));
m_painter.drawEllipse(RCC, this->height()/2 + 1, this->height()/2 + 1);
m_painter.setPen(QColor(255, 255, 255));
QFont font("Microsoft YaHei UI",8);
m_painter.setFont(font);
QString text = QString(tr("Open"));
textPoint.setX(LCC.x());
textPoint.setY(LCC.y() + this->height() / 5);
m_painter.drawText(textPoint, text);
}
else {
m_painter.setPen(Qt::NoPen);
m_painter.setBrush(QColor(235, 235, 235));
m_painter.drawRect(LC.x(), LC.y(), this->width() - 3 * this->height(), this->height());
m_painter.setBrush(QColor(235, 235, 235));
m_painter.drawEllipse(RCC, this->height() / 2, this->height() / 2);
m_painter.setBrush(QColor(220, 220, 220));
m_painter.drawEllipse(LCC, this->height() / 2, this->height() / 2);
m_painter.setPen(QColor(0, 0, 0));
QFont font("Microsoft YaHei UI", 8);
m_painter.setFont(font);
QString text = QString(tr("Open"));
int Delta = QFontMetrics(font).width(text) + 2;
textPoint.setX(RCC.x() - Delta);
textPoint.setY(LCC.y() + this->height() / 5);
m_painter.drawText(textPoint, text);
}
m_painter.restore();
}