Where it is more correct to define the function that I plan to use in all the created windows (QWidget, QDialog, QMainWindow).

void MainWindow::moveToCenter() { QDesktopWidget desktop; QRect rect = desktop.availableGeometry(desktop.primaryScreen()); QPoint center = rect.center(); //координаты центра экрана center.setX(center.x() - (this->width()/2)); center.setY(center.y() - (this->height()/2)); move(center); } 

separate class and connect? Or kokogo that parent?

    1 answer 1

    If you plan to use it not only for the current window, but also for all the others, then make a common function, for example:

     void moveToCenter(QWidget& widget) { QDesktopWidget desktop; QRect rect = desktop.availableGeometry(desktop.primaryScreen()); QPoint center = rect.center(); //координаты центра экрана center.setX(center.x() - (widget.width()/2)); center.setY(center.y() - (widget.height()/2)); widget.move(center); } void moveToCenter(QWidget* widget) { moveToCenter(*widget); } 

    Well, use:

     QWidget *ww = new QWidget(); ww->show(); moveToCenter(ww); QWidget w; w.show(); moveToCenter(w); 

    This function can be rendered from MainWindow, or left in it, but made static

    • I did not understand all the same (Create these functions in the mainwindow. What do they have the same names like that? With use I understood everything) - Alexey Smirnov
    • You can create them in the mainwindow, you can outside the mainwindow. The names are the same but the parameters are different, this is called "overload". Allows you to use functions with the same name to work with different data - gil9red