QT 滑动按钮的实现.docx

上传人:b****6 文档编号:3870511 上传时间:2022-11-26 格式:DOCX 页数:14 大小:24.75KB
下载 相关 举报
QT 滑动按钮的实现.docx_第1页
第1页 / 共14页
QT 滑动按钮的实现.docx_第2页
第2页 / 共14页
QT 滑动按钮的实现.docx_第3页
第3页 / 共14页
QT 滑动按钮的实现.docx_第4页
第4页 / 共14页
QT 滑动按钮的实现.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

QT 滑动按钮的实现.docx

《QT 滑动按钮的实现.docx》由会员分享,可在线阅读,更多相关《QT 滑动按钮的实现.docx(14页珍藏版)》请在冰豆网上搜索。

QT 滑动按钮的实现.docx

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();

这样就可以了。

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高中教育 > 语文

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1