Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

click left mouse button, window's position change



  • CustomizeQWidget::CustomizeQWidget(QWidget *parent)
        : QDialog(parent)
    {
        this -> setWindowFlags(Qt::FramelessWindowHint);
    }
    
    CustomizeQWidget::~CustomizeQWidget()
    {
    }
    
    void CustomizeQWidget::paintEvent(QPaintEvent *)
    {
        QStyleOption opt;
        opt.init(this);
        QPainter p(this);
        style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
    }
    
    void CustomizeQWidget::mousePressEvent(QMouseEvent *event)
    {
        if(event->button() == Qt::LeftButton)
        {
            m_last_mouse_position = event->globalPos();
        }
    }
    
    void CustomizeQWidget::mouseMoveEvent(QMouseEvent *event)
    {
        if (!event->buttons().testFlag(Qt::LeftButton))
                return;
        const QPoint position = pos() + event->globalPos() - m_last_mouse_position; //the position of mainfrmae + (current_mouse_position - last_mouse_position)
        move(position.x(), position.y());
        m_last_mouse_position = event->globalPos();
    }
    

    I written code like this. it removed system's Frame and can be dragged. But , When I clicking a QPushButton throw mouse left button, keep a little time, the windows's position got changed. How can I fix it.


  • Moderators

    @chris_rookie
    I would suggest, by defining an area where you want the mousePressEvent to be a valid starting point.

    If pressed in that area set a bool/flag to true and only move, when that variable allows it ?



  • @J-Hilk
    I got you.
    But there's many of button. I have to writte so much code to fix this issue.
    Is there any better suggestions ^_^.
    if not, I would do like this.


Log in to reply