Qt实现可拖动按钮

 更新时间:2020年08月20日 13:23:23   作者:BIG_C_GOD  
这篇文章主要为大家详细介绍了Qt实现可拖动按钮,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了Qt实现可拖动按钮的具体代码,供大家参考,具体内容如下

直接上代码

self-contained.h

#ifndef SELFCONTAINED_H
#define SELFCONTAINED_H

#include <QWidget>
#include <QPainter>
#include <QTimer>
#include <QImage>
#include <QMouseEvent>
#include <QVector>

#endif // SELFCONTAINED_H

按钮控件

drawbutton.h:

#ifndef DRAGBUTTON_H
#define DRAGBUTTON_H

#include "self-contained.h"

class DragButton : public QWidget
{
  Q_OBJECT
public:
  DragButton(QWidget *parent = 0);

  void setInitialScaling(double scaling);
  void setPixmap(QString pixmap);
  void setText(QString str);
  void setIsHold(bool flag);
protected:
  int isPress;
  int isHold;

  QTimer *m_aniTimer;
  QTimer *m_holdTimer;

  double m_scaling;
  double m_InitialScaling;//当前缩放比例
  QPoint m_mouseSrcPos;//最小缩放比例

  QPixmap m_pixmap;
  QString m_text;

  void paintEvent(QPaintEvent *);
  void enterEvent(QEvent *);
  void leaveEvent(QEvent *);
  void mousePressEvent(QMouseEvent *);
  void mouseReleaseEvent(QMouseEvent *);
  void mouseMoveEvent(QMouseEvent *);

signals:
  void release_signal();
  void drag_signal();//拖动时发出信号
  void clicked();
public slots:
  void zoomIn();//放大
  void zoomOut();//缩小
  void hold_slot();
};

#endif // DRAGBUTTON_H

drawbutton.cpp

#include "dragbutton.h"

DragButton::DragButton(QWidget *parent) :
  QWidget(parent),isPress(0),isHold(0),m_scaling(0.5),m_InitialScaling(0.5),m_mouseSrcPos(0,0)
{
  m_aniTimer = new QTimer(this);
  m_aniTimer->setInterval(7);

  m_holdTimer = new QTimer(this);
  m_holdTimer->setInterval(1000);
  m_holdTimer->setSingleShot(true);
  connect(m_holdTimer,SIGNAL(timeout()),this,SLOT(hold_slot()));
}

void DragButton::setInitialScaling(double scaling)
{
  if(scaling <= 1 && scaling > 0)
  {
    m_InitialScaling = scaling;
    m_scaling = scaling;
  }
}

void DragButton::setPixmap(QString pixmap)
{
  m_pixmap.load(pixmap);
  update();
}

void DragButton::setText(QString str)
{
  m_text = str;
  update();
}

void DragButton::setIsHold(bool flag)
{
  isHold = flag;
  update();
}

void DragButton::paintEvent(QPaintEvent *)
{
  if(m_pixmap.isNull())
    return;

  QPainter painter(this);
  painter.setRenderHint(QPainter::Antialiasing);

  if(isPress)
  {
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(0,0,0,130));
    painter.drawRoundedRect(rect(),20,20);
  }

  m_pixmap = m_pixmap.scaled(width(),height() - 20,Qt::KeepAspectRatio,Qt::SmoothTransformation);

  int w = m_pixmap.width()*m_scaling;
  int h = m_pixmap.height()*m_scaling;
  painter.drawPixmap(QRect((width() - w)/2,(height() - h)/2 - 20,w,h),
            m_pixmap,m_pixmap.rect());

  painter.setPen(QColor(0,0,0));
  painter.drawText(QRect(0,height() - 40,width(),40),Qt::AlignCenter,m_text);
}

void DragButton::enterEvent(QEvent *)
{
  disconnect(m_aniTimer,SIGNAL(timeout()),this,SLOT(zoomOut()));
  connect(m_aniTimer,SIGNAL(timeout()),this,SLOT(zoomIn()));

  m_aniTimer->start();
}

void DragButton::leaveEvent(QEvent *)
{
  disconnect(m_aniTimer,SIGNAL(timeout()),this,SLOT(zoomIn()));
  connect(m_aniTimer,SIGNAL(timeout()),this,SLOT(zoomOut()));

  m_aniTimer->start();
}

void DragButton::mousePressEvent(QMouseEvent *e)
{
  if(!isHold)
    isPress = 1;

  m_holdTimer->start();

  m_mouseSrcPos = e->pos();

  update();
}

