如何通过使用PyQt5将'Qt.WA_X11NetWmWindowTypeDesktop'属性应用于我的QML窗口

问题描述 投票:1回答:1

我正在桌面环境中工作,我希望将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_())

main.qml

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'对象没有属性'setAttribute'。第二次尝试成功运行了脚本,但是窗口没有打开。我认为可能是因为另一个桌面窗口正在运行。因此,我使用用户名登录到另一个控制台,创建了一个bash文件来运行窗口管理器和dektop窗口,然后通过startx / home / newtron / myscript启动它。它打开了一个空白窗口。甚至没有鼠标(可能是失败的)。

有人可以帮我吗?

谢谢。

python linux qml pyqt5 x11
1个回答
2
投票

Qt::WA_X11NetWmWindowTypeDesktopQt::WA_X11NetWmWindowTypeDesktop,因此它仅对QWidget有意义,并且似乎OP可以理解它,因为在尝试时,它使用了QMainWindow,但问题是QMainWindow没有显示QML,因此没有解决方案。

想法是在QML中放置一个QWidget,因此在这种情况下,有2种可能的解决方案:

© www.soinside.com 2019 - 2024. All rights reserved.