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

Window widget not moving with move()



  • hello

    i am trying to move a window with move() inside a MDIarea but that is not working, here is the code :

    wCon = new myWindow(this);
    wCon->setAttribute(Qt::WA_DeleteOnClose);
    ui->mdiArea->addSubWindow(wCon);
    wCon->setFixedSize(200,200);
    int x, y;
        x = (this->width() - wCon->width())/2;
        y = (this->height() - wCon->height())/2;
    wCon->move(x,y);
    wCon->show();
    // this = mainWindow that contains the MDI
    // myWindow = QWidget class
    // also tried : wCon->move(wCon->rect().center());
    

    the new window stays at top left of the MDIarea and does not move to the new location (center of MDI area).

    Any idea why ?



  • Hi,

    @Xena_o said in Window widget not moving with move():

    wCon->move(x,y);

    you have to move your widget as QMdiSubWindow not as QWidget (otherwise it moves the widget content inside your MdiWindow and not the whole MdiWindow in your MdiArea.)

    Try mdiArea->activeSubWindow()->move(x, y);
    (or currentSubWindow())



  • @Pl45m4 thank you !

    with your help i could figure out how the code should be:

    wCon = new myWindow(this);
    
        QMdiSubWindow *cw = new QMdiSubWindow;
        cw->setWidget(wCon);
    
        //destroy window on close
        cw->setAttribute(Qt::WA_DeleteOnClose);
    
        //add window to MDI area
         ui->mdiArea->addSubWindow(cw);
    
        //put window in middle of MDI
        cw->move(wCon->rect().center());
    
        //show the window
        cw->show();
    

    this code works well ! thanks !


Log in to reply