您现在的位置: 爱51代码网 >> 范文 >> 文章正文
QT如何实现左右滑动的按钮

QT如何实现左右滑动的按钮
我要实现这样的功能,就想Iphone一样,实现开关按钮,右滑关闭,左滑打开,这样的功能在QT下怎么实现?
望各位大神赐教。

自己写代码,我写过一个,贴图上去,读取触摸位置,移动图片,想怎么滑就怎么滑。

mouselabel.h   #ifndef MOUSELABEL_H #define MOUSELABEL_H #include "myslider.h" class MySliderMenu;   //为做设置界面里一个漂亮滑块而写的类。。。 class mouseLabel : public QLabel {     friend class MySliderMenu;     Q_OBJECT private:     QString ImageBgPath;     QString ImageSwPath;     MySliderMenu *FocusLabel;     bool releaseFlag; public:       mouseLabel(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 MySliderMenu :public QLabel {     friend class mouseLabel;     Q_OBJECT private:     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 imageBgPath, 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 MySliderMenu 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) { } void 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::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 <unistd.h> #include "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->raise();     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(QPalette::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,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; } 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(),PixmapSw.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 = 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 ;     } } 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-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 MySliderMenu::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(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 MySliderMenu(pFrameMenu,nX,nY,defImagePath,actImagePath,0);

其中pFrameMenu 是父窗口

  • 上一篇文章:

  • 下一篇文章: 没有了
  • 最新文章 热点文章 相关文章
    mysql主从同步延迟方案解决的学习
    青岛科学六年级下册教材分析
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    青少年吸烟心理探析
    保险受益人制度相关问题的探讨
    mysql主从同步延迟方案解决的学习
    生日旅行总结
    中小板生日快乐随感
    送生日快乐桑葚乳酪小蛋糕
    写给女儿的生日快乐
    总分公司财务核算
    恢复使用繁体字可行性研究报告
    保险受益人制度相关问题的探讨
    初中生地理读图能力培养的研究
    搞笑生日祝福
    case expressions must be c
     



    设为首页 | 加入收藏 | 网站地图 | 友情链接 |