繁体   English   中英

如何使用 PyQt5 将“Qt.WA_X11NetWmWindowTypeDesktop”属性应用于我的 QML 窗口

[英]How to apply 'Qt.WA_X11NetWmWindowTypeDesktop' attribute to my QML window by using PyQt5

我在桌面环境中工作,我希望我的 QML 窗口作为我的主桌面窗口。 我正在将 QML 与 PyQt5 集成。

这是我的代码

import sys
from PyQt5 import*
from PyQt5.QtCore import*
from PyQt5.QtWidgets import*
from PyQt5.QtQuick import*
from PyQt5.QtQml import*
from threading import Thread
import os
import importlib
import subprocess
import tempfile
import re
import random
import os.path
from os import path

dir_path = os.path.dirname(os.path.realpath(__file__))
if __name__ == '__main__':
    myApp = QApplication(sys.argv)

    engine = QQmlApplicationEngine()
    context = engine.rootContext()
    context.setContextProperty("main", engine)

    engine.load('/home/newtron/Muscovy/main.qml')

    window = QMainWindow()

    win = engine.rootObjects()[0]

    win.show()
    window.setAttribute(Qt.WA_X11NetWmWindowTypeDesktop, True)

    sys.exit(myApp.exec_())

主文件

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

ApplicationWindow {
    property var theme: String("#ffffff")
    property var focusColor: String('transparent')
    id: applicationWindow
    visible: true
    width: Screen.width
    height: Screen.height
    color: "#2d2b2b"
    title: qsTr("Muscovy")
    background: Image {
        id: image
        anchors.fill: parent
        source: "../Pictures/Wallpapers/113844.jpg"
        fillMode: Image.PreserveAspectCrop
    }
    PropertyAnimation{
        id: themeOpen
        target: rectangle
        duration: 200
        property: 'anchors.rightMargin'
        to: 0
    }
    PropertyAnimation{
        id: themeClose
        target: rectangle
        duration: 200
        property: 'anchors.rightMargin'
        to: -45
    }

    Loader {
        id: loader
        anchors.fill: parent
        source: 'main2.qml'
    }

    Rectangle {
        id: rectangle
        x: 1166
        y: 391
        width: 50
        height: 225
        color: "#68ffffff"
        radius: 25
        anchors.right: parent.right
        anchors.rightMargin: -45
        anchors.verticalCenter: parent.verticalCenter

        MouseArea {
            id: mouseArea
            anchors.fill: parent
            hoverEnabled: true
            onEntered: {
                themeOpen.start()
            }
            onExited: {
                themeClose.start()
            }
        }

        RoundButton {
            id: roundButton1
            x: 7
            y: 142
            anchors.top: parent.top
            anchors.topMargin: 142
            anchors.right: parent.right
            anchors.rightMargin: 7
            anchors.left: parent.left
            anchors.leftMargin: 7
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 48
            onClicked:{
                theme = String('#ffffff')
                loader.source = 'main2.qml'
            }
            background: Rectangle {
                id: rectan1
                height: 20
                color: "#434343"
                radius: parent.radius
                gradient: Gradient {
                    GradientStop {
                        position: 0.00;
                        color: "#00f7ff";
                    }
                    GradientStop {
                        position: 1.00;
                        color: "#0091ff";
                    }
                }
                anchors.fill: parent
                rotation: 45
            }
            Text {
                id: element1
                x: 5
                y: 8
                color: "#ffffff"
                text: qsTr("Li")
                anchors.horizontalCenter: parent.horizontalCenter
                anchors.verticalCenter: parent.verticalCenter
                font.pixelSize: 12
            }
        }

        RoundButton {
            id: roundButton
            x: 8
            y: 51
            anchors.top: parent.top
            anchors.topMargin: 51
            anchors.bottom: parent.bottom
            anchors.bottomMargin: 139
            anchors.left: parent.left
            anchors.leftMargin: 8
            anchors.right: parent.right
            anchors.rightMargin: 8
            onClicked:{
                theme = String('#000000')
                loader.source = 'main2.qml'
            }

            background: Rectangle{
                id:rectan
                height: 20
                rotation:45
                color: "#434343"
                anchors.fill: parent
                radius: parent.radius
                gradient: Gradient {
                    GradientStop {
                        position: 0
                        color: "#434343"
                    }

                    GradientStop {
                        position: 1
                        color: "#000000"
                    }
                }
            }

            Text {
                id: element
                x: 5
                y: 8
                color: "#ffffff"
                text: qsTr("Da")
                anchors.verticalCenter: parent.verticalCenter
                anchors.horizontalCenter: parent.horizontalCenter
                font.pixelSize: 12
            }
        }
    }
}

main2.qml

import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.12

Item {
    DropArea{
        anchors.fill: parent
        GridView{
            id: grid
            anchors.fill: parent
            clip: true
            interactive: false
            model: listContent
            cellHeight:90
            cellWidth:90
            delegate: Component{
                Rectangle {
                    id:rectID
                    width: 80
                    height: 80
                    color: "#002f99f6"
                    radius: 5
                    border.color: "#2f99f6"
                    border.width: 0

                    MouseArea {
                        property var focused: false
                        property var childX: parent.x/90
                        property var childY: parent.y/90
                        signal toggled
                        drag.target: rectID
                        onToggled: {
                            if(focused == true){
                                parent.color = "#4f2f99f6"
                                parent.border.width = 1
                                focused = false
                            }
                            else{
                                parent.color = "transparent"
                                parent.border.width = 0
                            }
                        }

                        objectName: objName
                        signal doubClicked
                        anchors.fill: parent
                        hoverEnabled: true
                        Component.onCompleted: {
                            grid.clipChanged.connect(toggled)
                        }
                        onEntered: {
                            if(focused != true){
                                parent.color = "#4cffffff"
                                textID.elide = Text.ElideNone
                                textID.wrapMode = Text.WrapAnywhere
                                parent.z = 10000
                            }
                        }
                        onExited:{
                            if(focused != true){
                                parent.color = "transparent"
                                textID.elide = Text.ElideRight
                                textID.wrapMode = Text.NoWrap
                                parent.z = 5
                            }
                        }
                        onClicked: {
                            focused = true
                            if(grid.clip == true)
                                grid.clip = false
                            else
                                grid.clip = true
                        }
                        onReleased: {
                            console.log(childX.toFixed(0))
                            console.log(childY.toFixed(0))
                            parent.x = childX.toFixed(0)*90
                            parent.y = childY.toFixed(0)*90
                        }

                        onDoubleClicked: {
                            doubClicked()
                        }

                        Image {
                            x: 8
                            y: 0
                            width: 64
                            height: 64
                            source: imgSource
                            anchors.top: parent.top
                            anchors.topMargin: 0
                            anchors.horizontalCenter: parent.horizontalCenter
                        }

                        Text {
                            id:textID
                            x: 8
                            y: 47
                            color: theme
                            text: fileName
                            fontSizeMode: Text.FixedSize
                            verticalAlignment: Text.AlignTop
                            font.weight: Font.Normal
                            style: Text.Normal
                            elide: Text.ElideRight
                            wrapMode: Text.NoWrap
                            horizontalAlignment: Text.AlignHCenter
                            textFormat: Text.AutoText
                            anchors.left: parent.left
                            anchors.leftMargin: 0
                            anchors.right: parent.right
                            anchors.rightMargin: 0
                            anchors.top: parent.top
                            anchors.topMargin: 78
                            font.pixelSize: 12
                            clip: true
                        }
                    }
                }
            }
        }
        ListModel{

             ListElement{ objName:"objectanyname"; fileName:"anyname"; imgSource:"unknown.png"; textID:"text11";rectID:"rect1"}
             ListElement{ objName:"objectany.py"; fileName:"any.py"; imgSource:"unknown.png"; textID:"text12";rectID:"rect2"}
             ListElement{ objName:"objectanyt.py"; fileName:"anyt.py"; imgSource:"unknown.png"; textID:"text13";rectID:"rect3"}
             ListElement{ objName:"objectbash"; fileName:"bash"; imgSource:"unknown.png"; textID:"text14";rectID:"rect4"}
             ListElement{ objName:"objectbash.py"; fileName:"bash.py"; imgSource:"unknown.png"; textID:"text15";rectID:"rect5"}
             ListElement{ objName:"objectbash.sh"; fileName:"bash.sh"; imgSource:"unknown.png"; textID:"text16";rectID:"rect6"}
             ListElement{ objName:"objectBecalm.desktop"; fileName:"Becalm.desktop"; imgSource:"unknown.png"; textID:"text17";rectID:"rect7"}
             ListElement{ objName:"objectbuild-datetime-Desktop_Qt_5_12_5_GCC_64bit-Debug"; fileName:"build-datetime-Desktop_Qt_5_12_5_GCC_64bit-Debug"; imgSource:"folder.png"; textID:"text18";rectID:"rect8"}
             ListElement{ objName:"objectcnspec.desktop"; fileName:"cnspec.desktop"; imgSource:"unknown.png"; textID:"text19";rectID:"rect9"}
             ListElement{ objName:"objectCounter-Strike_Global_Offensive.desktop"; fileName:"Counter-Strike Global Offensive.desktop"; imgSource:"unknown.png"; textID:"text110";rectID:"rect10"}
             ListElement{ objName:"objecteFootball_PES_2020.desktop"; fileName:"eFootball PES 2020.desktop"; imgSource:"unknown.png"; textID:"text111";rectID:"rect11"}
             ListElement{ objName:"objectfirstprogramme"; fileName:"firstprogramme"; imgSource:"folder.png"; textID:"text112";rectID:"rect12"}
             ListElement{ objName:"objectHimno.desktop"; fileName:"Himno.desktop"; imgSource:"unknown.png"; textID:"text113";rectID:"rect13"}
             ListElement{ objName:"objectmain.py"; fileName:"main.py"; imgSource:"unknown.png"; textID:"text114";rectID:"rect14"}
             ListElement{ objName:"objectname.py"; fileName:"name.py"; imgSource:"unknown.png"; textID:"text115";rectID:"rect15"}
             ListElement{ objName:"objectNUCLEAR"; fileName:"NUCLEAR"; imgSource:"folder.png"; textID:"text116";rectID:"rect16"}
             ListElement{ objName:"object__pycache__"; fileName:"__pycache__"; imgSource:"folder.png"; textID:"text117";rectID:"rect17"}
             ListElement{ objName:"objectS.A.I.A.'s_Awakening_A_Robothorium_Visual_Novel.desktop"; fileName:"S.A.I.A.'s Awakening A Robothorium Visual Novel.desktop"; imgSource:"unknown.png"; textID:"text118";rectID:"rect18"}
             ListElement{ objName:"objectsemi2.odp"; fileName:"semi2.odp"; imgSource:"unknown.png"; textID:"text119";rectID:"rect19"}
             ListElement{ objName:"objectSeminar2.pptx"; fileName:"Seminar2.pptx"; imgSource:"unknown.png"; textID:"text120";rectID:"rect20"}
             ListElement{ objName:"objectsemi.odp"; fileName:"semi.odp"; imgSource:"unknown.png"; textID:"text121";rectID:"rect21"}
             ListElement{ objName:"objectsemi.pptx"; fileName:"semi.pptx"; imgSource:"unknown.png"; textID:"text122";rectID:"rect22"}
             ListElement{ objName:"objectStar_Conflict.desktop"; fileName:"Star Conflict.desktop"; imgSource:"unknown.png"; textID:"text123";rectID:"rect23"}
             ListElement{ objName:"objectsteam.desktop"; fileName:"steam.desktop"; imgSource:"unknown.png"; textID:"text124";rectID:"rect24"}
             ListElement{ objName:"objecttest.py"; fileName:"test.py"; imgSource:"unknown.png"; textID:"text125";rectID:"rect25"}
             ListElement{ objName:"objectUntitled.png"; fileName:"Untitled.png"; imgSource:"unknown.png"; textID:"text126";rectID:"rect26"}
             ListElement{ objName:"objectUntitled.xcf"; fileName:"Untitled.xcf"; imgSource:"unknown.png"; textID:"text127";rectID:"rect27"}
             ListElement{ objName:"objectWarplanes_WW1_Sky_Aces.desktop"; fileName:"Warplanes WW1 Sky Aces.desktop"; imgSource:"unknown.png"; textID:"text128";rectID:"rect28"}
             ListElement{ objName:"objectZombie_Grinder_Dedicated_Server.desktop"; fileName:"Zombie Grinder Dedicated Server.desktop"; imgSource:"unknown.png"; textID:"text129";rectID:"rect29"}            id:listContent


        }
    }
}

