Unable to set geometry, when Widget is moved after it is shown
-
Hello everyone, I have next situation. I'm using QFileSystemWatcher to determine when the directory content has changed. If it has, then I allocate a widget of fixed size (400x400) and display it. The problem is, when I command to show the widget and afterwards I try to move the widget to the center of the screen I get next runtime message and widget is not displayed with the correct size:
setGeometry: Unable to set geometry 400x400+761+348 (frame: 422x456+750+303) on QWidgetWindow/"QWidgetClassWindow" on "\.\DISPLAY1". Resulting geometry: 602x603+764+362 (frame: 624x659+753+317) margins: 11, 45, 11, 11 minimum size: 400x400 maximum size: 400x400 MINMAXINFO maxSize=0,0 maxpos=0,0 mintrack=422,456 maxtrack=422,456)
This behavior is not observed if widget is allocated out of the slot called when the directory content changes.
Next code shows a minimal example of what I'm talking about. The solution to this is to first set the geometry of the widget and then display it (in case someone faces the same problem).
MainWindow::MainWindow(int argc,char **argv) : QApplication(argc, argv) { QFileSystemWatcher* sysWatcher = new QFileSystemWatcher(this); sysWatcher->addPath("C:\\PrintMe"); connect(sysWatcher,SIGNAL(directoryChanged(QString)), this,SLOT(allocateWidget())); // If widget is allocated out of slot there is no issue allocateWidget(); } void MainWindow::allocateWidget() { QWidget *w = new QWidget(0); w->setFixedSize(400, 400); w->show(); QRect g = QApplication::screens()[0]->availableGeometry(); w->move(g.center() - w->rect().center()); }
I got the solution by coincidence but I wonder if someone has an explanation for this behavior. I'm using Qt 5.15.2 and MinGW compiler.