user2494298
user2494298

Reputation: 299

Qt QDialog moves location slightly each time it is closed and showed again

I have a QDialog and it pops up as a top level window in the center of my main window the first time it is opened(great, exactly what I want). However, each time I hit the "X" to close the window, when I trigger an event to cause the QDialog to pop up again(I use show() function for this)...the widget has slightly moved down and to the right(maybe 10 pixels each way, each time). Does anyone know what is causing this behavior/have a solution? Ideally I would like it to always pop up in the center of my main window(like it does the first time it opens).

Thanks in advance.

commandTimeWindow = new QDialog();
commandTimeWindow->resize(390, 180);
commandTimeWindow->setWindowTitle("Command In Progress");
commandTimeWindow->setStyleSheet("background-color: white;");
commandTimeWindow->setWindowFlags(Qt::WindowTitleHint |     Qt::WindowCloseButtonHint);
commandTimeWindow->setWindowIcon(QIcon(""));
commandTimeWindow->installEventFilter(this);


 commandTimeWindow->close();

 commandTimeWindow->show();

Also the standard "X" button closes the window...but I didnt write any code on this end.

Upvotes: 0

Views: 841

Answers (2)

user2494298
user2494298

Reputation: 299

I was able to solve my issue using the code below:

void MyMainWindow::moveEvent(QMoveEvent* event)
{
    const QPoint global = this->mapToGlobal(rect().center());
    waitDialog->move(global.x() - waitDialog->width() / 2, global.y() -   waitDialog->height() / 2);
}

Upvotes: 0

Andrej
Andrej

Reputation: 280

You can use the widget's move(x, y) method or replace resize with setGeometry.

Upvotes: 1

Related Questions