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

Resizing problems, dragging when it is not allowed



  • 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);
    }
    

  • Lifetime Qt Champion

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



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