Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to move a Dialog when I move my QMainWindow
Forum Updated to NodeBB v4.3 + New Features

How to move a Dialog when I move my QMainWindow

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 2 Posters 2.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bdmontz
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • B Offline
        B Offline
        bdmontz
        wrote on last edited by
        #3

        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()));
        }
        
        1 Reply Last reply
        0
        • B Offline
          B Offline
          bdmontz
          wrote on last edited by
          #4

          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));
          }
          
          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

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

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved