• 8186阅读
  • 5回复

[提问]Qt 截图 eventFilter的问题 [复制链接]

上一主题 下一主题
离线weiweiqiao
 

只看楼主 倒序阅读 楼主  发表于: 2011-02-08
我想实现类似qq截图的功能。先截个全图,然后通过橡皮筋选框选择要选择的部分
但是我通过派生QLabel后,只能通过重写3个mouse方法来实现此功能,为什么通过eventFilter方法判断鼠标的输入不可以实现,下面是我的code,麻烦帮忙看下。谢谢。

头文件
  1. #ifndef SHOTLABEL_H
  2. #define SHOTLABEL_H
  3. #include <QWidget>
  4. #include <QLabel>
  5. class QPoint;
  6. class QRubberBand;
  7. class ShotLabel : public QLabel
  8. {
  9.     Q_OBJECT
  10. public:
  11.     explicit ShotLabel(QWidget *parent = 0);
  12. protected:
  13.     virtual void mousePressEvent(QMouseEvent *);
  14.     virtual void mouseMoveEvent(QMouseEvent *);
  15.     virtual void mouseReleaseEvent(QMouseEvent *);
  16.     //virtual bool eventFilter(QObject *obj, QEvent *ev);
  17. private:
  18.     QRubberBand *rubberBand;
  19.     QPoint origin;
  20. };
  21. #endif // SHOTLABEL_H


实现文件
  1. #include "shotlabel.h"
  2. #include <QtGui>
  3. ShotLabel::ShotLabel(QWidget *parent) :
  4.     QLabel(parent)
  5. {
  6.     this->installEventFilter(this);
  7.     //this->setPixmap(QPixmap(tr(":/images/jap_66.png")));
  8.     setPixmap(QPixmap::grabWindow(QApplication::desktop()->winId()));
  9.     rubberBand = 0;
  10.     showFullScreen();
  11. }
  12. void ShotLabel::mousePressEvent(QMouseEvent *e)
  13. {
  14.     if (e->button() == Qt::RightButton)
  15.     {
  16.         return;
  17.     }
  18.     origin = e->pos();
  19.     if(!rubberBand)
  20.     {
  21.         rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  22.     }
  23.     rubberBand->setGeometry(QRect(origin,QSize()));
  24.     rubberBand->show();
  25. }
  26. void ShotLabel::mouseMoveEvent(QMouseEvent *e)
  27. {
  28.     if(rubberBand)
  29.         rubberBand->setGeometry(QRect(origin,e->pos()).normalized());
  30. }
  31. void ShotLabel::mouseReleaseEvent(QMouseEvent *e)
  32. {
  33.     if(rubberBand)
  34.         rubberBand->hide();
  35. }
  36. /*
  37. bool ShotLabel::eventFilter(QObject *obj, QEvent *ev)
  38. {
  39.     if (obj != this)
  40.     {
  41.         QLabel::eventFilter(obj, ev);
  42.     }
  43.     QMouseEvent *e = static_cast<QMouseEvent*> (ev);
  44.     ShotLabel *o = static_cast<ShotLabel*> (obj);
  45.     //点击鼠标左键
  46.     if ((e->button() == Qt::LeftButton)
  47.         && (e->type() == QEvent::MouseButtonPress))
  48.     {
  49.         origin = e->pos();
  50.         if(!rubberBand)
  51.         {
  52.             rubberBand = new QRubberBand(QRubberBand::Rectangle, this);
  53.         }
  54.         rubberBand->setGeometry(QRect(origin,QSize()));
  55.         rubberBand->show();
  56.         return true;
  57.     }
  58.     //点击左键并拖拉
  59.     if ((e->button() == Qt::LeftButton)
  60.         && (e->type() == QEvent::MouseMove))
  61.     {
  62.         if(rubberBand)
  63.             rubberBand->setGeometry(QRect(origin,e->pos()).normalized());
  64.         return true;
  65.     }
  66.     //释放左键
  67.     if ((e->button() == Qt::LeftButton)
  68.         && (e->type() == QEvent::MouseButtonRelease))
  69.     {
  70.         if(rubberBand)
  71.             rubberBand->hide();
  72.         return true;
  73.     }
  74.     return false;
  75. }
  76. */

Jobs Insanely Great.
离线weiweiqiao

只看该作者 1楼 发表于: 2011-02-08
解决了。
将eventFilter方法中的
if ((e->button() == Qt::LeftButton)
        && (e->type() == QEvent::MouseMove))
改成
if  (e->type() == QEvent::MouseMove)
即可。
Jobs Insanely Great.
离线davis45
只看该作者 2楼 发表于: 2011-02-09
回 1楼(weiweiqiao) 的帖子
我试了试LZ的方法,想重新实现QRubberBand类,却老是说我没有默认的构造函数(“no appropriate default constructor available”),我重新写了一个空的构造函数,还是同样的问题,真是搞不懂哪里出问题了。
  1. #ifndef RUBBERRECT_H
  2. #define RUBBERRECT_H
  3. #include <QtGui/QRubberBand>
  4. class CRubberRect : public QRubberBand
  5. {
  6. public:
  7. explicit CRubberRect( Shape s, QWidget * p = 0 );
  8. ~CRubberRect();
  9. protected:
  10. void mouseDoubleClickEvent( QMouseEvent * event );
  11. };
  12. #endif

谢谢大家的指教
离线weiweiqiao

只看该作者 3楼 发表于: 2011-02-09
回 2楼(davis45) 的帖子
不知道对不对,你的#include中应该有QWidget的吧。。
Jobs Insanely Great.
离线davis45
只看该作者 4楼 发表于: 2011-03-10
Re:回 2楼(davis45) 的帖子
引用第3楼weiweiqiao于2011-02-09 16:50发表的 回 2楼(davis45) 的帖子 :
不知道对不对,你的#include中应该有QWidget的吧。。


肯定有啊
谢谢大家的指教
离线yjet828
只看该作者 5楼 发表于: 2011-05-11
为什么我声明bool eventFilter的时候就报错啊??格式没错 什么时候声明会出错呢
快速回复
限100 字节
 
上一个 下一个