c++ - Qt鼠标事件左键不翻译qt对象

标签 c++ qt

我在 QGLWidget 类上有以下鼠标事件......

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    QPointF mousePosition = event->pos(); 
     float dx = event->x() - lastPos.x();
     float dy = event->y() - lastPos.y();

     if (event->buttons() & Qt::LeftButton) {
         setXRotation(xRot + 8.0 * dy);
         setYRotation(yRot + 8.0 * dx);
     } else if (event->buttons() & Qt::RightButton) {
         xTrans += dx;
         yTrans -= dy;  
     }    
     lastPos = event->pos();
}

paint GL 中的旋转有效但平移无效。我做错了什么...?

void GLWidget::paintGL()
    {

     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     glMatrixMode(GL_MODELVIEW);
     glPushMatrix();
     glLoadIdentity();
     glTranslatef(0.0, 0.0, -5.0);
     glRotatef(xRot / 16.0, 1.0, 0.0, 0.0);
     glRotatef(yRot / 16.0, 0.0, 1.0, 0.0);
     glRotatef(zRot / 16.0, 0.0, 0.0, 1.0);


glTranslatef(xTrans, yTrans, 0.0f);
             glScalef(scale, scale, scale);
    //draw function here
glPopMatrix();

            }

最佳答案

您必须弹出矩阵才能应用它

http://www.swiftless.com/tutorials/opengl/pop_and_push_matrices.html

这允许推送的矩阵实际完成计算并得到使用。

glPopMatrix(); //end the current object transformations

希望对您有所帮助。

关于c++ - Qt鼠标事件左键不翻译qt对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23705956/

相关文章:

c++ - std::dynamic_pointer_cast 未正确向下转换

c++ - 使用 vector 迭代器遇到的错误

android - 如何在 Debug模式下将 Qt Creator 中的 --android-platform 更改为不是 android--1?

linux - 将命令行参数传递给 qml

C++ Qt QGraphicsView : Remembering the position of scrollbars after reload

c++ - Qt图表错误: ASSERT: "width > 0.0" in file painting\qrasterizer. cpp,第761行

c++ - 为什么我会收到此模板编译错误?

c++ - 如何对一组对进行排序?

c++ - 无需迭代即可从字符串数组中识别特定字符串

c++ - Qt 覆盖 QLabel PaintEvent