Chinaunix首页 | 论坛 | 博客
  • 博客访问: 127022
  • 博文数量: 30
  • 博客积分: 972
  • 博客等级: 中士
  • 技术积分: 332
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-08 10:04
文章分类

全部博文(30)

文章存档

2012年(30)

分类: LINUX

2012-07-31 08:30:35


点击(此处)折叠或打开

  1. /***************moveico.h************/
  2. #ifndef MOVEICO_H
  3. #define MOVEICO_H

  4. #include <QWidget>
  5. #include <QLabel>

  6. namespace Ui {
  7.     class MoveIco;
  8. }

  9. class MoveIco : public QWidget
  10. {
  11.     Q_OBJECT

  12. public:
  13.     explicit MoveIco(QWidget *parent = 0);
  14.     ~MoveIco();

  15. private:
  16.     Ui::MoveIco *ui;
  17.     QLabel *label;
  18.     int x,y;
  19.     int w,h;
  20. protected:
  21.     void mousePressEvent(QMouseEvent *);
  22.     void paintEvent(QPaintEvent *);
  23.     void keyPressEvent(QKeyEvent *);
  24.     void wheelEvent(QWheelEvent *);
  25.     void resizeEvent(QResizeEvent *);
  26. };

  27. #endif // MOVEICO_H


点击(此处)折叠或打开

  1. /************moveico.cpp*************/
  2. #include "moveico.h"
  3. #include "ui_moveico.h"
  4. #include <QMouseEvent>
  5. #include <QDebug>

  6. MoveIco::MoveIco(QWidget *parent) :
  7.     QWidget(parent),
  8.     ui(new Ui::MoveIco)
  9. {
  10.     x = y =0;
  11.     w = h =50;
  12.     label = new QLabel(this);
  13.     label->setScaledContents(true);//设置图片自适应
  14.     label->setPixmap(QPixmap("../image/qt2.png"));//插入图片
  15.     ui->setupUi(this);
  16. }

  17. MoveIco::~MoveIco()
  18. {
  19.     delete ui;
  20. }
  21. void MoveIco::mousePressEvent(QMouseEvent *e)//鼠标点击事件
  22. {
  23.     switch(e->button())//判断键值
  24.     {
  25.         case Qt::LeftButton:
  26.              if(this->label->pos().x() > 0)//左边界限定
  27.              x -=10;
  28.              break;
  29.         case Qt::RightButton:
  30.              if(this->label->pos().x() < this->rect().width() - this->label->width())//右边界限定
  31.              x+=10;
  32.              break;
  33.     }
  34. }

  35. void MoveIco::paintEvent(QPaintEvent *e)//绘图事件
  36. {
  37.     this->label->move(0+x,0+y);
  38.     this->label->resize(w,h);
  39.     update();
  40. }

  41. void MoveIco::keyPressEvent(QKeyEvent *e)//键盘按键事件
  42. {
  43.     int flag = 1;
  44.     switch(e->modifiers())//判断是否有辅助键按下
  45.     {
  46.     case Qt::AltModifier:
  47.         flag=2;
  48.         break;
  49.     default:
  50.         break;
  51.     }
  52.     switch(e->key())//判定按下键盘的什么键
  53.     {
  54.     case Qt::Key_Left:
  55.         if(this->label->pos().x() > 0)//左边界限定
  56.         x -= 10*flag;
  57.         break;
  58.     case Qt::Key_Right:
  59.         if(this->label->pos().x() < this->rect().width() - this->label->width())//右边界限定
  60.         x += 10*flag;
  61.         break;
  62.     case Qt::Key_Up:
  63.         if(this->label->pos().y() > 0)
  64.         y -= 10*flag;
  65.         break;
  66.     case Qt::Key_Down:
  67.         if(this->label->pos().y() < this->rect().height() - this->label->height())
  68.         y += 10*flag;
  69.         break;
  70.     }
  71. }

  72. void MoveIco::wheelEvent(QWheelEvent *e)//滚轮事件
  73. {
  74.     if(e->delta() > 0)//向上滚
  75.     {
  76.         w = this->label->width()+10;
  77.         h = this->label->height()+10;
  78.     }

  79.     if(e->delta() < 0)//向下滚
  80.     {
  81.         w = this->label->width()-10;
  82.         h = this->label->height()-10;
  83.     }
  84. }

  85. void MoveIco::resizeEvent(QResizeEvent *e)//窗口大小变化事件
  86. {
  87.     if((this->label->pos().x() >= this->rect().width() - this->label->width()) || (this->label->pos().y() >= this->rect().height() - this->label->height()))
  88.     {
  89.         y = y*e->size().height()/e->oldSize().height();
  90.         x = x*e->size().width()/e->oldSize().width();
  91.         w = w*e->size().width()/e->oldSize().width(); h = h*e->size().height()/e->oldSize().height();
  92.     }
  93. }//先判断label的位置,然后在根据新旧窗口的大小比例计算新坐标。

阅读(4135) | 评论(1) | 转发(0) |
0

上一篇:有种写内核的想法

下一篇:重新上线

给主人留下些什么吧!~~

hclen906152012-07-31 08:36:42