Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

set the QDockWidget's postion to eh center of the parent QMainWindow.



  • i want to set the QDockWidget's postion to eh center of the parent QMainWindow.
    the QDockWidget is created in the QMainWindow's constrctor, but i found that the QMainWindow's position and size are Not Fixed until the QMainWindow.show() is called.
    is there any way to avoid this?
    than you!


  • Lifetime Qt Champion

    Hi,

    You can use a slot that you connect to a single shot timer that you fire at the end of your main window constructor.



  • a single shot ? is there any examples ?
    thank you



  • QTimer::singleShot(<interval>, <receiver>, <slot>)


  • Lifetime Qt Champion



  • so what value should i set here : int msec... in my situation here



  • it seems that i should also put QMainWindow::showMaximized() in the QMainWindow's constructor, and before QTimer::singleShot(<interval>, <receiver>, <slot>)

    is it ok to put QMainWindow::showMaximized() in the QMainWindow's constructor, but not in the main.cpp ?



  • it's really bad, i just tested.
    i write like this at the end of the QMainWindow's constructor:
    ......
    showMaximized();
    QTimer::singleShot(0, this, SLOT(xxxx()));
    ......

    and in the xxxx(),
    i get the pos, and size
    the pos is (-8,-8), and size is (0,0)
    and the geometry i got is (0,23,1120,70).....so weired
    ............
    what is the normal way to center a window in the right Center of the QMainWindow?


  • Moderators

    class DockWidget : public QDockWidget {
    public:
        DockWidget(const QString& title, QWidget* parent) : QDockWidget(title, parent) {
            QTimer::singleShot(0, this, &DockWidget::centerInParent);
        }
        void centerInParent() {
            if(isVisible() && parentWidget()) {
                auto g = geometry();
                g.moveCenter(parentWidget()->geometry().center());
                setGeometry(g);
            }
        }
    };
    

    The single shot timer will work only if the dock is shown immediately. Otherwise you can call centerInParent() at any time.
    And then, for example in the main window constructor:

    auto dock = new DockWidget("Foo", this);
    dock->setFloating(true);
    

    Another programmer using your widget should be able to create it hidden if he wishes to. Don't call show*() in the constructors.



  • really weired, i do this as u told.
    but the parent is still 1120 width, 700 height. it's not right, as i showMaximized after the mainWindow created.


  • Lifetime Qt Champion

    What size should it have ?



  • bigger than 1120, my screen resolution.
    can i just get the screen resolution? and set the pos and size based on this for the QDock? i don't know if i can get the Right screen resolution in the MainWindow constructor...or the QDock's constructor
    thank you!


  • Lifetime Qt Champion

    QDesktopWidget can give you these information


Log in to reply