資源簡介
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? 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數據斷電自動保存
評論
共有 條評論