質問

I am working on a simple 3D viewer using PyQt and its bindings for OpenGL. I would like to implement the following user actions (like for example in solidworks):

  1. pan/translate: with combination ctrl+middle mouse button (pressed)
  2. rotate: middle mouse button (pressed)

and moving mouse. The code is:

def mousePressEvent(self, event):
    self.last_pos = event.posF()

def mouseMoveEvent(self, event):
    dxy = event.posF() - self.last_pos
    dx = dxy.x() / self.width()
    dy = dxy.y() / self.height()
    #    rotate - 
    if event.buttons() & Qt.MidButton:
        self.camera.rotate(dx, dy)
    #    translate/pan
    elif (event.buttons() & Qt.MidButton) and (event.modifiers() & Qt.ControlModifier):
        self.camera.pan(dx, dy)
    #    zoom
    self.last_pos = event.posF()

The problem I have is that when I press ctrl and then middle mouse button both (self.camera.rotate(dx, dy) and self.camera.pan(dx, dy)) functions are executed, but I would like just to translate the object(s). I would like to ask you is there a way how can the code be modified that translate and rotate will work with the desires key combinations. Right now I do not have any ideas.

役に立ちましたか?

解決

Both actions require the middle button. But only pan/translate requires Ctrl, so use that to switch between them:

if event.buttons() & Qt.MidButton:
    if event.modifiers() & Qt.ControlModifier:
        self.camera.pan(dx, dy)
    else:
        self.camera.rotate(dx, dy)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top