How to move a Dialog when I move my QMainWindow



  • I am using a Dialog and a QMainWindow, I would like to make it so that when I move the Main window around, the Dialog moves in unison, so it appears stuck to that relative spot. I can't find a signal from QMainWindow that would work with this, and I don't know what I would need to do for subclassing (or whatever it's called, when I make a new function to overwrite a built in function so I can add an event).

    So you can see what I'm trying to do. Here is a gif of it not working.
    http://imgur.com/a/hylXC

    What is the best way to make it do what I want?


  • Lifetime Qt Champion

    Hi,

    You can use the moveEvent method to track the position changes of your main window and move your other widget accordingly.

    Hope it helps



  • Thanks!

    Maybe you can help me out with this new bug, as I'm very new with Events, so I'm not sure why this isn't working. The math seems to work out, but something is not working the way I expect.

    When I move the main window the dialog slides gradually. If I shake the main window I can watch it drift around my screen. It doesn't break anything, but I'd like it to appear stuck to the main window.

    Any idea what's going on and how to fix it?

    void MainWindow::colorKeyButton_pressed()
    {
        if(this->findChild<ColorKey*>("colorKeyDialog"))
            return;
        ColorKey colorKeyDialog(this);
    
        colorKeyDialog.setObjectName("colorKeyDialog");
        colorKeyDialog.move(this->pos().x()+75,this->pos().y()+516);
        colorKeyDialog.show();
        colorKeyDialog.exec();
    }
    
    void MainWindow::moveEvent(QMoveEvent * event)
    {
        QMainWindow::moveEvent( event );
        if(!this->findChild<ColorKey*>("colorKeyDialog"))
        {
            return;
        }
        ColorKey *colorKeyDialog = this->findChild<ColorKey*>("colorKeyDialog");
        colorKeyDialog->move(colorKeyDialog->pos().x()+(event->pos().x() - event->oldPos().x()),colorKeyDialog->pos().y()+(event->pos().y() - event->oldPos().y()));
    }
    


  • tried moving it around, but it didn't change anything, other than readability.

    void MainWindow::moveEvent(QMoveEvent * event)
    {
    
        int oldX, newX, oldY, newY;
        oldX = event->oldPos().x();
        newX = event->pos().x();
        oldY = event->oldPos().y();
        newY = event->pos().y();
    
        QMainWindow::moveEvent( event );
    
        if(!this->findChild<ColorKey*>("colorKeyDialog"))
        {
            return;
        }
        ColorKey *colorKeyDialog = this->findChild<ColorKey*>("colorKeyDialog");
        colorKeyDialog->move(colorKeyDialog->pos().x()+(newX - oldX),colorKeyDialog->pos().y()+(newY - oldY));
    }
    

  • Lifetime Qt Champion

    Something's not clear, did you get it working since you marked the thread as solved ?


Log in to reply
 

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