1、QT 滑动按钮的实现方法一:mouselabel.h #ifndef MOUSELABEL_H#define MOUSELABEL_H#include myslider.hclass MySliderMenu; /为做设置界面里一个漂亮滑块而写的类。class mouseLabel : public QLabel friend class MySliderMenu; Q_OBJECTprivate: QString ImageBgPath; QString ImageSwPath; MySliderMenu *FocusLabel; bool releaseFlag;public: mouse
2、Label(QWidget *parent,MySliderMenu *focusLabel); void mousePressEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *ev); void setTwoImage(QString ImageBg,QString ImageSw); #endif / MOUSELABEL_H- myslider.h class mouseLabel;/为做设置界面里一个漂亮滑块而写的类。class MySlider
3、Menu :public QLabel friend class mouseLabel; Q_OBJECTprivate: QPixmap PixmapBg; QPixmap PixmapSw; QLabel *ImageBgLabel; QLabel *ImageSwLabel; / QLabel *TextBgLabel; mouseLabel *TextSwLabel; int SliderId; int xPos; int yPos; int State;public: MySliderMenu(QWidget * parent,int x, int y, QString imageB
4、gPath, QString imageSwPath,int nSliderID); void setPosition(int x,int y); void moveImageSw(int pos); void moveImageSwLittle(int pos); void mousePressEvent(QMouseEvent *e); void mouseReleaseEvent(QMouseEvent *e); void mouseMoveEvent(QMouseEvent *ev); int getSliderID(); int getState();/extern MySlider
5、Menu MySlider; #define MENU_ON 1#define MENU_OFF 2 #endif / MYSLIDER_H-mouselabel.cpp #include mouselabel.h#include myslider.h mouseLabel:mouseLabel(QWidget *parent,MySliderMenu *focusLabel):QLabel(parent) FocusLabel = focusLabel; releaseFlag = true; void mouseLabel:mousePressEvent(QMouseEvent *e)vo
6、id mouseLabel:mouseReleaseEvent(QMouseEvent *e) releaseFlag = true; if(FocusLabel-getState() = MENU_OFF) FocusLabel-moveImageSw(MENU_OFF); else FocusLabel-moveImageSw(MENU_ON); void mouseLabel:mouseMoveEvent(QMouseEvent *ev) static QPoint FirstPoint,LastPoint; if(releaseFlag) FirstPoint = QCursor:po
7、s(); 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(LastPo
8、int.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:%dn,LastPoint.x(),LastPoint.y();-myslider.cpp#include myslider.h#include #inc
9、lude main.h#include language.h#include mouselabel.h /为做设置界面里一个漂亮滑块开关而写的类。MySliderMenu:MySliderMenu(QWidget * parent,int x, int y, QString imageBgPath, QString imageSwPath,int nSliderID) :QLabel(parent) PixmapBg.load(imageBgPath); PixmapSw.load(imageSwPath); ImageBgLabel = new QLabel(parent); this-ra
10、ise(); ImageSwLabel = new QLabel(parent); /TextBgLabel = new QLabel(parent); TextSwLabel = new mouseLabel(parent,this); ImageBgLabel-setPixmap(PixmapBg); ImageSwLabel-setPixmap(PixmapSw); ImageBgLabel-setGeometry(x,y,PixmapBg.rect().width(),PixmapBg.rect().height(); QPalette pal; pal.setColor(QPalet
11、te:WindowText,Qt:white); QString str1= ,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,Pixm
12、apSw.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-setMou
13、seTracking(true); /这是激活整个窗体的鼠标追踪 /this-installEventFilter(this); xPos = x; yPos = y; State = MENU_ON; SliderId = nSliderID;void MySliderMenu:setPosition(int x,int y) this-setGeometry(x,y,PixmapBg.rect().width(),PixmapBg.rect().height(); ImageSwLabel-setGeometry(x-1,y-1,PixmapSw.rect().width(),Pixmap
14、Sw.rect().height();void MySliderMenu:moveImageSw(int pos) 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 =
15、 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(); S
16、tate =MENU_OFF ; void MySliderMenu:moveImageSwLittle(int pos) 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-
17、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(); int MyS
18、liderMenu:getSliderID() return SliderId;int MySliderMenu:getState() return State;void MySliderMenu:mousePressEvent(QMouseEvent *e) if(State = MENU_OFF) moveImageSw(MENU_ON); else moveImageSw(MENU_OFF); / printf(press n);void MySliderMenu:mouseReleaseEvent(QMouseEvent *e) /* if(State = 2) moveImageSw
19、(1); else moveImageSw(2); */ / printf(release n);void MySliderMenu: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 = new MyS
20、liderMenu(pFrameMenu,nX,nY,defImagePath,actImagePath,0);其中pFrameMenu 是父窗口 效果: 方法二:#ifndef SWITCHMENU_H#define SWITCHMENU_H #include class SwitchMenu : public QWidget Q_OBJECTpublic: explicit SwitchMenu(QWidget *parent = 0); protected: virtual void paintEvent(QPaintEvent *); virtual void mousePressEv
21、ent(QMouseEvent *); virtual void mouseReleaseEvent(QMouseEvent *); signals: public slots: public: void initialize(const QString&, const QString&); private: QPixmap m_pixmapBk; QPixmap m_pixmapFore; bool m_bOn; bool m_bLBtnDown; #endif / SWITCHMENU_H #include switchmenu.h #include #include SwitchMenu
22、:SwitchMenu(QWidget *parent) : QWidget(parent) setWindowFlags(Qt:FramelessWindowHint); m_bOn = false; m_bLBtnDown = false; void SwitchMenu:paintEvent(QPaintEvent *) QPainter painter(this); painter.drawPixmap(QPoint(0,0), m_pixmapBk); QPoint ptFore; if (m_bOn) ptFore = rect().topLeft(); else ptFore =
23、 QPoint(this-width() - m_pixmapFore.width(), this-rect().top(); painter.drawPixmap(ptFore, m_pixmapFore); QRect rcOn; rcOn.setTopLeft(rect().topLeft(); rcOn.setBottomRight(QPoint(m_pixmapFore.width(), m_pixmapFore.height(); painter.drawText(rcOn, Qt:AlignCenter, ON); QRect rcOff; rcOff.setTopLeft(QP
24、oint(rect().width() - m_pixmapFore.width(), rect().top(); rcOff.setBottomRight(rect().bottomRight(); painter.drawText(rcOff, Qt:AlignCenter, Off); void SwitchMenu:mousePressEvent(QMouseEvent *) m_bLBtnDown = true; void SwitchMenu:mouseReleaseEvent(QMouseEvent *e) if (m_bLBtnDown) m_bOn = !m_bOn; m_b
25、LBtnDown = false; update(); void SwitchMenu:initialize(const QString& strImageBk, const QString& strImageFore) m_pixmapBk.load(strImageBk); m_pixmapFore.load(strImageFore); int nWidth = m_pixmapBk.width(); int nHeight = m_pixmapBk.height(); setGeometry(0, 0, m_pixmapBk.width(), m_pixmapBk.height(); 使用方法: SwitchMenu switchMenu; switchMenu.initialize(:/res/switchMenu_bk, :/res/switchMenu_fore); switchMenu.show(); 这样就可以了。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1