Qt makes me question my sanity and existence. I do not know why code that runs in one program that I wrote will not work in another program that I wrote. The following code is identical in both programs. In P1, it works correctly, allowing only left clicks. In P2, this is exactly the same, except that the left click code does something else.
In P2, I check the left click condition and execute the code if it is true. Well, when I left or right-clicked, it won’t do the code. If I change the condition to check the right click and return, if true, then the left click works fine, but the right click will not return. If I remove the conditions, both the left and right click run the code.
I lose my mind because such stupid things happen all the time, and I don’t know why, although I do everything the same way as other programs that work (what I wrote).
Edit: seems to ignore if-check in the mouseRelease function and works correctly for mousePress and mouseMove.
P1 (this program works exactly the way I want it):
void GLWidget::mousePressEvent(QMouseEvent *event)
{
clickOn = event->pos();
clickOff = event->pos();
if (event->buttons() & Qt::RightButton){
return;
}
}
void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
clickOff = event->pos();
if (event->buttons() & Qt::RightButton)
return;
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
clickOff = event->pos();
if (event->buttons() & Qt::LeftButton) {
updateGL();
} else if(event->buttons() & Qt::RightButton){
}
}
P2 (structured similarly to P1, but doesn’t work like that):
void GLWidget::mousePressEvent(QMouseEvent *event)
{
clickOn = event->pos();
clickOff = event->pos();
if (event->buttons() & Qt::LeftButton) {
}
}
void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
clickOff = event->pos();
if (event->buttons() & Qt::LeftButton) {
}
}
void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
clickOff = event->pos();
clickDiff = clickOff - clickOn;
if (event->buttons() & Qt::LeftButton) {
updateGL();
}
}
source
share