• 5085阅读
  • 2回复

问个关于重写mouse事件的问题 [复制链接]

上一主题 下一主题
离线fox1987
 
只看楼主 倒序阅读 楼主  发表于: 2010-04-28
我想判断右键点击,
就继承了一个QWidget,
然后重写方法如下
void MyWidget::mousePressEvent(QMouseEvent* event )
{
    Qt::MouseButton buttonLeft = event->button();
    if(Qt::LeftButton==buttonLeft)
    {
        QMessageBox msg;
        msg.setWindowTitle("Left Btn");
        msg.setText("Left Btn Press");
        msg.exec();
    }
    else if(Qt::RightButton==buttonLeft)
    {
        QMessageBox msg;
        msg.setWindowTitle("Right Btn");
        msg.setText("Right Btn Press");
        msg.exec();
    }
    else
    {

    }
}
void MyWidget::mouseReleaseEvent(QMouseEvent* event)
{
    Qt::MouseButton buttonRight = event->button();
    if(Qt::RightButton==buttonRight)
    {
        QMessageBox msg;
        msg.setWindowTitle("Right Btn");
        msg.setText("Right Btn Release");
        msg.exec();
    }
    else if(Qt::LeftButton==buttonRight)
    {
        QMessageBox msg;
        msg.setWindowTitle("Left Btn");
        msg.setText("Left Btn Release");
        msg.exec();
    }
    else
    {

    }
}

但是我在面板上点击右键的时候有时候弹出一个MessageBox,有时候弹出来两个。。
(个人觉得应该是两个啊,点下不是有press然后是Release嘛。。)
离线抠涕
只看该作者 1楼 发表于: 2010-04-28
复制你的代码运行了一下, 没有问题, 都是两个阿
离线xtfllbl

只看该作者 2楼 发表于: 2010-04-28
引用楼主fox1987于2010-04-28 10:21发表的 问个关于重写mouse事件的问题 :
我想判断右键点击,
就继承了一个QWidget,
然后重写方法如下
void MyWidget::mousePressEvent(QMouseEvent* event )
{
.......


void QWidget::mousePressEvent ( QMouseEvent * event )   [virtual protected]
This event handler, for event event, can be reimplemented in a subclass to receive mouse press events for the widget.

If you create new widgets in the mousePressEvent() the mouseReleaseEvent() may not end up where you expect, depending on the underlying window system (or X11 window manager), the widgets' location and maybe more.

The default implementation implements the closing of popup widgets when you click outside the window. For other widget types it does nothing.

See also mouseReleaseEvent(), mouseDoubleClickEvent(), mouseMoveEvent(), event(), QMouseEvent, and Scribble Example.
上海欢迎您
快速回复
限100 字节
 
上一个 下一个