Resize a Boarderless QMainWindow



  • Hello everyone, I'm trying to resize and move a boarderless qmainwindow. I have used qt am stuck on this. If someone can walk me though this it would be great. I currenly have it moveing but not resizing.

    Here is my code:

    @void mainTrackbox::mousePressEvent(QMouseEvent *event){
    offset = event->pos();
    }

    void mainTrackbox::mouseMoveEvent(QMouseEvent *event)
    {
    if (event->buttons() && Qt::LeftButton) {

        QPoint diff = event->pos() - offset;
        QPoint newpos = this->pos() + diff;
        this->move(newpos);
    

    }

    }

    void mainTrackbox::mouseReleaseEvent(QMouseEvent *event) {
    offset = QPoint();
    }@



  • I made some progress, I now have it resizing, but it resize the opposite of how it should on the left and top but fine of the right and bottom.

    Here is my code:

    @void mainTrackbox::mousePressEvent(QMouseEvent *event){
    offset = event->pos();
    }

    void mainTrackbox::mouseMoveEvent(QMouseEvent *event)
    {
    qDebug() << offset.x() << "," << offset.y();
    if (event->buttons() && Qt::LeftButton) {

        if(offset.x() <= resizeBorder && offset.y() <= resizeBorder) {
            setCursor(Qt::SizeFDiagCursor);
            resizeMode = true;
        }
        else if(offset.x() >= size().width() - resizeBorder && offset.y() <= resizeBorder) {
            setCursor(Qt::SizeBDiagCursor);
            resizeMode = true;
        }
        else if(offset.x() <= resizeBorder && offset.y() >= size().height() - resizeBorder) {
            setCursor(Qt::SizeBDiagCursor);
            resizeMode = true;
        }
        else if(offset.x() >= size().width() - resizeBorder && offset.y() >= size().height() - resizeBorder) {
            setCursor(Qt::SizeFDiagCursor);
            resizeMode = true;
        }
        else if(offset.x() <= resizeBorder) {
            setCursor(Qt::SizeHorCursor);
            resizeMode = true;
        }
        else if(offset.y() <= resizeBorder) {
            setCursor(Qt::SizeVerCursor);
            resizeMode = true;
        }
        else if(offset.x() >= size().width() - resizeBorder) {
            setCursor(Qt::SizeHorCursor);
            resizeMode = true;
        }
        else if(offset.y() >= size().height() - resizeBorder) {
            setCursor(Qt::SizeVerCursor);
            resizeMode = true;
        }
    
        QPoint diff = event->pos() - offset;
        QPoint newpos = this->pos() + diff;
        if(!resizeMode) {
            this->move(newpos);
        }
    }
    

    }

    void mainTrackbox::mouseReleaseEvent(QMouseEvent *event) {
    setCursor(Qt::ArrowCursor);
    QPoint diff = event->pos() - offset;
    QPoint newpos = this->pos() + diff;
    if(resizeMode) {
    this->resize(newpos.x(), newpos.y());
    resizeMode = false;
    }
    offset = QPoint();
    }@



  • I'm currently working on a window that makes exactly the behavior that you are looking for. Here I post some code that I use. I hope it would be useful!

    I specify 5 pixels to act to resize the window.

    @
    #define PIXELS_TO_ACT 5

    void MainWindow::mouseMoveEvent(QMouseEvent *e)
    {
    int xMouse = e->pos().x();
    int yMouse = e->pos().y();
    int wWidth = geometry().width();
    int wHeight = geometry().height();

    if (moveWidget)
    {
        inResizeZone = false;
        moveWindow(e);
    }
    else if (allowToResize)
        resizeWindow(e);
    //Right side cursor
    else if (xMouse >= wWidth - PIXELS_TO_ACT or allowToResize)
    {
        inResizeZone = true;
    
        if (yMouse >= wHeight - PIXELS_TO_ACT)
            setCursor(Qt::SizeFDiagCursor);
        else if (yMouse <= PIXELS_TO_ACT)
            setCursor(Qt::SizeBDiagCursor);
        else
            setCursor(Qt::SizeHorCursor);
    
        resizeWindow(e);
    }
    //Left side cursor
    else if (xMouse <= PIXELS_TO_ACT or allowToResize)
    {
        inResizeZone = true;
    
        if (yMouse >= wHeight - PIXELS_TO_ACT)
            setCursor(Qt::SizeBDiagCursor);
        else if (yMouse <= PIXELS_TO_ACT)
            setCursor(Qt::SizeFDiagCursor);
        else
            setCursor(Qt::SizeHorCursor);
    
        resizeWindow(e);
    }
    //Bottom side cursor
    else if ((yMouse >= wHeight - PIXELS_TO_ACT) or allowToResize)
    {
        inResizeZone = true;
        setCursor(Qt::SizeVerCursor);
    
        resizeWindow(e);
    }
    //Top side cursor
    else if (yMouse <= PIXELS_TO_ACT or allowToResize)
    {
        inResizeZone = true;
        setCursor(Qt::SizeVerCursor);
    
        resizeWindow(e);
    }
    else
    {
        inResizeZone = false;
        setCursor(Qt::ArrowCursor);
    }
    
    e->accept();
    

    }
    @



  • Second part!
    @void MainWindow::mousePressEvent(QMouseEvent *e)
    {
    if (e->button() == Qt::LeftButton)
    {
    if (inResizeZone)
    {
    allowToResize = true;

            if (e->pos().y() <= PIXELS_TO_ACT)
            {
                if (e->pos().x() <= PIXELS_TO_ACT)
                    resizeDiagTopLeft = true;
                else if (e->pos().x() >= geometry().width() - PIXELS_TO_ACT)
                    resizeDiagTopRight = true;
                else
                    resizeVerTop = true;
            }
            else if (e->pos().x() <= PIXELS_TO_ACT)
                resizeHorLeft = true;
        }
        else if (e->pos().x() >= PIXELS_TO_ACT and e->pos().x() < ui->titleBar->geometry().width()
                 and e->pos().y() >= PIXELS_TO_ACT and e->pos().y() < ui->titleBar->geometry().height())
        {
            moveWidget = true;
            dragPosition = e->globalPos() - frameGeometry().topLeft();
        }
    }
    e->accept();
    

    }

    void MainWindow::mouseReleaseEvent(QMouseEvent *e)
    {
    moveWidget = false;
    allowToResize = false;
    resizeVerSup = false;
    resizeHorEsq = false;
    resizeDiagSupEsq = false;
    resizeDiagSupDer = false;

    e->accept();
    

    }

    void MainWindow::moveWindow(QMouseEvent *e)
    {
    if (e->buttons() & Qt::LeftButton)
    {
    move(e->globalPos() - dragPosition);
    e->accept();
    }
    }

    void MainWindow::resizeWindow(QMouseEvent *e)
    {
    if (allowToResize)
    {
    int xMouse = e->pos().x();
    int yMouse = e->pos().y();
    int wWidth = geometry().width();
    int wHeight = geometry().height();

        if (cursor().shape() == Qt::SizeVerCursor)
        {
            if (resizeVerTop)
            {
                int newY = geometry().y() + yMouse;
                int newHeight = wHeight - yMouse;
    
                if (newHeight > minimumSizeHint().height())
                {
                    resize(wWidth, newHeight);
                    move(geometry().x(), newY);
                }
            }
            else
                resize(wWidth, yMouse+1);
        }
        else if (cursor().shape() == Qt::SizeHorCursor)
        {
            if (resizeHorLeft)
            {
                int newX = geometry().x() + xMouse;
                int newWidth = wWidth - xMouse;
    
                if (newWidth > minimumSizeHint().width())
                {
                    resize(newWidth, wHeight);
                    move(newX, geometry().y());
                }
            }
            else
                resize(xMouse, wHeight);
        }
        else if (cursor().shape() == Qt::SizeBDiagCursor)
        {
            int newX = 0;
            int newWidth = 0;
            int newY = 0;
            int newHeight = 0;
    
            if (resizeDiagTopRight)
            {
                newX = geometry().x();
                newWidth = xMouse;
                newY = geometry().y() + yMouse;
                newHeight = wHeight - yMouse;
            }
            else
            {
                newX = geometry().x() + xMouse;
                newWidth = wWidth - xMouse;
                newY = geometry().y();
                newHeight = yMouse;
            }
    
            if (newWidth >= minimumSizeHint().width() and newHeight >= minimumSizeHint().height())
            {
                resize(newWidth, newHeight);
                move(newX, newY);
            }
            else if (newWidth >= minimumSizeHint().width())
            {
                resize(newWidth, wHeight);
                move(newX, geometry().y());
            }
            else if (newHeight >= minimumSizeHint().height())
            {
                resize(wWidth, newHeight);
                move(geometry().x(), newY);
            }
        }
        else if (cursor().shape() == Qt::SizeFDiagCursor)
        {
            if (resizeDiagTopLeft)
            {
                int newX = geometry().x() + xMouse;
                int newWidth = wWidth - xMouse;
                int newY = geometry().y() + yMouse;
                int newHeight = wHeight - yMouse;
    
                if (newWidth >= minimumSizeHint().width() and newHeight >= minimumSizeHint().height())
                {
                    resize(newWidth, newHeight);
                    move(newX, newY);
                }
                else if (newWidth >= minimumSizeHint().width())
                {
                    resize(newWidth, wHeight);
                    move(newX, geometry().y());
                }
                else if (newHeight >= minimumSizeHint().height())
                {
                    resize(wWidth, newHeight);
                    move(geometry().x(), newY);
                }
            }
            else
                resize(xMouse+1, yMouse+1);
        }
    
        e->accept();
    }
    

    }@



  • Hello,
    Thanks for all of the help, but I am having one problem.
    No matter what i do, allowToResize is never getting set to true. If I try and remove requirement for allow to allowToResize in resizeWindow(), then it never stops resize and goes back to move mode. Do you know if there is anything I can do?



  • Also, are the cursors supposed change when you move your mouse over the window? For me they only change when you click.



  • allowToResize must be set to true in mousePressEvent

    Have you call @setMouseTracking(true);@ in the constructor?



  • Thanks for all of the help.

    Mouse Tracking is set to true, but since it wasn't polling mouse move event correctly, I created a event filter that does. Now it is almost all functional.

    The window moves normally (though I had to replace the ui bar that you used with arbitrary numbers).

    However, resizeing is broken. If I move on the top right, top left, bottom left, left, and top, the window goes crazy, resizes in absurd dimensions faster than then the eye can really see and crashes with a bunch of warnings which I think say the window is trying to resize over my set maximum size.

    Here are the error messages:

    QXcbShmImage: shmget() failed (22) for size 37353680 (733x12740)
    QXcbShmImage: shmget() failed (22) for size 41024544 (733x13992)
    QXcbShmImage: shmget() failed (22) for size 44695408 (733x15244)
    The program has unexpectedly finished.

    I am using ubuntu 13.04 and qt 5, if it makes any difference.



  • Here is my code (The only differences from your code being the event filter and the ui bar size using arbitrary numbers)

    @#include "mainTrackbox.h"
    #include "ui_mainTrackbox.h"
    #include <QStyle>
    #include <QIcon>
    #include <QMessageBox>
    #include <QDesktopWidget>
    #include <QMouseEvent>

    int const mainTrackbox::resizeBorder = 20;
    #define PIXELS_TO_ACT 5

    mainTrackbox::mainTrackbox(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::mainTrackbox)
    {
    ui->setupUi(this);
    style = qApp->style();
    qApp->installEventFilter(this);
    setMouseTracking(true);
    resizeMode = false;
    moveWidget =false;
    inResizeZone = false;
    allowToResize = false;
    resizeDiagTopLeft = false;
    resizeDiagTopRight = false;
    resizeVerTop = false;
    resizeHorLeft = false;
    resizeHorEsq = false;
    resizeVerSup =false;
    resizeDiagSupDer = false;
    resizeDiagSupEsq =false;
    }

    mainTrackbox::~mainTrackbox()
    {
    delete ui;
    }

    void mainTrackbox::mouseMoveEvent(QMouseEvent *e)
    {
    int xMouse = e->pos().x();
    int yMouse = e->pos().y();
    int wWidth = geometry().width();
    int wHeight = geometry().height();

    if (moveWidget)
    {
        inResizeZone = false;
        moveWindow(e);
    }
    else if (allowToResize)
        resizeWindow(e);
    //Right side cursor
    else if (xMouse >= wWidth - PIXELS_TO_ACT or allowToResize)
    {
        inResizeZone = true;
    
        if (yMouse >= wHeight - PIXELS_TO_ACT)
            setCursor(Qt::SizeFDiagCursor);
        else if (yMouse <= PIXELS_TO_ACT)
            setCursor(Qt::SizeBDiagCursor);
        else
            setCursor(Qt::SizeHorCursor);
    
        resizeWindow(e);
    }
    //Left side cursor
    else if (xMouse <= PIXELS_TO_ACT or allowToResize)
    {
        inResizeZone = true;
    
        if (yMouse >= wHeight - PIXELS_TO_ACT)
            setCursor(Qt::SizeBDiagCursor);
        else if (yMouse <= PIXELS_TO_ACT)
            setCursor(Qt::SizeFDiagCursor);
        else
            setCursor(Qt::SizeHorCursor);
    
        resizeWindow(e);
    }
    //Bottom side cursor
    else if ((yMouse >= wHeight - PIXELS_TO_ACT) or allowToResize)
    {
        inResizeZone = true;
        setCursor(Qt::SizeVerCursor);
    
        resizeWindow(e);
    }
    //Top side cursor
    else if (yMouse <= PIXELS_TO_ACT or allowToResize)
    {
        inResizeZone = true;
        setCursor(Qt::SizeVerCursor);
    
        resizeWindow(e);
    }
    else
    {
        inResizeZone = false;
        setCursor(Qt::ArrowCursor);
    }
    
    e->accept();
    

    }

    void mainTrackbox::mousePressEvent(QMouseEvent *e)
    {
    if (e->button() == Qt::LeftButton)
    {
    if (inResizeZone)
    {
    allowToResize = true;

            if (e->pos().y() <= PIXELS_TO_ACT)
            {
                if (e->pos().x() <= PIXELS_TO_ACT)
                    resizeDiagTopLeft = true;
                else if (e->pos().x() >= geometry().width() - PIXELS_TO_ACT)
                    resizeDiagTopRight = true;
                else
                    resizeVerTop = true;
            }
            else if (e->pos().x() <= PIXELS_TO_ACT)
                resizeHorLeft = true;
        }
        else if (e->pos().x() >= PIXELS_TO_ACT and e->pos().x() < 50
                 and e->pos().y() >= PIXELS_TO_ACT and e->pos().y() < 20)
        {
            moveWidget = true;
            dragPosition = e->globalPos() - frameGeometry().topLeft();
        }
    }
    e->accept();
    

    }@



  • @void mainTrackbox::mouseReleaseEvent(QMouseEvent *e)
    {
    moveWidget = false;
    allowToResize = false;
    resizeVerSup = false;
    resizeHorEsq = false;
    resizeDiagSupEsq = false;
    resizeDiagSupDer = false;

    e->accept();
    

    }

    void mainTrackbox::moveWindow(QMouseEvent *e)
    {
    if (e->buttons() & Qt::LeftButton)
    {
    move(e->globalPos() - dragPosition);
    e->accept();
    }
    }

    void mainTrackbox::resizeWindow(QMouseEvent *e)
    {
    if (allowToResize)
    {
    int xMouse = e->pos().x();
    int yMouse = e->pos().y();
    int wWidth = geometry().width();
    int wHeight = geometry().height();

        if (cursor().shape() == Qt::SizeVerCursor)
        {
            if (resizeVerTop)
            {
                int newY = geometry().y() + yMouse;
                int newHeight = wHeight - yMouse;
    
                if (newHeight > minimumSizeHint().height())
                {
                    resize(wWidth, newHeight);
                    move(geometry().x(), newY);
                }
            }
            else
                resize(wWidth, yMouse+1);
        }
        else if (cursor().shape() == Qt::SizeHorCursor)
        {
            if (resizeHorLeft)
            {
                int newX = geometry().x() + xMouse;
                int newWidth = wWidth - xMouse;
    
                if (newWidth > minimumSizeHint().width())
                {
                    resize(newWidth, wHeight);
                    move(newX, geometry().y());
                }
            }
            else
                resize(xMouse, wHeight);
        }
        else if (cursor().shape() == Qt::SizeBDiagCursor)
        {
            int newX = 0;
            int newWidth = 0;
            int newY = 0;
            int newHeight = 0;
    
            if (resizeDiagTopRight)
            {
                newX = geometry().x();
                newWidth = xMouse;
                newY = geometry().y() + yMouse;
                newHeight = wHeight - yMouse;
            }
            else
            {
                newX = geometry().x() + xMouse;
                newWidth = wWidth - xMouse;
                newY = geometry().y();
                newHeight = yMouse;
            }
    
            if (newWidth >= minimumSizeHint().width() and newHeight >= minimumSizeHint().height())
            {
                resize(newWidth, newHeight);
                move(newX, newY);
            }
            else if (newWidth >= minimumSizeHint().width())
            {
                resize(newWidth, wHeight);
                move(newX, geometry().y());
            }
            else if (newHeight >= minimumSizeHint().height())
            {
                resize(wWidth, newHeight);
                move(geometry().x(), newY);
            }
        }
        else if (cursor().shape() == Qt::SizeFDiagCursor)
        {
            if (resizeDiagTopLeft)
            {
                int newX = geometry().x() + xMouse;
                int newWidth = wWidth - xMouse;
                int newY = geometry().y() + yMouse;
                int newHeight = wHeight - yMouse;
    
                if (newWidth >= minimumSizeHint().width() and newHeight >= minimumSizeHint().height())
                {
                    resize(newWidth, newHeight);
                    move(newX, newY);
                }
                else if (newWidth >= minimumSizeHint().width())
                {
                    resize(newWidth, wHeight);
                    move(newX, geometry().y());
                }
                else if (newHeight >= minimumSizeHint().height())
                {
                    resize(wWidth, newHeight);
                    move(geometry().x(), newY);
                }
            }
            else
                resize(xMouse+1, yMouse+1);
        }
    
        e->accept();
    }
    

    }

    bool mainTrackbox::eventFilter(QObject *obj, QEvent *event)
    {
    if (event->type() == QEvent::MouseMove)
    {
    QMouseEvent mouseEvent = static_cast<QMouseEvent>(event);
    mouseMoveEvent(mouseEvent);
    }
    return false;
    }@


Log in to reply
 

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