QT4 Drag Window Without Title Bar

前端 未结 3 2073
一向
一向 2020-12-28 21:17

The application I\'m working on has a custom UI that required me to remove the title bar from the main window. Unfortunately, I can\'t figure out how to make it so I can mo

相关标签:
3条回答
  • 2020-12-28 21:43

    You should try this instead

    class MyWidget : public QMainWindow
    {
    protected:
        void mouseMoveEvent(QMouseEvent* event);
        void mousePressEvent(QMouseEvent* event);
        void mouseReleaseEvent(QMouseEvent* event);
    private:
        QPoint mLastMousePosition;
        bool mMoving;
    }
    /// Source:
    void MyWidget::mousePressEvent(QMouseEvent* event)
    {
        if(event->button() == Qt::LeftButton)
        {
            mMoving = true;
            mLastMousePosition = event->pos();
        }
    }
    
    void MyWidget::mouseMoveEvent(QMouseEvent* event)
    {
        if( event->buttons().testFlag(Qt::LeftButton) && mMoving)
        {
            this->move(this->pos() + (event->pos() - mLastMousePosition));
        }
    }
    
    void MyWidget::mouseReleaseEvent(QMouseEvent* event)
    {
        if(event->button() == Qt::LeftButton)
        {
            mMoving = false;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 21:52

    Most intuitive way to do this would be clicking on the widget surface and dragging. In order to achieve this, you need to follow some steps.

    The method goes as follows: When the user presses inside the widget, set a boolean flag and store the position of the mouse and then when the button is released, set it to false. The next step is moving the widget. In the mouseMoveEvent, check if the flag is set. If it is set, take the new position of the mouse. Calculate the difference between new position and the stored one. Then, set the position of the window to original position + the calculated mouse movement. Then store the new mouse position.

    The code required would be this:

    WARNING: This code example is incorrect and will result in a jumping behavior when dragging the window. Please use the code from the Qt Shaped Clock example instead.

    /// Header contents:
    class MyWidget : public QMainWindow
    {
    protected:
        void mouseMoveEvent(QMouseEvent* event);
        void mousePressEvent(QMouseEvent* event);
        void mouseReleaseEvent(QMouseEvent* event);
    private:
        QPoint mLastMousePosition;
        bool mMoving;
    }
    /// Source:
    void MyWidget::mousePressEvent(QMouseEvent* event)
    {
        if(event->button() == Qt::LeftButton)
        {
            mMoving = true;
            mLastMousePosition = event->pos();
        }
    }
    
    void MyWidget::mouseMoveEvent(QMouseEvent* event)
    {
        if( event->buttons().testFlag(Qt::LeftButton) && mMoving)
        {
            this->move(this->pos() + (event->pos() - mLastMousePosition));
            mLastMousePosition = event->pos();
        }
    }
    
    void MyWidget::mouseReleaseEvent(QMouseEvent* event)
    {
        if(event->button() == Qt::LeftButton)
        {
            mMoving = false;
        }
    }
    
    0 讨论(0)
  • 2020-12-28 21:55

    I don't remenber very well, but you can register a custom event handler, is easy there are so many examples on google. check for mouse event, before that you can use QObject::sender() and get the metaobject to know the class of the object tha is pressed if the object is a qframe or qwidget or another class that you want to use to move the window. then you can apply a logic to drag the window. the pros is that you can use with all window and frame without reimplement each one of them with mouse events

    0 讨论(0)
提交回复
热议问题