QT如何实现左右滑动的按钮

危靖 2013-01-21 04:12:41
我要实现这样的功能,就想Iphone一样,实现开关按钮,右滑关闭,左滑打开,这样的功能在QT下怎么实现?
望各位大神赐教。
...全文
2407 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
oldmtn 2014-12-29
  • 打赏
  • 举报
回复
引用 15 楼 liuhuanm 的回复:
[quote=引用 7 楼 oldmtn 的回复:] 其实就是2张图片,加2个字符串。 你也可以从QWidget派生自己处理,那样比上面的简单多了。 我帖段代码:
#ifndef SWITCHMENU_H
#define SWITCHMENU_H

#include <QWidget>

class SwitchMenu : public QWidget
{
    Q_OBJECT
public:
    explicit SwitchMenu(QWidget *parent = 0);
    
protected:
    virtual void paintEvent(QPaintEvent *);
    virtual void mousePressEvent(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 <QPainter>
#include <QMouseEvent>

SwitchMenu::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 = 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(QPoint(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_bLBtnDown = 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();
这样就可以了。。。
为什么我运行之后图标不显示[/quote] 你有资源吗?
liuhuanm 2014-12-29
  • 打赏
  • 举报
回复
引用 7 楼 oldmtn 的回复:
其实就是2张图片,加2个字符串。 你也可以从QWidget派生自己处理,那样比上面的简单多了。 我帖段代码:
#ifndef SWITCHMENU_H
#define SWITCHMENU_H

#include <QWidget>

class SwitchMenu : public QWidget
{
    Q_OBJECT
public:
    explicit SwitchMenu(QWidget *parent = 0);
    
protected:
    virtual void paintEvent(QPaintEvent *);
    virtual void mousePressEvent(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 <QPainter>
#include <QMouseEvent>

SwitchMenu::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 = 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(QPoint(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_bLBtnDown = 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();
这样就可以了。。。
为什么我运行之后图标不显示
a65600041 2014-10-22
  • 打赏
  • 举报
回复
为什么会编译出错
luo494320570 2013-02-06
  • 打赏
  • 举报
回复
引用 8 楼 meiky 的回复:
我里面主要是加了滑动的的效果,根据手势图片跟着滑动,而且判断滑动距离不到预定距离时松开手指滑块会弹回去,细节比较多
你好,我想问下滑动这块到底怎么实现的,看的一头雾水啊。你有源码吗?
危靖 2013-01-22
  • 打赏
  • 举报
回复
拜托了大哥,我真的是不会啊,现在项目还很急,全靠你了啊。
危靖 2013-01-22
  • 打赏
  • 举报
回复
太谢谢你了,呵呵,不介意加个QQ吧,2606882663,以后方便交流,我做的比较杂,Android,linux,QT都做。
meiky 2013-01-22
  • 打赏
  • 举报
回复
使用的时候
nX = 0;//位置坐标
nY = 0;
defImagePath = "slider_bg.png";//背景及开关图片
actImagePath = "slider.png";
MySliderMenu *pMySlider = new MySliderMenu(pFrameMenu,nX,nY,defImagePath,actImagePath,0);

其中pFrameMenu 是父窗口
效果:

meiky 2013-01-22
  • 打赏
  • 举报
回复

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");
}



mlj1668956679 2013-01-22
  • 打赏
  • 举报
回复
第一个人的代码很有意思,第二个人的总结也挺不错
危靖 2013-01-22
  • 打赏
  • 举报
回复
oldmtn,能不能给我看下运行图啊,我运行了没有反应啊,加下我QQ吧
危靖 2013-01-22
  • 打赏
  • 举报
回复
oldmtn, 你好 为什么我编译的时候总报collect2:ld returned 1 exit status的错误,应该是连接的时候出错了。
危靖 2013-01-22
  • 打赏
  • 举报
回复
谢谢了,我试试看,关键是刚接触QT不久,很多都不会。
meiky 2013-01-22
  • 打赏
  • 举报
回复
我里面主要是加了滑动的的效果,根据手势图片跟着滑动,而且判断滑动距离不到预定距离时松开手指滑块会弹回去,细节比较多
oldmtn 2013-01-22
  • 打赏
  • 举报
回复
其实就是2张图片,加2个字符串。 你也可以从QWidget派生自己处理,那样比上面的简单多了。 我帖段代码:
#ifndef SWITCHMENU_H
#define SWITCHMENU_H

#include <QWidget>

class SwitchMenu : public QWidget
{
    Q_OBJECT
public:
    explicit SwitchMenu(QWidget *parent = 0);
    
protected:
    virtual void paintEvent(QPaintEvent *);
    virtual void mousePressEvent(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 <QPainter>
#include <QMouseEvent>

SwitchMenu::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 = 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(QPoint(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_bLBtnDown = 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();
这样就可以了。。。
危靖 2013-01-21
  • 打赏
  • 举报
回复
我是QT新手,真的不知道怎么做,可以把关键代码贴出来让我看看么,如果有例子的话就更好了,非常感谢你。我的邮箱leisure_wj@163.com
meiky 2013-01-21
  • 打赏
  • 举报
回复
自己写代码,我写过一个,贴图上去,读取触摸位置,移动图片,想怎么滑就怎么滑。

16,215

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