Qt鼠标移动/动作组合

时间:2014-02-27 11:50:13

标签: qt

如何管理组合鼠标移动(例如左键单击+中键单击+左键单击释放)

我有方法mousePressEventmouseReleaseEvent,但我没有找到合并它们的方法。

1 个答案:

答案 0 :(得分:0)

如果您需要合并来自mousePressmouseRelease的信息,您需要以某种方式跟踪哪个按钮仍然按下以及哪个按钮已经再次释放。

下面是一个相当简单的示例,它似乎与您描述的内容(打印输出指示的操作)

如果有帮助,请告诉我

#include <QGraphicsScene>
#include <QGraphicsView>
#include <QMouseEvent>
#include <QtCore>


class MyGraphicsView: public QGraphicsView
{
public:
  MyGraphicsView( QGraphicsScene *scene, QWidget *parent = 0) : QGraphicsView(scene,parent)
  {
    active[Qt::LeftButton] = false;
    active[Qt::RightButton] = false;
    active[Qt::MiddleButton] = false;
  }

public slots:
  virtual void mousePressEvent( QMouseEvent * event );
  virtual void mouseReleaseEvent( QMouseEvent * event );

  std::map< Qt::MouseButton, bool > active;

};

void MyGraphicsView::mousePressEvent( QMouseEvent * event )
{
  active[event->button()] = true;

  if( event->button() == Qt::LeftButton && !active[Qt::MiddleButton] && !active[Qt::RightButton]) {
    qDebug() << "select";
  } else if (event->button() == Qt::RightButton && !active[Qt::LeftButton] && !active[Qt::MiddleButton] ) {
    qDebug() << "property";
  } else if ( event->button() == Qt::LeftButton && active[Qt::MiddleButton] && !active[Qt::RightButton]) {
    qDebug() << "zoom";
  } else if ( event->button() == Qt::MiddleButton && !active[Qt::LeftButton] && !active[Qt::RightButton] ) {
    qDebug() << "pan";
  }

}

void MyGraphicsView::mouseReleaseEvent( QMouseEvent * event )
{
  if( event->button() == Qt::LeftButton && active[Qt::LeftButton] && active[Qt::MiddleButton] ){
    qDebug() << "move";
  }
  active[event->button()] = false;
}



int main( int argc, char **argv )
{
    QApplication app(argc, argv);
    QGraphicsScene scene;
    scene.setSceneRect( -100.0, -100.0, 200.0, 200.0 );

    MyGraphicsView view( &scene );
    view.show();

    return app.exec();
}