QT 滑动按钮的实现.docx
《QT 滑动按钮的实现.docx》由会员分享,可在线阅读,更多相关《QT 滑动按钮的实现.docx(14页珍藏版)》请在冰豆网上搜索。
![QT 滑动按钮的实现.docx](https://file1.bdocx.com/fileroot1/2022-11/25/999f5346-8c2c-4520-83aa-1b98d4cdc01b/999f5346-8c2c-4520-83aa-1b98d4cdc01b1.gif)
QT滑动按钮的实现
方法一:
mouselabel.h
#ifndefMOUSELABEL_H
#defineMOUSELABEL_H
#include"myslider.h"
classMySliderMenu;
//为做设置界面里一个漂亮滑块而写的类。
。
。
classmouseLabel:
publicQLabel
{
friendclassMySliderMenu;
Q_OBJECT
private:
QStringImageBgPath;
QStringImageSwPath;
MySliderMenu*FocusLabel;
boolreleaseFlag;
public:
mouseLabel(QWidget*parent,MySliderMenu*focusLabel);
voidmousePressEvent(QMouseEvent*e);
voidmouseReleaseEvent(QMouseEvent*e);
voidmouseMoveEvent(QMouseEvent*ev);
voidsetTwoImage(QStringImageBg,QStringImageSw);
};
#endif//MOUSELABEL_H
----------------------------------------------------------------
myslider.h
classmouseLabel;
//为做设置界面里一个漂亮滑块而写的类。
。
。
classMySliderMenu:
publicQLabel
{
friendclassmouseLabel;
Q_OBJECT
private:
QPixmapPixmapBg;
QPixmapPixmapSw;
QLabel*ImageBgLabel;
QLabel*ImageSwLabel;
//QLabel*TextBgLabel;
mouseLabel*TextSwLabel;
intSliderId;
intxPos;
intyPos;
intState;
public:
MySliderMenu(QWidget*parent,intx,inty,QStringimageBgPath,QStringimageSwPath,intnSliderID);
voidsetPosition(intx,inty);
voidmoveImageSw(intpos);
voidmoveImageSwLittle(intpos);
voidmousePressEvent(QMouseEvent*e);
voidmouseReleaseEvent(QMouseEvent*e);
voidmouseMoveEvent(QMouseEvent*ev);
intgetSliderID();
intgetState();
};
//externMySliderMenuMySlider;
#defineMENU_ON1
#defineMENU_OFF2
#endif//MYSLIDER_H
-----------------------------------------------
mouselabel.cpp
#include"mouselabel.h"
#include"myslider.h"
mouseLabel:
:
mouseLabel(QWidget*parent,MySliderMenu*focusLabel):
QLabel(parent)
{
FocusLabel=focusLabel;
releaseFlag=true;
}
voidmouseLabel:
:
mousePressEvent(QMouseEvent*e)
{
}
voidmouseLabel:
:
mouseReleaseEvent(QMouseEvent*e)
{
releaseFlag=true;
if(FocusLabel->getState()==MENU_OFF)
{
FocusLabel->moveImageSw(MENU_OFF);
}else
{
FocusLabel->moveImageSw(MENU_ON);
}
}
voidmouseLabel:
:
mouseMoveEvent(QMouseEvent*ev)
{
staticQPointFirstPoint,LastPoint;
if(releaseFlag)
{
FirstPoint=QCursor:
:
pos();
releaseFlag=false;
}
LastPoint=QCursor:
:
pos();//获取当前光标的位置
if(LastPoint.x()-FirstPoint.x()>10)
{
if(FocusLabel->getState()==MENU_ON)
{
FocusLabel->moveImageSwLittle(MENU_OFF);
}
}
if(FirstPoint.x()-LastPoint.x()>10)
{
if(FocusLabel->getState()==MENU_OFF)
{
FocusLabel->moveImageSwLittle(MENU_ON);
}
}
if(LastPoint.x()-FirstPoint.x()>80)
{
if(FocusLabel->getState()==MENU_ON)
{
FocusLabel->moveImageSw(MENU_OFF);
}
}
if(FirstPoint.x()-LastPoint.x()>80)
{
if(FocusLabel->getState()==MENU_OFF)
{
FocusLabel->moveImageSw(MENU_ON);
}
}
//printf("x:
%d,Y:
%d\n",LastPoint.x(),LastPoint.y());
}
----------------------------------------------------
myslider.cpp
#include"myslider.h"
#include
#include"main.h"
#include"language.h"
#include"mouselabel.h"
//为做设置界面里一个漂亮滑块开关而写的类。
。
。
MySliderMenu:
:
MySliderMenu(QWidget*parent,intx,inty,QStringimageBgPath,QStringimageSwPath,intnSliderID)
:
QLabel(parent)
{
PixmapBg.load(imageBgPath);
PixmapSw.load(imageSwPath);
ImageBgLabel=newQLabel(parent);
this->raise();
ImageSwLabel=newQLabel(parent);
//TextBgLabel=newQLabel(parent);
TextSwLabel=newmouseLabel(parent,this);
ImageBgLabel->setPixmap(PixmapBg);
ImageSwLabel->setPixmap(PixmapSw);
ImageBgLabel->setGeometry(x,y,PixmapBg.rect().width(),PixmapBg.rect().height());
QPalettepal;
pal.setColor(QPalette:
:
WindowText,Qt:
:
white);
QStringstr1="",str2="";
str1+=Str_On;
str2+=Str_Off;
this->setText(QObject:
:
tr((str1+str2).toLocal8Bit().data()));
//this->installEventFilter(this);
this->setPalette(pal);
this->setGeometry(x,y,PixmapBg.rect().width(),PixmapBg.rect().height());
ImageSwLabel->setGeometry(x-1,y-1,PixmapSw.rect().width(),PixmapSw.rect().height());
TextSwLabel->setGeometry(x-1,y-1,PixmapSw.rect().width(),PixmapSw.rect().height());
TextSwLabel->setText(QObject:
:
tr(Str_On));
TextSwLabel->setAlignment(Qt:
:
AlignCenter);
TextSwLabel->setPalette(pal);
//setMouseTracking(true);//这是激活整个窗体的鼠标追踪
//gpFrameMenu4->setMouseTracking(true);//这是激活整个窗体的鼠标追踪
//this->installEventFilter(this);
xPos=x;
yPos=y;
State=MENU_ON;
SliderId=nSliderID;
}
voidMySliderMenu:
:
setPosition(intx,inty)
{
this->setGeometry(x,y,PixmapBg.rect().width(),PixmapBg.rect().height());
ImageSwLabel->setGeometry(x-1,y-1,PixmapSw.rect().width(),PixmapSw.rect().height());
}
voidMySliderMenu:
:
moveImageSw(intpos)
{
if(pos==MENU_ON)
{
ImageSwLabel->setGeometry(xPos-1,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
TextSwLabel->setText(QObject:
:
tr(Str_On));
TextSwLabel->setGeometry(xPos-1,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
State=MENU_ON;
}else
if(pos==MENU_OFF)
{
ImageSwLabel->setGeometry(xPos+PixmapBg.rect().width()/2,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
TextSwLabel->setText(QObject:
:
tr(Str_Off));
TextSwLabel->setGeometry(xPos+PixmapBg.rect().width()/2,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
State=MENU_OFF;
}
}
voidMySliderMenu:
:
moveImageSwLittle(intpos)
{
if(pos==MENU_ON)
{
ImageSwLabel->setGeometry(xPos+PixmapBg.rect().width()/2-10,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
TextSwLabel->setText(QObject:
:
tr(Str_Off));
TextSwLabel->setGeometry(xPos+PixmapBg.rect().width()/2-10,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
}else
if(pos==MENU_OFF)
{
ImageSwLabel->setGeometry(xPos+9,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
TextSwLabel->setText(QObject:
:
tr(Str_On));
TextSwLabel->setGeometry(xPos+9,yPos-1,PixmapSw.rect().width(),PixmapSw.rect().height());
}
}
intMySliderMenu:
:
getSliderID()
{
returnSliderId;
}
intMySliderMenu:
:
getState()
{
returnState;
}
voidMySliderMenu:
:
mousePressEvent(QMouseEvent*e)
{
if(State==MENU_OFF)
{
moveImageSw(MENU_ON);
}else
{
moveImageSw(MENU_OFF);
}
//printf("press\n");
}
voidMySliderMenu:
:
mouseReleaseEvent(QMouseEvent*e)
{
/*if(State==2)
{
moveImageSw
(1);
}else
{
moveImageSw
(2);
}
*/
//printf("release\n");
}
voidMySliderMenu:
:
mouseMoveEvent(QMouseEvent*ev)
{
/*if(State==2)
{
moveImageSw
(1);
}else
{
moveImageSw
(2);
}*/
//printf("mouve\n");
}
使用的时候
nX=0;//位置坐标
nY=0;
defImagePath="slider_bg.png";//背景及开关图片
actImagePath="slider.png";
MySliderMenu*pMySlider=newMySliderMenu(pFrameMenu,nX,nY,defImagePath,actImagePath,0);
其中pFrameMenu是父窗口
效果:
方法二:
#ifndefSWITCHMENU_H
#defineSWITCHMENU_H
#include
classSwitchMenu:
publicQWidget
{
Q_OBJECT
public:
explicitSwitchMenu(QWidget*parent=0);
protected:
virtualvoidpaintEvent(QPaintEvent*);
virtualvoidmousePressEvent(QMouseEvent*);
virtualvoidmouseReleaseEvent(QMouseEvent*);
signals:
publicslots:
public:
voidinitialize(constQString&,constQString&);
private:
QPixmapm_pixmapBk;
QPixmapm_pixmapFore;
boolm_bOn;
boolm_bLBtnDown;
};
#endif//SWITCHMENU_H
#include"switchmenu.h"
#include
#include
SwitchMenu:
:
SwitchMenu(QWidget*parent):
QWidget(parent)
{
setWindowFlags(Qt:
:
FramelessWindowHint);
m_bOn=false;
m_bLBtnDown=false;
}
voidSwitchMenu:
:
paintEvent(QPaintEvent*)
{
QPainterpainter(this);
painter.drawPixmap(QPoint(0,0),m_pixmapBk);
QPointptFore;
if(m_bOn){
ptFore=rect().topLeft();
}else{
ptFore=QPoint(this->width()-m_pixmapFore.width(),
this->rect().top());
}
painter.drawPixmap(ptFore,m_pixmapFore);
QRectrcOn;
rcOn.setTopLeft(rect().topLeft());
rcOn.setBottomRight(QPoint(m_pixmapFore.width(),m_pixmapFore.height()));
painter.drawText(rcOn,Qt:
:
AlignCenter,"ON");
QRectrcOff;
rcOff.setTopLeft(QPoint(rect().width()-m_pixmapFore.width(),rect().top()));
rcOff.setBottomRight(rect().bottomRight());
painter.drawText(rcOff,Qt:
:
AlignCenter,"Off");
}
voidSwitchMenu:
:
mousePressEvent(QMouseEvent*)
{
m_bLBtnDown=true;
}
voidSwitchMenu:
:
mouseReleaseEvent(QMouseEvent*e)
{
if(m_bLBtnDown){
m_bOn=!
m_bOn;
m_bLBtnDown=false;
}
update();
}
voidSwitchMenu:
:
initialize(constQString&strImageBk,constQString&strImageFore)
{
m_pixmapBk.load(strImageBk);
m_pixmapFore.load(strImageFore);
intnWidth=m_pixmapBk.width();
intnHeight=m_pixmapBk.height();
setGeometry(0,0,m_pixmapBk.width(),m_pixmapBk.height());
}
使用方法:
SwitchMenuswitchMenu;
switchMenu.initialize(":
/res/switchMenu_bk",":
/res/switchMenu_fore");
switchMenu.show();
这样就可以了。
。
。