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. click left mouse button, window's position change
Forum Updated to NodeBB v4.3 + New Features

click left mouse button, window's position change

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 509 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.
  • C Offline
    C Offline
    chris_rookie
    wrote on last edited by
    #1
    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.

    J.HilkJ 1 Reply Last reply
    0
    • C chris_rookie
      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.

      J.HilkJ Online
      J.HilkJ Online
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @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 ?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      C 1 Reply Last reply
      0
      • J.HilkJ J.Hilk

        @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 ?

        C Offline
        C Offline
        chris_rookie
        wrote on last edited by
        #3

        @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.

        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