Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 995565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:44:35+00:00 2026-05-16T06:44:35+00:00

Qt makes me question my sanity and existence. I don’t know why code that

  • 0

Qt makes me question my sanity and existence. I don’t know why code that works in one program that I wrote will not work in another program I wrote. The following code is identical in both programs. In P1 it works correctly by only allowing left clicks. In P2 it is exactly the same, except the left click code is does something different.

In P2 I have it checking the left click condition and do the code if it is true. Well, when I left or right click, it won’t do the code. If I change the condition to check for a right click and return if true, then the left click works normally, but right click won’t return. If I remove the conditions, both left and right click runs the code.

I am losing my mind because stupid stuff like this keeps happening all the time and I don’t know why even though I do everything the same as the other programs that work (that I wrote).

Edit: It seems to ignore the if-check in the mouseRelease function and works correctly for mousePress and mouseMove.

P1 (this program works exactly how I want it to):

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    clickOn = event->pos();
    clickOff = event->pos();

    // right mouse button
    if (event->buttons() & Qt::RightButton){
        return;
    }

    // rest of left-click code here
}

/*************************************/

void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
    clickOff = event->pos();

    // right mouse button shouldn't do anything
    if (event->buttons() & Qt::RightButton)
        return;

    // rest of left click code here

}

/*************************************/

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    clickOff = event->pos();

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {

        // left click code

        updateGL();

    } else if(event->buttons() & Qt::RightButton){

        // right mouse button code

    }
}

P2 (structured similar to P1, but doesn’t work correctly):

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    clickOn = event->pos();
    clickOff = event->pos();

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {
        // left click code
    }

}

void GLWidget::mouseReleaseEvent(QMouseEvent *event)
{
    clickOff = event->pos();

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {
        // left click code
    }

}

void GLWidget::mouseMoveEvent(QMouseEvent *event)
{
    clickOff = event->pos();
    clickDiff = clickOff - clickOn;

    // do it only if left mouse button is down
    if (event->buttons() & Qt::LeftButton) {
        // left click code
        updateGL();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T06:44:35+00:00Added an answer on May 16, 2026 at 6:44 am

    From QMouseEvent::buttons() documentation:

    For mouse release events this excludes the button that caused the event.

    So the solution is to use QMouseEvent::button() instead:

    void GLWidget::mouseReleaseEvent(QMouseEvent *event)
    {
        clickOff = event->pos();
    
        // do it only if left mouse button is down
        if (event->button() == Qt::LeftButton) {
            // left click code
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.