• Qt实现范围滑动条SuperSlider


    .pro

     1 QT       += core gui
     2 
     3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
     4 
     5 CONFIG += c++11
     6 
     7 # The following define makes your compiler emit warnings if you use
     8 # any Qt feature that has been marked deprecated (the exact warnings
     9 # depend on your compiler). Please consult the documentation of the
    10 # deprecated API in order to know how to port your code away from it.
    11 DEFINES += QT_DEPRECATED_WARNINGS
    12 
    13 # You can also make your code fail to compile if it uses deprecated APIs.
    14 # In order to do so, uncomment the following line.
    15 # You can also select to disable deprecated APIs only up to a certain version of Qt.
    16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
    17 
    18 SOURCES += 
    19     SuperSlider.cpp 
    20     main.cpp 
    21     mainwindow.cpp
    22 
    23 HEADERS += 
    24     SuperSlider.h 
    25     mainwindow.h
    26 
    27 FORMS += 
    28     mainwindow.ui
    29 
    30 # Default rules for deployment.
    31 qnx: target.path = /tmp/$${TARGET}/bin
    32 else: unix:!android: target.path = /opt/$${TARGET}/bin
    33 !isEmpty(target.path): INSTALLS += target
    View Code

    main.cpp

     1 #include "mainwindow.h"
     2 
     3 #include <QApplication>
     4 
     5 int main(int argc, char *argv[])
     6 {
     7     QApplication a(argc, argv);
     8     MainWindow w;
     9     w.show();
    10     return a.exec();
    11 }
    View Code

    mainwindow.h

     1 #ifndef MAINWINDOW_H
     2 #define MAINWINDOW_H
     3 
     4 #include <QMainWindow>
     5 
     6 #include "SuperSlider.h"
     7 
     8 QT_BEGIN_NAMESPACE
     9 namespace Ui { class MainWindow; }
    10 QT_END_NAMESPACE
    11 
    12 class MainWindow : public QMainWindow
    13 {
    14     Q_OBJECT
    15 
    16 public:
    17     MainWindow(QWidget *parent = nullptr);
    18     ~MainWindow();
    19 
    20 private:
    21     Ui::MainWindow *ui;
    22 };
    23 #endif // MAINWINDOW_H
    View Code

    mainwindow.cpp

     1 #include "mainwindow.h"
     2 #include "ui_mainwindow.h"
     3 
     4 MainWindow::MainWindow(QWidget *parent)
     5     : QMainWindow(parent)
     6     , ui(new Ui::MainWindow)
     7 {
     8     ui->setupUi(this);
     9 
    10     SuperSlider *osuperslider = new SuperSlider(this);
    11     osuperslider->setGeometry(100, 100, 200, 30);
    12 }
    13 
    14 MainWindow::~MainWindow()
    15 {
    16     delete ui;
    17 }
    View Code
    SuperSlider.h
     1 #ifndef SUPERSLIDER_H
     2 #define SUPERSLIDER_H
     3 
     4 #pragma once
     5 
     6 #include "qslider.h"
     7 #include "qlabel.h"
     8 
     9 
    10 /*
    11 *  Super sick nasty awesome double handled slider!
    12 *
    13 *   @author Steve
    14 */
    15 class SuperSliderHandle;
    16 
    17 class SuperSlider: public QSlider
    18 {
    19   Q_OBJECT
    20 public:
    21   SuperSlider(QWidget *parent = 0);
    22   SuperSliderHandle *alt_handle;
    23   void mouseReleaseEvent(QMouseEvent *event);
    24   int alt_value();
    25   void alt_setValue(int value);
    26   void Reset();
    27   void alt_update();
    28 signals:
    29   void alt_valueChanged(int);
    30 };
    31 
    32 class SuperEventFilter : public QObject
    33 {
    34 public:
    35   SuperEventFilter(SuperSlider *_grandParent)
    36   {grandParent = _grandParent;};
    37 
    38 protected:
    39   bool eventFilter(QObject* obj, QEvent* event);
    40 
    41 private:
    42   SuperSlider *grandParent;
    43 };
    44 
    45 class SuperSliderHandle: public QLabel
    46 {
    47   Q_OBJECT
    48 public:
    49     SuperSliderHandle(SuperSlider *parent = 0);
    50     void mousePressEvent(QMouseEvent *event);
    51     int value();
    52     int mapValue();
    53     SuperSlider *parent;
    54     bool handleActivated;
    55 private:
    56   SuperEventFilter *filter;
    57   public slots:
    58     void setValue(double value);
    59 };
    60 
    61 #endif // SUPERSLIDER_H
    View Code
    SuperSlider.cpp
      1 #include "SuperSlider.h"
      2 
      3 
      4 //Qt
      5 #include <QMouseEvent>
      6 #include "qmimedata.h"
      7 #include "qdrag.h"
      8 #include "qwidgetaction.h"
      9 #include "qapplication.h"
     10 #include "qpixmap.h"
     11 #include "qcursor.h"
     12 #include "qguiapplication.h"
     13 #include "qdir.h"
     14 #include <QProxyStyle>
     15 
     16 class SliderProxy : public QProxyStyle
     17 {
     18 public:
     19   int pixelMetric ( PixelMetric metric, const QStyleOption * option = 0, const QWidget * widget = 0 ) const
     20   {
     21     switch(metric) {
     22     case PM_SliderThickness  : return 25;
     23     case PM_SliderLength     : return 25;
     24     default                  : return (QProxyStyle::pixelMetric(metric,option,widget));
     25     }
     26   }
     27 };
     28 
     29 SuperSlider::SuperSlider(QWidget *parent)
     30   : QSlider(parent)
     31 {
     32   //styling
     33   setOrientation(Qt::Horizontal);
     34   setAcceptDrops(true);
     35   SliderProxy *aSliderProxy = new SliderProxy();
     36 
     37   //hard coded path to image :/ sorry
     38   QString path = "d:/1.png";// QDir::fromNativeSeparators(ImagesPath("handle.png"));
     39   setStyleSheet("QSlider::handle { image: url(" + path + "); }");
     40   setStyle(aSliderProxy);
     41 
     42   //setting up the alternate handle
     43   alt_handle = new SuperSliderHandle(this);
     44   addAction(new QWidgetAction(alt_handle));
     45   alt_handle->move(this->pos().x() + this->width()- alt_handle->width(), this->pos().y() );
     46 
     47 }
     48 
     49 SuperSliderHandle::SuperSliderHandle(SuperSlider *_parent)
     50   : QLabel(_parent)
     51 {
     52   parent = _parent;
     53   filter = new SuperEventFilter(parent);
     54 
     55   //styling
     56   setAcceptDrops(true);
     57   //hard coded path to image :/ sorry
     58   QPixmap pix = QPixmap("d:/2.png");//QPixmap(ImagesPath("handle.png"));
     59   pix =  pix.scaled(25, 25, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
     60   setPixmap(pix);
     61 }
     62 
     63 int SuperSlider::alt_value()
     64 {
     65   return alt_handle->value();
     66 }
     67 
     68 void SuperSlider::alt_setValue(int value)
     69 {
     70   alt_handle->setValue(value);
     71 }
     72 
     73 void SuperSlider::mouseReleaseEvent(QMouseEvent *mouseEvent)
     74 {
     75   if (mouseEvent->button() == Qt::LeftButton)
     76   {
     77     alt_handle->show();
     78     alt_handle->handleActivated = false;
     79   }
     80   mouseEvent->accept();
     81 }
     82 
     83 void SuperSlider::alt_update()
     84 {
     85   QPoint posCursor(QCursor::pos());
     86   QPoint posParent(mapToParent(mapToGlobal(pos())));
     87   QPoint point(alt_handle->mapToParent(alt_handle->mapFromGlobal(QCursor::pos())).x(),alt_handle->y());
     88   int horBuffer = (alt_handle->width());
     89   bool lessThanMax = mapToParent(point).x() < pos().x()+ width() - horBuffer;
     90   bool greaterThanMin = mapToParent(point).x() > pos().x();
     91   if(lessThanMax && greaterThanMin)
     92     alt_handle->move(point);
     93   emit alt_valueChanged(alt_value());
     94 }
     95 
     96 void SuperSliderHandle::mousePressEvent(QMouseEvent *mouseEvent)
     97 {
     98   qGuiApp->installEventFilter(filter);
     99   parent->clearFocus();
    100 }
    101 
    102 bool SuperEventFilter::eventFilter(QObject* obj, QEvent* event)
    103 {
    104   switch(event->type())
    105   {
    106   case QEvent::MouseButtonRelease:
    107     qGuiApp->removeEventFilter(this);
    108     return true;
    109     break;
    110   case QEvent::MouseMove:
    111     grandParent->alt_update();
    112     return true;
    113     break;
    114   default:
    115     return QObject::eventFilter(obj, event);
    116   }
    117   return false;
    118 }
    119 
    120 void SuperSliderHandle::setValue(double value)
    121 {
    122   double width = parent->width(), position = pos().x();
    123   double range = parent->maximum() - parent->minimum();
    124   int location = (value - parent->minimum())/range;
    125   location = location *width;
    126   move(y(),location);
    127 }
    128 
    129 int SuperSliderHandle::value()
    130 {
    131   double width = parent->width(), position = pos().x();
    132   double value = position/width;
    133   double range = parent->maximum() - parent->minimum();
    134   return parent->minimum() + (value * range);
    135 }
    136 void SuperSlider::Reset()
    137 {
    138   int horBuffer = (alt_handle->width());
    139   QPoint myPos = mapToGlobal(pos());
    140   QPoint point(myPos.x() + width() - horBuffer, myPos.y()- alt_handle->height());
    141   point = alt_handle->mapFromParent(point);
    142 
    143   alt_handle->move(point);
    144   alt_handle->show();
    145   alt_handle->raise();
    146 
    147 }
    View Code


    作者:疯狂Delphi
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

    欢迎关注我,一起进步!扫描下方二维码即可加我

  • 相关阅读:
    HTML5---offline application(application cache)
    apache asp.net
    长轮询
    comet ajax轮询
    bootstrap3
    weixin
    backbone csdn
    backbone case
    backbone showcase
    javascript performance
  • 原文地址:https://www.cnblogs.com/FKdelphi/p/14654167.html
Copyright © 2020-2023  润新知