Positioning a Dialog



  • A Dialog seems to appear on the screen at the location where the application launched, which might be very far away from the application's current location. Dialog doesn't have an "anchors" property, and setting its x and y properties results in a dialog with no content. Is there any way to control the position of a Dialog?



  • @xargs1

    I don't know if this is the "right" way to do it but acting on the onVisibleChanged signal lets you position the dialog. What's odd is that onVisibleChanged gets triggered twice for on open() call. I don't get that.

    You could also make the dialog modal – on OSX at least, it anchors it to the app window.

    import QtQuick 2.5
    import QtQuick.Controls 1.4
    import QtQuick.Dialogs 1.2
    
    
    ApplicationWindow {
        id: appWindow
        visible: true
        width: 800
        height: 600
    
    
        Item {
            Dialog {
                id: helloDialog
                modality:Qt.NonModal
    
                Label {
                    text: "Hello world!"
                }
    
                onVisibilityChanged: {
                    if (visible) {
                        console.log("visible", helloDialog.visible)
                        var xPos = (appWindow.x + appWindow.width /2) - helloDialog.width /2;
                        var yPos = (appWindow.y + appWindow.height /2) - helloDialog.height /2;
                        helloDialog.setX(xPos);
                        helloDialog.setY(yPos);
                    }
                }
            }
        }
    
        Button {
            text: "Open"
            onClicked: {
                helloDialog.open()
            }
        }
    }
    


  • That approach seems to work fine. It's strange that you need to jump through that extra hoop.

    Modality doesn't have any effect on Linux.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.