Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QMouse event
Qt 6.11 is out! See what's new in the release blog

QMouse event

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TETTRA
    wrote on last edited by
    #1

    Everyone knows that we are putting the program on a robotic table clamping the top strip. So I want to know how to put the window in only when I clamped the top bar and also how to make it where I clamped and left the mouse?

    QPoint newF::previousPosition() const
    {
        return m_previousPosition;
    }
    
    void newF::setPreviousPosition(QPoint previousPosition)
    {
        if (m_previousPosition == previousPosition)
            return;
    
        m_previousPosition = previousPosition;
        emit previousPositionChanged(previousPosition);
    }
    
    void newF::mouseMoveEvent(QMouseEvent *event)
    {
                auto dx = event->x() - m_previousPosition.x();
                auto dy = event->y() - m_previousPosition.y();
                setGeometry(x() + dx, y() + dy, width(), height());
    
        return QWidget::mouseMoveEvent(event);
    }
    
    joeQJ 1 Reply Last reply
    0
    • T TETTRA

      Everyone knows that we are putting the program on a robotic table clamping the top strip. So I want to know how to put the window in only when I clamped the top bar and also how to make it where I clamped and left the mouse?

      QPoint newF::previousPosition() const
      {
          return m_previousPosition;
      }
      
      void newF::setPreviousPosition(QPoint previousPosition)
      {
          if (m_previousPosition == previousPosition)
              return;
      
          m_previousPosition = previousPosition;
          emit previousPositionChanged(previousPosition);
      }
      
      void newF::mouseMoveEvent(QMouseEvent *event)
      {
                  auto dx = event->x() - m_previousPosition.x();
                  auto dy = event->y() - m_previousPosition.y();
                  setGeometry(x() + dx, y() + dy, width(), height());
      
          return QWidget::mouseMoveEvent(event);
      }
      
      joeQJ Offline
      joeQJ Offline
      joeQ
      wrote on last edited by
      #2

      @TETTRA Hi, friend, do you want to use the mouse to move the window dialog of not have title bar ?

      sorry . I didn't understand what you said.

      Just do it!

      T 1 Reply Last reply
      2
      • joeQJ joeQ

        @TETTRA Hi, friend, do you want to use the mouse to move the window dialog of not have title bar ?

        sorry . I didn't understand what you said.

        T Offline
        T Offline
        TETTRA
        wrote on last edited by
        #3

        @joeQ Yes

        joeQJ 1 Reply Last reply
        0
        • T TETTRA

          @joeQ Yes

          joeQJ Offline
          joeQJ Offline
          joeQ
          wrote on last edited by joeQ
          #4

          @TETTRA Why didn't you use the mousePress,mouseRelease,mouseMove event?

          Reimplement the following virtual functions in subclass of QMainWindow or QDialog, or QWidget

          
          virtual void mouseMoveEvent(QMouseEvent *event)
          virtual void mousePressEvent(QMouseEvent *event)
          virtual void mouseReleaseEvent(QMouseEvent *event)
          

          First way(Suggest)

          When user use the left button to press window, it will begin to move, in the moving, you can know the position of mouse from mouseMoveEvent, and to set the new position of window use move() function. when user release left button, moving window is end.

          Second way

          Only use the mouseMoveEvent, but, you must to check the left button is pressed when mouse moving in function. But, you must set the setMouseTracking(true).

          Notes:

          If you want the moving action look naturally. You should record the press point in Application Window position.

          Try them again.

          Demo Snippet Code

          
          Widget::Widget(QWidget *parent) :
              QWidget(parent),
              ui(new Ui::Widget)
          {
              ui->setupUi(this);
          }
          
          Widget::~Widget()
          {
              delete ui;
          }
          
          void Widget::mousePressEvent(QMouseEvent *event)
          {
              _winPt   = this->pos();
              _mousePt = event->globalPos();
              _dtPt    = _mousePt - _winPt;
          }
          
          void Widget::mouseMoveEvent(QMouseEvent *event)
          {
              this->move(event->globalPos() - _dtPt);
          }
          
          

          Just do it!

          T 1 Reply Last reply
          1
          • joeQJ joeQ

            @TETTRA Why didn't you use the mousePress,mouseRelease,mouseMove event?

            Reimplement the following virtual functions in subclass of QMainWindow or QDialog, or QWidget

            
            virtual void mouseMoveEvent(QMouseEvent *event)
            virtual void mousePressEvent(QMouseEvent *event)
            virtual void mouseReleaseEvent(QMouseEvent *event)
            

            First way(Suggest)

            When user use the left button to press window, it will begin to move, in the moving, you can know the position of mouse from mouseMoveEvent, and to set the new position of window use move() function. when user release left button, moving window is end.

            Second way

            Only use the mouseMoveEvent, but, you must to check the left button is pressed when mouse moving in function. But, you must set the setMouseTracking(true).

            Notes:

            If you want the moving action look naturally. You should record the press point in Application Window position.

            Try them again.

            Demo Snippet Code

            
            Widget::Widget(QWidget *parent) :
                QWidget(parent),
                ui(new Ui::Widget)
            {
                ui->setupUi(this);
            }
            
            Widget::~Widget()
            {
                delete ui;
            }
            
            void Widget::mousePressEvent(QMouseEvent *event)
            {
                _winPt   = this->pos();
                _mousePt = event->globalPos();
                _dtPt    = _mousePt - _winPt;
            }
            
            void Widget::mouseMoveEvent(QMouseEvent *event)
            {
                this->move(event->globalPos() - _dtPt);
            }
            
            
            T Offline
            T Offline
            TETTRA
            wrote on last edited by
            #5

            @joeQ Excuse me. Can you write me more in detail. Just i'm new

            joeQJ 1 Reply Last reply
            0
            • T TETTRA

              @joeQ Excuse me. Can you write me more in detail. Just i'm new

              joeQJ Offline
              joeQJ Offline
              joeQ
              wrote on last edited by joeQ
              #6

              @TETTRA I don't know what you do not understand. The snippet code is very clear. Can you drew the window and diaog coordinate system ? Please draw it for yourself, you will clear well. and, you also can debug it to see the value.

              Click to download the complete demo

              The link is valid for one week

              Just do it!

              T 1 Reply Last reply
              2
              • joeQJ joeQ

                @TETTRA I don't know what you do not understand. The snippet code is very clear. Can you drew the window and diaog coordinate system ? Please draw it for yourself, you will clear well. and, you also can debug it to see the value.

                Click to download the complete demo

                The link is valid for one week

                T Offline
                T Offline
                TETTRA
                wrote on last edited by
                #7

                @joeQ Thx. If I use this-> setWindowFlags (Qt :: SplashScreen) ;? Then how is the vintue tittle bar? But that did not stretch the windows?

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved