如何从QML

时间:2017-07-14 03:07:07

标签: qt qml

我的应用程序有一个带有按钮的主窗口,当我单击按钮时,我使用createComponent创建Window {}的子类并显示它(纯粹用QML)。我正在我的macbook上运行应用程序,并附带另一台显示器。

如果我没有尝试设置新窗口的.x.y属性,那么它会显示在我的主窗口的顶部,主窗口是否在我的macbook上'屏幕或附加的监视器(即新窗口始终显示在与主窗口相同的屏幕上)。但是,如果我设置了新窗口的.x.y属性(根本不是任何值),那么无论我有哪个屏幕,新窗口都会显示在macbook屏幕上主窗口。

如何控制显示新窗口的可用屏幕?相关的,如何在屏幕上精确控制新窗口的位置(例如,如何让新窗口始终显示在右下角)?

编辑:基本代码。的 RemoteWindow.qml

Window {
    id: myWindow
    flags: Qt.Window | Qt.WindowTitleHint | Qt.WindowStaysOnTopHint 
        | Qt.WindowCloseButtonHint
    modality: Qt.NonModal
    height: 500
    width: 350

    // window contents, doesn't matter
}

在我的主窗口中,我有这个功能(remoteControl是一个保存对远程窗口的引用的属性):

function showRemoteWindow() {
    remoteControl.x = Screen.width - remoteControl.width
    remoteControl.y = Screen.height - remoteControl.height
    remoteControl.show()
}

同样在我的主窗口我有一个按钮,在onClicked:事件中我有这个代码:

if (remoteControl) {
    showRemoteWindow()
} else {
    var component = Qt.createComponent("RemoteWindow.qml")
    if (component.status === Component.Ready) {
        remoteControl = component.createObject(parent)
        showRemoteWindow() // window appears even without this call,
            // but calling this method to also set the initial position
    }
}

如果我在showRemoteWindow函数中注释掉.x.y的设置,那么我的RemoteWindow始终显示在与我的主窗口相同的屏幕中(macbook屏幕或连接的显示器)。但是,如果我将这两行留下未注释(或进行任何其他尝试设置窗口的x或y位置),那么我的RemoteWindow 总是出现在macbook屏幕上,无论我的主窗口是哪个屏幕是在。

1 个答案:

答案 0 :(得分:6)

就像@Blabdouze所说,现在Qt 5.9中有Window import QtQuick.Window 2.3 // the 2.3 is necessary Window { //... screen: Qt.application.screens[0] } 属性。 您可以为其分配screen数组的元素。

如果您想在第一个屏幕中显示一个窗口,您可以这样做:

x

将屏幕分配给窗口似乎将其定位在屏幕的中心。 如果您想精确控制窗口的位置,可以使用yscreen代替Window { //... screen: Qt.application.screens[0] //assigning the window to the screen is not needed, but it makes the x and y binding more readable x: screen.virtualX y: screen.virtualY + screen.height - height } 。例如,如果要在第一个屏幕的左下角显示一个窗口:

QList<QObject*> screens;
for (QScreen* screen : QGuiApplication::screens())
    screens.append(screen);
engine.rootContext()->setContextProperty("screens", QVariant::fromValue(screens));

如果你还没有使用Qt 5.9,你可以像c ++这样从c ++中公开screen数组:

virtualX

使用Qt.application.screens / geometry代替virtualY / x: screens[0].geometry.x 来访问屏幕的几何图形:

select * from (
SELECT o.object_id, o.name, 
Replace(Replace(Replace(SUBSTRING(m.definition,13,(CHARINDEX(CHAR(13),m.definition + CHAR(13)))-13),'[',''),']',''),'dbo.','') as definitionName
FROM sys.objects AS o
left join sys.sql_modules AS m on o.object_id = m.object_id
where o.type='v' )x
where name != definitionName