- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在桌面环境中工作,我希望我的 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' object has no attribute 'setAttribute'。在第二次尝试中,脚本成功运行,但窗口未打开。我认为这可能是因为另一个桌面窗口正在运行。因此,我使用我的用户名登录到另一个控制台,创建了一个 bash 文件来运行窗口管理器和桌面窗口,然后通过 startx/home/newtron/myscript 启动它。它打开了一个空白窗口。连鼠标都没有(可能是失败)。
有人可以帮我吗?
谢谢。
最佳答案
Qt::WA_X11NetWmWindowTypeDesktop
是 Qt::WidgetAttribute
所以它只对 QWidget 有意义,并且 OP 似乎理解它,因为在尝试中它使用 QMainWindow 但问题是 QMainWindow 不显示 QML,所以这不是解决方案。
这个想法是在 QML 所在的位置创建一个 QWidget,因此在这种情况下有 2 种可能的解决方案:
在 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_())
main.qml
// ...
ApplicationWindow {
property var theme: String("#ffffff")
property var focusColor: String('transparent')
id: applicationWindow
<b>visible: false</b>
width: Screen.width
// ...
QQuickView
与 QWidget::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_())
main.qml
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
// ...
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_())
main.qml
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
// ...
关于python - 如何使用 PyQt5 将 'Qt.WA_X11NetWmWindowTypeDesktop' 属性应用到我的 QML 窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59803377/
https://github.com/mattdiamond/Recorderjs/blob/master/recorder.js中的代码 我不明白 JavaScript 语法,比如 (functio
在 iOS 7 及更早版本中,如果我们想在应用程序中找到 topMostWindow,我们通常使用以下代码行 [[[UIApplication sharedApplication] windows]
我已经尝试解决这个问题很长一段时间了:我无法访问窗口的 url,因为它位于另一个域上..有一些解决方案吗? function login() { var cb = window.ope
是否可以将 FFMPEG 视频流传递到 C# 窗口?现在它在新窗口中作为新进程打开,我只是想将它传递给我自己的 SessionWindow。 此时我像这样执行ffplay: public void E
我有一个名为 x 的矩阵看起来像这样: pTime Close 1 1275087600 1.2268 2 1275264000 1.2264 3 1275264300 1.2
在编译时,发生搜索,grep搜索等,Emacs会在单独的窗口中创建一个新的缓冲区来显示结果,有没有自动跳转到那个窗口的方法?这很有用,因为我可以使用 n 和 p 而不是 M-g n 和 M-g p 移
我有一个启动 PowerShell 脚本的批处理文件。 批处理文件: START Powershell -executionpolicy RemoteSigned -noexit -file "MyS
我有一个基于菜单栏的应用程序,单击图标时会显示一个窗口。在 Mac OS X Lion 上一切正常,但由于某种原因,在 Snow Leopard 和早期版本的 Mac OS X 上会出现错误。任何时候
在 macOS 中,如何在 Xcode 和/或 Interface Builder 中创建带有“集成标题栏和工具栏”的窗口? 这是“宽标题栏”类型的窗口,已添加到 OS X 10.10 Yosemit
在浏览器 (Chrome) 中 JavaScript: var DataModler = { Data: { Something: 'value' }, Process: functi
我有 3 个 html 页面。第 1 页链接到第 2 页,第 2 页链接到第 3 页(为了简单起见)。 我希望页面 2 中的链接打开页面 3 并关闭页面 1(选项卡 1)。 据我了解,您无法使用 Ja
当点击“创建节点”按钮时,如何打开一个新的框架或窗口?我希望新框架包含一个文本字段和下拉菜单,以便用户可以选择一个选项。 Create node Search node
我有一个用户控件,用于编辑应用程序中的某些对象。 我最近遇到一个实例,我想弹出一个新的对话框(窗口)来托管此用户控件。 如何实例化新窗口并将需要设置的任何属性从窗口传递到用户控件? 感谢您的宝贵时间。
我有一个Observable,它发出许多对象,我想使用window或buffer操作对这些对象进行分组。但是,我不想指定count参数来确定窗口中应包含多少个对象,而是希望能够使用自定义条件。 例如,
我有以下代码,它打开一个新的 JavaFX 阶段(我们称之为窗口)。 openAlertBox.setOnAction(e -> { AlertBox alert = AlertBox
我要添加一个“在新窗口中打开”上下文菜单项,该菜单项将以新的UIScene打开我的应用程序文档之一。当然,我只想在实际上支持多个场景的设备上显示该菜单项。 目前,我只是在检查设备是否是使用旧设备的iP
我正在尝试创建一个 AIR 应用程序来记录应用程序的使用情况,使用 AIR 从系统获取信息的唯一简单方法是使用命令行工具和抓取 标准输出 . 我知道像 这样的工具顶部 和 ps 对于 OS X,但它们
所以我有这个简单的 turtle 螺旋制作器,我想知道是否有一种方法可以打印出由该程序创建的我的设计副本。 代码: import turtle x= float(input("Angle: ")) y
我正在编写一个 C# WPF 程序,它将文本消息发送到另一个程序的窗口。我有一个宏程序作为我的键盘驱动程序 (Logitech g15) 的一部分,它已经这样做了,尽管它不会将击键直接发送到进程,而是
我尝试使用以下代码通过 UDP 发送,但得到了奇怪的结果。 if((sendto(newSocket, sendBuf, totalLength, 0, (SOCKADDR *)&sendAd
我是一名优秀的程序员,十分优秀!