资源简介
QT自绘开关按钮,里边带预览图!手机上那种左右滑动开关按钮
代码片段和文件信息
#include “switchwidget.h“
CSwitchWidget::CSwitchWidget(QWidget *parent): QWidget(parent)
{
m_pressstyle = GRAPH_PRESSstyle_SYSTEM1;
//渐进色
m_offbkcolor1 = QColor(133 133 133); //分开背景色1
m_offbkcolor2 = QColor(99 99 99); //分开背景色2
//渐进色
m_onbkcolor1 = QColor(91 137 14); //闭合背景色1
m_onbkcolor2 = QColor(711038); //闭合背景色2
m_slidecolor1 = QColor(252 252 252);
m_slidecolor2 = QColor(224 224 224);
m_textcolor = QColor(255 255 255);
m_textfont = QFont(QString::fromLocal8Bit(“宋体“) 10);
m_offname = QString::fromLocal8Bit(“分“);
m_onname = QString::fromLocal8Bit(“合“);
m_state = 0;
}
CSwitchWidget::~CSwitchWidget()
{
}
void CSwitchWidget::mousePressEvent(QMouseEvent * event)
{
QPoint cursorpos = event->pos();
int x = cursorpos.x();
int y = cursorpos.y();
if (event->button() == Qt::LeftButton) {
if (x < width() / 2.0) emit StateChanged(1);
else emit StateChanged(0);
}
repaint();
QWidget::mousePressEvent(event);
}
void CSwitchWidget::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
QRect widgetrect(0 0 width() height());
DrawPressstyle(&painter widgetrect m_state);
//QPalette pa;
//QBrush brush(Qt::NoBrush);
//qDrawShadePanel(&painter 0 0 width() height() pa TRUE 2 &brush);
}
void CSwitchWidget::DrawPressstyle(QPainter *painter QRect &rect uchar state)
{
if (painter == NULL) return;
DrawPressBkGround(painter rect state); //绘制背景
DrawPressText(painter rect state); //绘制文字
DrawPressSlide(painter rect state); //绘制滑块
}
void CSwitchWidget::DrawPressBkGround(QPainter *painter QRect &rect uchar state)
{
QBrush oldbrush = painter->brush();
QPen oldpen = painter->pen();
painter->save();
int panestyle = GetPressstyle();
int x1 = rect.left(); int y1 = rect.top();
int x2 = rect.right(); int y2 = rect.bottom();
int width = rect.width(); int height = rect.height();
if (panestyle == GRAPH_PRESSstyle_SYSTEM1) { //矩形背景、方形滑块
if (state > 0) { //投入状态
painter->setPen(Qt::NoPen);
QLinearGradient linGrad(rect.left() rect.top() rect.left() rect.bottom());
linGrad.setColorAt(0 m_onbkcolor1);
linGrad.setColorAt(1 m_onbkcolor2);
linGrad.setSpread(QGradient::PadSpread);
painter->setBrush(linGrad);
painter->drawRoundedRect(rect 5.0 5.0);
}
else { //退出状态
painter->setPen(Qt::NoPen);
QLinearGradient linGrad(rect.left() rect.top() rect.left() rect.bottom());
linGrad.setColorAt(0 m_offbkcolor1);
linGrad.setColorAt(1 m_offbkcolor2);
linGrad.setSpread(QGradient::PadSpread);
painter->setBrush(linGrad);
painter->drawRoundedRect(rect 5.0 5.0);
}
//画立体阴影......
painter->setBrush(Qt::NoBrush);
QPen tmppen(Qt::darkGray);
tmppen.setWidth(2.0);
painter->setPen(tmppen);
painter->drawRoundedRect(rect 5.0 5.0);
}
else if (panestyle == GRAPH_PRESSstyle_SYSTEM2) {
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 10025 2014-02-11 10:03 switchwidget.cpp
文件 3016 2014-02-11 10:03 switchwidget.h
文件 6659 2014-02-11 10:36 widget.PNG
- 上一篇:网络安全课程设计 剖析特洛伊木马
- 下一篇:Labview数据断电自动保存
相关资源
- Qt版的Rtsp客户端 源代码
- 简单的数据库查询系统窗口实现
- QT实现的自定义窗口框架
- 多路模拟开关cc4051功能扩展方法
- Qt5.6简体中文翻译文件
- Qt5.9Creator自定义信号(SignalTest)
- MQTT_STM32_W5500
- Qt5.8 打开指定路径txt文件 读写TXT文件
- 用QT设计的一个计算器
- QT+release
- N皇后_Qt图形界面_
- QT日历程序
- qt 故障树 带画图功能
- QT绘制简单OpenGL图形
- Qt静态编译的设置过程Qt5.1.0
- QT4 QCA 加密库
- v4l2 qt实时显示摄像头数据未使用ope
- zigbee的串口编程应用源代码
- QT+opencv边缘检测,轮廓提取及轮廓跟
- QT+opencv图像增强,包括高斯平滑,中
- qt 串口通讯发送和接收都有
- QT5 下 usb 设备插拔检测
- QWebChannel
- Qt双滑块滑动器(slider)控件
- QT 源码 圆形时钟
- 基于QT+Linux的网络聊天室
- QGraphicsView Draw
- QT-玛丽奥医生的源代码
- QT画坐标系折线图
- MainWindow和Dialog通过信号和槽通信
评论
共有 条评论