GLViewWidget怎么禁止鼠标移动事件
时间: 2023-11-27 17:53:59 浏览: 33
GLViewWidget是Qt中用于OpenGL绘图的窗口控件,可以通过重写其mouseMoveEvent()函数来禁止鼠标移动事件。具体实现代码如下:
```cpp
class MyGLViewWidget : public QGLWidget
{
//...
protected:
void mouseMoveEvent(QMouseEvent *event) override
{
// 不做任何操作,即禁止鼠标移动事件
}
};
```
在这个示例中,我们重写了mouseMoveEvent()函数,但是没有做任何操作,这样就可以禁止鼠标移动事件。如果需要重新启用鼠标移动事件,只需要在该函数中添加相应的代码即可。
相关问题
GLViewWidget怎么禁止鼠标移动事件,用python给出具体案例
GLViewWidget是PyQtGraph中的一个组件,用于显示OpenGL渲染的图形界面。如果你想禁止鼠标移动事件,可以通过重写GLViewWidget的mouseMoveEvent()函数来实现。
具体实现方式如下:
```python
import pyqtgraph.opengl as gl
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QWidget
class MyGLViewWidget(gl.GLViewWidget):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def mouseMoveEvent(self, ev):
# 禁止鼠标移动事件
pass
if __name__ == '__main__':
app = QApplication([])
widget = QWidget()
view = MyGLViewWidget(widget)
view.show()
app.exec_()
```
在上述代码中,我们定义了一个继承自GLViewWidget的MyGLViewWidget类,并重写了它的mouseMoveEvent()函数,在函数中直接pass,即不做任何处理,从而实现了禁止鼠标移动事件的效果。
最后,在主程序中创建MyGLViewWidget对象并显示即可。
GLViewWidget显示坐标
GLViewWidget是一个OpenGL窗口,如果你想要在窗口上显示坐标,可以在OpenGL中使用gluProject函数将3D坐标转换为2D坐标,然后在窗口上绘制出来。具体实现方法可以参考下面的代码示例:
```python
from OpenGL.GL import *
from OpenGL.GLU import *
from PyQt5.QtOpenGL import *
class GLViewWidget(QGLWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.x = 0
self.y = 0
def initializeGL(self):
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glDepthFunc(GL_LESS)
glEnable(GL_DEPTH_TEST)
glShadeModel(GL_SMOOTH)
def resizeGL(self, width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45.0, float(width) / float(height), 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glTranslatef(0.0, 0.0, -6.0)
glColor3f(1.0, 1.0, 1.0)
glutSolidSphere(1.0, 20, 20)
def mouseMoveEvent(self, event):
x = event.x()
y = event.y()
viewport = glGetIntegerv(GL_VIEWPORT)
modelview = glGetDoublev(GL_MODELVIEW_MATRIX)
projection = glGetDoublev(GL_PROJECTION_MATRIX)
self.x, self.y, _ = gluUnProject(x, self.height() - y, 0.0, modelview, projection, viewport)
def paintEvent(self, event):
painter = QPainter(self)
painter.drawText(self.x, self.y, "({:.2f}, {:.2f})".format(self.x, self.y))
```
这个示例中,我们在GLViewWidget中继承了QGLWidget,并且重载了initializeGL、resizeGL、paintGL、mouseMoveEvent和paintEvent等函数。其中,mouseMoveEvent函数会在鼠标移动时被触发,我们可以在这个函数中使用gluUnProject函数将窗口坐标转换为OpenGL坐标,并且将它保存下来。然后,我们在paintEvent函数中使用QPainter来绘制这个坐标。这样,当鼠标移动时,就会在窗口上显示出当前的坐标。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)