void DragButton::mouseReleaseEvent(QMouseEvent *e)
{
  m_holdTimer->stop();

  isPress = 0;
  isHold = 0;

  if(rect().contains(e->pos()))
    emit clicked();

  emit release_signal();

  update();
}

void DragButton::mouseMoveEvent(QMouseEvent *e)
{
  if(isHold)
  {
    move(pos() - m_mouseSrcPos + e->pos());
    emit drag_signal();
  }
  else
    m_mouseSrcPos = e->pos();
}

void DragButton::zoomIn()
{
  m_scaling += 0.01;
  if(m_scaling >= 1)
  {
    m_scaling = 1;
    m_aniTimer->stop();
  }
  update();
}

void DragButton::zoomOut()
{
  m_scaling -= 0.01;
  if(m_scaling <= m_InitialScaling)
  {
    m_scaling = m_InitialScaling;
    m_aniTimer->stop();
  }
  update();
}

void DragButton::hold_slot()
{
  isHold = 1;
  isPress = 0;
  m_aniTimer->stop();
  m_scaling = m_InitialScaling;

  update();
}

整合按钮的控件

drawwidget.h

#include "dragbutton.h"

class DragWidget : public QWidget
{
  Q_OBJECT

public:
  DragWidget(QWidget *parent = 0);
  ~DragWidget();

  void addButton(DragButton*);

protected:
  QVector<DragButton*> BtnVector;
  QPoint m_mouseSrcPos;//记录坐标点

  void resetInterface();//复位

signals:

public slots:
  void BtnMove_slots();
  void BtnRelease_slots();
};

#endif // DRAGWIDGET_H

drawwidget.cpp

#include "dragwidget.h"

DragWidget::DragWidget(QWidget *parent)
  : QWidget(parent),m_mouseSrcPos(0,0)
{
}

DragWidget::~DragWidget()
{

}

void DragWidget::addButton(DragButton* btn)
{
  connect(btn,SIGNAL(drag_signal()),this,SLOT(BtnMove_slots()));
  connect(btn,SIGNAL(release_signal()),this,SLOT(BtnRelease_slots()));


  BtnVector.push_back(btn);
  btn->show();
  resetInterface();
}

void DragWidget::resetInterface()
{
  for(int i = 0;i < BtnVector.length();++i)
  {
    BtnVector[i]->setGeometry(i * width()/BtnVector.length(),0,width()/BtnVector.length(),height());
  }
}

void DragWidget::BtnMove_slots()
{
  for(int i = 0;i < BtnVector.length();++i)//找到鼠标所在的按钮
    if(BtnVector[i] == sender())
    {
      int flag = (BtnVector[i]->pos().x() + BtnVector[i]->width()/2)/(width()/BtnVector.length());

      for(int l = 0;l < BtnVector.length();++l)//这里也可以做动画,但这次主要实现拖动的功能
      {
        if(l < i && l <flag)
          BtnVector[l]->setGeometry(l * width()/BtnVector.length(),0,width()/BtnVector.length(),height());
        else if((l > i && l <= flag)||(l >= flag && l < i))
          BtnVector[l]->setGeometry((l + ((i-flag)>0?1:-1))* width()/BtnVector.length(),0,width()/BtnVector.length(),height());
        else if(l > flag && l > i)
          BtnVector[l]->setGeometry(l * width()/BtnVector.length(),0,width()/BtnVector.length(),height());
      }
      //注释部分合为上面的循环
//      if(flag >= i)//往后拖
//        for(int l = 0;l < BtnVector.length();++l)
//        {
//          if(l < i)
//            BtnVector[l]->setGeometry(l * width()/BtnVector.length(),0,width()/BtnVector.length(),height());
//          else if(l > i && l <= flag)
//            BtnVector[l]->setGeometry((l - 1)* width()/BtnVector.length(),0,width()/BtnVector.length(),height());
//          else if(l > flag)
//            BtnVector[l]->setGeometry(l * width()/BtnVector.length(),0,width()/BtnVector.length(),height());
//        }
//      else if(flag < i)//往前拖
//        for(int l = 0;l < BtnVector.length();++l)
//        {
//          if(l < flag)
//            BtnVector[l]->setGeometry(l * width()/BtnVector.length(),0,width()/BtnVector.length(),height());
//          else if(l >= flag && l < i)
//            BtnVector[l]->setGeometry((l + 1)* width()/BtnVector.length(),0,width()/BtnVector.length(),height());
//          else if(l > i)
//            BtnVector[l]->setGeometry(l * width()/BtnVector.length(),0,width()/BtnVector.length(),height());
//        }
      break;
    }
}