我尝试在代码中添加window.setAttribute(Qt.WA_X11NetWmWindowTypeDesktop, True)但它不起作用。 我也尝试添加 Qt.Desktop 标志,这也不起作用。 第一次,它给了我错误 AttributeError: 'QQuickWindow' object has no attribute 'setAttribute'。 在第二次尝试中,脚本成功运行,但窗口没有打开。 我认为可能是因为另一个桌面窗口正在运行。 所以我用我的用户名登录到另一个控制台,创建了一个 bash 文件来运行窗口管理器和桌面窗口,然后通过 startx /home/newtron/myscript 启动它。 它打开了一个空白窗口。 甚至没有鼠标(可能是失败的)。

有谁能够帮助我?

谢谢。

Qt::WA_X11NetWmWindowTypeDesktopQt::WidgetAttribute所以它只对 QWidget 有意义,而且 OP 似乎理解它,因为在它的尝试中它使用了 QMainWindow 但问题是 QMainWindow 不显示 QML 所以不是解决方案。

这个想法是在 QML 所在的地方有一个 QWidget,所以在这种情况下有两种可能的解决方案:

  1. 在 ApplicationWindow 中设置visible: false并使用QWidget::createWindowContainer()

     import os import sys from PyQt5.QtCore import Qt, QUrl from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtQml import QQmlApplicationEngine DIR_PATH = os.path.dirname(os.path.realpath(__file__)) if __name__ == "__main__": myApp = QApplication(sys.argv) file = os.path.join(DIR_PATH, "main.qml") url = QUrl.fromLocalFile(file) engine = QQmlApplicationEngine() context = engine.rootContext() context.setContextProperty("main", engine) engine.load(url) if not engine.rootObjects(): sys.exit(-1) widget = QWidget.createWindowContainer(engine.rootObjects()[0]) widget.setAttribute(Qt.WA_X11NetWmWindowTypeDesktop, True) widget.showFullScreen() sys.exit(myApp.exec_())

    主文件

    // ... ApplicationWindow { property var theme: String("#ffffff") property var focusColor: String('transparent') id: applicationWindow visible: false width: Screen.width // ...

  2. QQuickViewQWidget::createWindowContainer()

     import os import sys from PyQt5.QtCore import Qt, QUrl from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtQuick import QQuickView DIR_PATH = os.path.dirname(os.path.realpath(__file__)) if __name__ == "__main__": myApp = QApplication(sys.argv) file = os.path.join(DIR_PATH, "main.qml") url = QUrl.fromLocalFile(file) view = QQuickView() def on_statusChanged(status): if status == QQuickView.Error: for error in view.errors(): print(error.toString()) sys.exit(-1) view.statusChanged.connect(on_statusChanged) view.setResizeMode(QQuickView.SizeRootObjectToView) engine = view.engine() context = engine.rootContext() context.setContextProperty("main", engine) view.setSource(url) view.setTitle(view.tr("Muscovy")) widget = QWidget.createWindowContainer(view) widget.setAttribute(Qt.WA_X11NetWmWindowTypeDesktop, True) widget.showMaximized() sys.exit(myApp.exec_())

    主文件

    import QtQuick 2.12 import QtQuick.Controls 2.12 Rectangle { property var theme: String("#ffffff") property var focusColor: String('transparent') color: "#2d2b2b" Image { id: image anchors.fill: parent source: "../Pictures/Wallpapers/113844.jpg" fillMode: Image.PreserveAspectCrop } PropertyAnimation{ id: themeOpen target: rectangle // ...
  3. QQuickWidget :

     import os import sys from PyQt5.QtCore import Qt, QUrl from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtQuickWidgets import QQuickWidget DIR_PATH = os.path.dirname(os.path.realpath(__file__)) if __name__ == "__main__": myApp = QApplication(sys.argv) file = os.path.join(DIR_PATH, "main.qml") url = QUrl.fromLocalFile(file) widget = QQuickWidget() widget.resize(640, 480) def on_statusChanged(status): if status == QQuickWidget.Error: for error in view.errors(): print(error.toString()) sys.exit(-1) widget.statusChanged.connect(on_statusChanged) widget.setResizeMode(QQuickWidget.SizeRootObjectToView) engine = widget.engine() context = engine.rootContext() context.setContextProperty("main", engine) widget.setSource(url) widget.setWindowTitle(widget.tr("Muscovy")) widget.setAttribute(Qt.WA_X11NetWmWindowTypeDesktop, True) widget.showMaximized() sys.exit(myApp.exec_())

    主文件

    import QtQuick 2.12 import QtQuick.Controls 2.12 Rectangle { property var theme: String("#ffffff") property var focusColor: String('transparent') color: "#2d2b2b" Image { id: image anchors.fill: parent source: "../Pictures/Wallpapers/113844.jpg" fillMode: Image.PreserveAspectCrop } PropertyAnimation{ id: themeOpen target: rectangle // ...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM