16,215
社区成员
发帖
与我相关
我的任务
分享
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");
}
#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();
这样就可以了。。。