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 8337151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:12:46+00:00 2026-06-09T04:12:46+00:00

I have a QMainWindow that has a QWidget . The QWidget appears, and has

  • 0

I have a QMainWindow that has a QWidget. The QWidget appears, and has its origin set in the mouse down event of the QMainWindow. Then, in the mouse move event of the QMainWindow, the geometry of the QWidget is set, such that it appears that the user is drawing a rectangle on top of the QMainWindow. This is how I’m doing it:

void MyQMainWindow::mousePressEvent(QMouseEvent * E) {
    QPoint pos = e->pos();
    myQWidget->setGeometry(pos.x(), pos.y(), 0, 0);
    myQWidget->show();
}

void MyQMainWindow::mouseMoveEvent(QMouseEvent * e) {
    QPoint pos = e->pos();
    QPoint prv = myQWidget->pos();
    int w = pos.x() - prv.x();
    int h = pos.y() - prv.y();

    myQWidget.setGeometry(prv.x(), prv.y(), w, h);
}

void MyQMainWindow::mouseReleaseEvent(QMouseEvent *) {
    myQWidget.hide();
}

The problem with this approach is that when I drag the mouse up, or left of where I clicked. My calculations for w and h are negative, and so the window does not resize correctly (or at all).

I realize that moving up/left would mean that I need to change the origin of the widget, while keeping the bottom right corner of it in the same place by also increasing the width/height as needed, but how do I do this?

Thanks!

  • 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-06-09T04:12:48+00:00Added an answer on June 9, 2026 at 4:12 am

    I would do something like this:

     void MyQMainWindow::updateWidgetGeometry()
     {
          if (_initial == QPoint(-1,-1)) {
              return;
          }
    
          x = qMin(_initial.x(), _current.x());
          y = qMin(_initial.y(), _current.y());
          w = abs(_initial.x() - _current.x());
          h = abs(_initial.y() - _current.y());
    
          myQWidget.setGeometry(x, y, w, h);    
     } 
    
     void MyQMainWindow::mousePressEvent(QMouseEvent * E)
     {
          _initial = e->pos();
          _current = e->pos();
          updateWidgetGeometry()
          myQWidget->show();
     } 
    
     void MyQMainWindow::mouseMoveEvent(QMouseEvent * E)
     {
          _current= e->pos();
          updateWidgetGeometry()
     }
    
     void MyQMainWindow::mouseReleaseEvent(QMouseEvent *) 
     {
         _current = QPoint(-1,-1);
         _initial = QPoint(-1,-1);
         myQWidget.hide();
     } 
    

    Typed this up without a compiler.

    By the way, you might also need to setMouseTracking(true) so that if your mouse move event happens inside the widget your MyQMainWindow gets to handle it. I’m not sure about that bit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a widget class subclass of QMainWindow, and it has a central widget(QWidget),
I have an object that has a timer and it throws an event when
I have an WPF usercontrol that has a TextBox. I set the text Underline
Consider that I have a MainWindow that has the following button declared in its
I have a QMainWindow with two QTableViews on it. Each QTableView has its own
I would like to have a QMainWindow that can change it's look at runtime,
I have a class that inherits QMainWindow and I just want it to have
i have a QMainWindow. It has this parameters: this->setWindowFlags(Qt::Tool); this->setFocusPolicy(Qt::StrongFocus); this->setAttribute(Qt::WA_QuitOnClose,true); After showEvent calles
I have a QMainWindow which spawns a few wizards. The QMainWindow has a QFrame
I have a simple form UI that has a slot for a button, starting

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.