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. Resizing problems, dragging when it is not allowed
QtWS25 Last Chance

Resizing problems, dragging when it is not allowed

Scheduled Pinned Locked Moved Solved General and Desktop
qtwidgetsresizedraggingborderless wind
3 Posts 2 Posters 719 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.
  • K Offline
    K Offline
    Krakenus00
    wrote on last edited by
    #1

    Hello, I am creating a borderless resizable window. I have constraints for minimum size and trying to prevent a user from any making the window smaller than it is allowed to be. But when I press LMB on the border when the window is at the minimum size and try to make it slightly smaller, it is being dragged. I can drag my window for about 11-15 px per one attempt. If I drag it a bit more than 11-15px, it jumps to the proper position. I checked my code with a debugger and it sets the proper values, however, the window is still being dragged.
    So I have the next code:

    void CustomizableWindow::mousePressEvent(QMouseEvent* event)
    {
    	if (isMaximized())
    		return;
    	m_bMousePressed = true;
    	m_bCanChangeCursor = false;
    	m_StartGeometry = this->geometry();
    }
    
    bool CustomizableWindow::eventFilter(QObject* obj, QEvent* event)
    {
    	if (!isMaximized())
    	{
    		// Check mouse moving
    		if (event->type() == QEvent::MouseMove)
    		{
    			QMouseEvent* pMouse = dynamic_cast<QMouseEvent*>(event);
    			if (pMouse)
    			{
    				if (!m_bMousePressed)
    					checkMousePos(pMouse->globalPos());
    				else checkBorderDragging(pMouse);
    			}
    		}
    
    		// Check mouse pressing on frame
    		else if (event->type() == QEvent::MouseButtonPress && obj == this)
    		{
    			QMouseEvent* pMouse = dynamic_cast<QMouseEvent*>(event);
    			if (pMouse)
    				mousePressEvent(pMouse);
    		}
    		....
    	}
    	return QWidget::eventFilter(obj, event);
    }
    
    void CustomizableWindow::checkBorderDragging(QMouseEvent* event)
    {
    	if (isMaximized())
    		return;
    
    	QPoint globalMousePos = event->globalPos();
    	if (m_bMousePressed)
    	{
    		QRect newGeometry = m_StartGeometry;
    		calculateDragging(newGeometry, globalMousePos);
    		setGeometry(newGeometry);
    		m_StartGeometry = newGeometry;
    	}
    }
    
    void CustomizableWindow::calculateDragging(QRect& windowGeometry, const QPoint& globalMousePos)
    {
    	int horDiff = 0, verDiff = 0;
    	int newX = windowGeometry.x(), newY = windowGeometry.y();
    
    	// Calculate difference
    	if (m_bDragLeft)
    	{
    		horDiff = windowGeometry.x() - globalMousePos.x();
    		newX = globalMousePos.x();
    	}
    	else if (m_bDragRight)
    	{
    		horDiff = globalMousePos.x() - (windowGeometry.x() + windowGeometry.width());
    	}
    	if (m_bDragTop)
    	{
    		verDiff = windowGeometry.y() - globalMousePos.y();
    		newY = globalMousePos.y();
    	}
    	else if (m_bDragBottom)
    	{
    		verDiff = globalMousePos.y() - (windowGeometry.y() + windowGeometry.height());
    	}
    
    	// Calculate new size
    	int newWidth = windowGeometry.width() + horDiff;
    	int newHeight = windowGeometry.height() + verDiff;
    
    	// Check if all is good
    	if (newWidth < WIND_MIN_W)
    	{
    		newWidth = WIND_MIN_W;
    		newX = windowGeometry.x();
    	}
    	if (newHeight < WIND_MIN_H)
    	{
    		newHeight = WIND_MIN_H;
    		newY = windowGeometry.y();
    	}
    
    	// Set new values
    	windowGeometry.setX(newX);
    	windowGeometry.setY(newY);
    	windowGeometry.setWidth(newWidth);
    	windowGeometry.setHeight(newHeight);
    }
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      so
      SetMinimumSize()
      Is not honored when one is doing custom resizing + boderless ?

      1 Reply Last reply
      0
      • K Offline
        K Offline
        Krakenus00
        wrote on last edited by
        #3

        SetMinimumSize() does not work for me. I think it is because I change the window's position. However, the problem was in my form file. For an unknown reason, it changed the window's size to 802x600 when I was checking for 800x600. Maybe there was some another way out, but now all works just fine.

        1 Reply Last reply
        1

        • Login

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