void DragWidget::BtnRelease_slots()
{
  for(int i = 0;i < BtnVector.length();++i)//找到鼠标所在的按钮
    if(BtnVector[i] == sender())
    {
      int posX = BtnVector[i]->pos().x();
      if(posX < 0)
        posX = 0;
      else if(posX > width())
        posX = width();

      int flag = (posX+BtnVector[i]->width()/2)/(width()/BtnVector.length());

      DragButton *btn = BtnVector[i];//修改vector顺序
      if(flag >= i)
        for(int l = i;l < flag;++l)
          BtnVector[l] = BtnVector[l+1];
      else
        for(int l = i;l > flag;--l)
          BtnVector[l] = BtnVector[l-1];

      BtnVector[flag] = btn;
    }

  resetInterface();//复位
}

使用

main.cpp

#include "dragwidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
  QApplication a(argc, argv);

  DragWidget ww;
  ww.setGeometry(200,200,800,200);

  DragButton w(&ww);
  w.setPixmap(":/image/image/contacts.png");
  w.setText("按钮1");
  w.setInitialScaling(0.6);

  DragButton w2(&ww);
  w2.setPixmap(":/image/image/time.png");
  w2.setText("按钮2");
  w2.setInitialScaling(0.6);

  DragButton w3(&ww);
  w3.setPixmap(":/image/image/checking.png");
  w3.setText("按钮3");
  w3.setInitialScaling(0.6);

  DragButton w4(&ww);
  w4.setPixmap(":/image/image/suitcase.png");
  w4.setText("按钮4");
  w4.setInitialScaling(0.6);

  ww.addButton(&w);
  ww.addButton(&w2);
  ww.addButton(&w3);
  ww.addButton(&w4);
  ww.show();

  return a.exec();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

相关文章

  • 10个步骤Opencv轻松检测出图片中条形码

    10个步骤Opencv轻松检测出图片中条形码

    这篇文章主要为大家详细介绍了Opencv轻松检测出图片中条形码的10个步骤,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • 带你从头学习C++的封装

    带你从头学习C++的封装

    这篇文章主要为大家从头学习了C++的封装,使用数据库,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • C++ 将一个文件读入数组再读出数组的方法

    C++ 将一个文件读入数组再读出数组的方法

    今天小编就为大家分享一篇C++ 将一个文件读入数组再读出数组的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • C++类中变量也可以是引用的代码实例

    C++类中变量也可以是引用的代码实例

    今天小编就为大家分享一篇关于C++类中变量也可以是引用的代码实例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-04-04
  • C++ 递归遍历文件并计算MD5的实例代码

    C++ 递归遍历文件并计算MD5的实例代码

    在本篇文章里小编给大家整理的是一篇关于C++ 递归遍历文件并计算MD5的实例代码,有兴趣的朋友们可以学习参考下。
    2021-07-07
  • c语言 两字符串交叉合并实例

    c语言 两字符串交叉合并实例

    今天小编就为大家分享一篇c语言 两字符串交叉合并实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2019-12-12
  • 基于C++编写一个密码系统

    基于C++编写一个密码系统

    这篇文章主要为大家详细介绍了如何基于C++编写一个简单的密码系统,文中的示例代码讲解详细,具有一定的借鉴价值,感兴趣的小伙伴可以跟随小编一起学习一下
    2023-11-11
  • C++ 路径中./、../、/代表的含义

    C++ 路径中./、../、/代表的含义

    这篇文章主要介绍了C++ 路径中./、../、/代表的含义,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-12-12
  • 利用C语言实现页面置换算法的详细过程

    利用C语言实现页面置换算法的详细过程

    一个好的页面置换算法,应具有较低的页面更换频率,从理论上讲,应该保留最近重复访问的页面,将以后都不再访问或者很长时间内不再访问的页面调出,下面这篇文章主要给大家介绍了关于利用C语言实现页面置换算法的相关资料,需要的朋友可以参考下
    2022-11-11
  • C++中求旋转数组中的最小数字(经典面试题)

    C++中求旋转数组中的最小数字(经典面试题)

    这篇文章主要介绍了C++中求旋转数组中的最小数字(经典面试题)的相关资料,需要的朋友可以参考下
    2017-03-03

最新评论