• 6881阅读
  • 2回复

[提问]mouseMoveEvent如何用阿? [复制链接]

上一主题 下一主题
离线qnewert
 
只看楼主 正序阅读 楼主  发表于: 2012-05-19
各位好,今天学习mouseevent的重载,结果貌似很悲剧。就是不成功。代码如下:
#include"shape-widget.h"
#include<QPixmap>
#include<QBitmap>
#include<QPainter>

ShapeWidget::ShapeWidget(QWidget *parent)
    :QWidget(parent, Qt::FramelessWindowHint)
{
    QPixmap pix;

    pix.load("../image/james.png", 0, Qt::AvoidDither|Qt::ThresholdDither|Qt::ThresholdAlphaDither);
    pix=pix.scaled(300, 350);
    resize(pix.size());
    setMask(pix.mask());
    this->setMouseTracking(true);
}

void ShapeWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
    dragPosition=event->globalPos()-frameGeometry().topLeft();
    event->accept();
    }

    if(event->button()==Qt::RightButton)
    {
    close();
    }
}

void ShapeWidget::mouseMoveEvent(QMouseEvent *event)
{
    if(event->button()&Qt::LeftButton)
    {
    move(event->globalPos()-dragPosition);
    event->accept();
    }
}

void ShapeWidget::paintEvent(QPaintEvent *)
{
    QPixmap pix;
    pix.load("../image/james.png");
    pix=pix.scaled(300, 350);

    QPainter painter(this);
    painter.drawPixmap(0, 0, pix);
}

结果显示如下:

上面重载的移动事件不起作用,各位高手有没啥建议?
qt,第一眼见到你,就深深的爱上你了。
离线yfx2003

只看该作者 2楼 发表于: 2012-05-20
   很不错的有始有终

赞一个
www.qtcn.org/gpq4/
ajaxref.okajax.com/
离线qnewert
只看该作者 1楼 发表于: 2012-05-19
我知道错误了。
上面的event->button()要改成event->buttons();
QMouseEvent::button()和QMouseEvent::buttons()两个函数的意思不一样。

QMouseEvent::button()
Returns the button that caused the event.
Note that the returned value is always Qt::NoButton for mouse move events.
上面这句话指明了,当为move事件时,总是返回NoButton。


Qt::MouseButtons QMouseEvent::buttons () const
Returns the button state when the event was generated. The button state is a combination of Qt::LeftButton, Qt::RightButton, Qt::MidButton using the OR operator. For mouse move events, this is all buttons that are pressed down. For mouse press and double click events this includes the button that caused the event. For mouse release events this excludes the button that caused the event.



qt,第一眼见到你,就深深的爱上你了。
快速回复
限100 字节
 
上一个 下一个