Window resizing logic error



  • Hello Everyone,

    I am working to resize a boarderless window in qt, but I am having a strange problem, when I try to resize on the top and the left of the window, my mouse movement is scaled down and movement is very jittery. On the bottom and right, it behaves as expected with normal resizes. I am pretty sure there is a logic error in my code but can't figure out after two days of contemplating figure it out. I write this with qt and c++ but is fairly simple and should be readable to anyone who understands c style computer code.

    My code works by setting a enum of possible sides the mouse can be over, set when the mouse moves over them. When the mouse moves over the edge, the code calls resizeWindow and sets in resize zone to true. When I click the mouse, the mouse sets the coordinates of the press and sets the variable allowToResize to true. When the time comes to resize, the code subtracts the current mouse position from the position of the click position, adds it to the side of the rectangle that you are resizing, and then resets the originional click position with the current clikc position

    I would greatly appreaciate it if anyone can figure out what I overlooked in the code that causes it to behave correctly on some sides and scaled down on the top and left. If you need more of my code just let me know and I will post it. Thanks!!!

    Here is my code:

    @//called when mouse is pressed
    void mainTrackbox::mousePressEvent(QMouseEvent *e)
    {
    if (e->button() == Qt::LeftButton)
    {
    if (inResizeZone)
    {
    offset = e->pos();
    allowToResize = true;
    }
    }
    e->accept();
    }

    //Called when mouse is released
    void mainTrackbox::mouseReleaseEvent(QMouseEvent *e)
    {
    moveWidget = false;
    allowToResize = false;
    resizeDir = none;
    offset = QPoint();

    e->accept();
    

    }

    //Called when mouse is over resize zone, enum set when mouse moves over resize zone as well
    void mainTrackbox::resizeWindow(QMouseEvent *e)
    {
    if(allowToResize) {
    QPoint eventPoint = e->pos();
    QRect geom = this->geometry();
    QPoint diff = eventPoint - offset;

        qDebug() << eventPoint;
        qDebug() << offset;
        qDebug() << diff;
    
        switch (resizeDir) {
        default:
            break;
        case none:
            qDebug() <<"None";
            break;
        case top:
            qDebug() <<  geom.top();
            geom.setTop(geom.top() + diff.y());
            qDebug() <<  geom.top();
            qDebug() <<"Top";
            break;
        case bottom:
            qDebug() <<"Bottom";
            geom.setBottom(geom.bottom() + diff.y());
            break;
        case left:
            qDebug() <<"Left";
            geom.setLeft(geom.left() + diff.x());
            break;
        case right:
            qDebug() <<"Right";
            geom.setRight(geom.right() + diff.x());
            break;
        }
        this->setGeometry(geom);
        offset = eventPoint;
    }
    

    }@



  • Don't know exactly and no expert on the paint stuff, but it probably has to do with the fact that all widgets/paintdevices are build from top left to bottom right causing more overhead when shifting those edges. On the other hand it should be possible to do so. Maybe increase your diff value to a higher threshold before redrawing. Let's say that you only redraw every 15 pixels? Just a thought ;-)


  • Moderators

    fixed in your "initial thread":http://qt-project.org/forums/viewthread/31246/.
    Nevertheless you wont be able to eliminate the jumping when resizing top/left edges since there is also an (event-based) moving of the window involved...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.