qt 绘制开关

//应沙雕要求绘制开关,备用
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();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值