- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
从 QML 调用 Python 函数的正确方法是什么,该函数将实例化并将对象返回给 QML,并使 QML 端负责对象的生命周期?我面临的问题是对象在到达 QML 之前就被垃圾收集了。
请注意,我不想在 Python 端保留对对象的显式引用,根据以下来自 the documentation 的引用我正在寻找的甚至应该是默认行为:
When data is transferred from C++ to QML, the ownership of the data always remains with C++. The exception to this rule is when a QObject is returned from an explicit C++ method call: in this case, the QML engine assumes ownership of the object...
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import signal
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtCore import QUrl, QObject, QVariant, pyqtSlot
from PyQt5.QtQml import QQmlEngine, qmlRegisterType
class Dummy(QObject):
def __del__(self):
print("Deleted")
class GUIEntryPoint(QObject):
@pyqtSlot(result=QVariant)
def get_foo(self):
foo = Dummy()
print("Created {}".format(foo))
print("Ownership after instantiation: {}".format(QQmlEngine.objectOwnership(foo)))
#QQmlEngine.setObjectOwnership(foo, 1) # has no effect
return foo
@pyqtSlot(QVariant)
def print_foo(self, foo):
print("{}, ownership: {}".format(foo, QQmlEngine.objectOwnership(foo)))
def run():
signal.signal(signal.SIGINT, signal.SIG_DFL)
app = QGuiApplication(sys.argv)
# these don't seem to make a difference
qmlRegisterType(GUIEntryPoint, 'GUIEntryPoint', 1, 0, 'GUIEntryPoint')
qmlRegisterType(Dummy, 'Dummy', 1, 0, 'Dummy')
qml_url = QUrl.fromLocalFile(os.path.join('zzz.qml'))
view = QQuickView()
gep = GUIEntryPoint()
view.rootContext().setContextProperty('BE', gep)
view.setSource(qml_url)
view.show()
sys.exit(app.exec_())
if __name__ == '__main__':
run()
import QtQuick 2.2
Rectangle {
id: rootItem
width: 640
height: 400
property var pleaseKeepMe: BE.get_foo()
Component.onCompleted: {
console.log("Completed");
BE.print_foo(pleaseKeepMe); // prints None, it has been deleted
console.log("creating another");
var x = BE.get_foo();
// at this point x has already been deleted on the Python side
console.log("created another");
BE.print_foo(x); // prints None
console.log("\n\nPress CTRL-C to exit");
}
}
最佳答案
简而言之:使用 SIP Python API 将所有权转移到 C++,同时保持 QObject 的 Python“部分”事件似乎解决了这个问题,这确实是一个所有权问题,如(另请参阅下面来源中的评论)所述:
from PyQt5.QtGui import QGuiApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtCore import QUrl, QObject, QVariant, pyqtSlot
from PyQt5.QtQml import QQmlEngine, qmlRegisterType
import sip
def ownership(obj):
jsOwned = QQmlEngine.objectOwnership(obj) == QQmlEngine.JavaScriptOwnership
return "Javascript" if jsOwned else "Cpp"
def onDummytDestroyed():
print "Dummy destroyed"
class Dummy(QObject):
i = 0
def __init__(self):
#let's not forget to properly initialize the base part
super(Dummy, self).__init__()
self.i = Dummy.i
Dummy.i = Dummy.i + 1
def __del__(self):
#let's track when the Python part gets destroyed
# (or, rather, "finalized", or whatever the correct term is :) )
print "Dummy.__del__(), self.i:", self.i
@pyqtSlot(result="QString")
def get_str(self):
#let's have a Python (not C++) implemented method in the class
return "Dummy string #" + str(self.i)
class GUIEntryPoint(QObject):
def __init__(self):
#same as with Dummy.__init__
super(GUIEntryPoint, self).__init__()
@pyqtSlot(result=QVariant) #would also work
#@pyqtSlot(result=QObject)
def get_foo(self):
print "GUIEntryPoint.get_foo():"
foo = Dummy()
foo.setObjectName("Foo")
#let's track when the C++ part gets destroyed
foo.destroyed.connect(onDummytDestroyed)
#this would destroy the Python part of foo as soon as the reference
#goes out of scope (the C++ part stay alive and will be properly accessible from QML,
#but attempts to access Python-implemented methods like Dummy.get_str()
#would cause a TypeError hinting that there is no such method)
#sip.transferto(foo, None)
#this works as expected, with both Python (and then C++) parts of foo
#being destroyed whenever the QML GC decides to get rid of the object
#(see program output, notice how GC is silent for a while)
sip.transferto(foo, foo)
#this is indeed not needed, as proper ownership will be set
#when the object passes to the QML land through the slot invocation
#QQmlEngine.setObjectOwnership(foo, QQmlEngine.JavaScriptOwnership)
print " initial ownership: ", ownership(foo)
return foo
@pyqtSlot(QObject) #would also work
#@pyqtSlot(QVariant)
def print_foo(self, foo):
print "GUIEntryPoint.print_foo():"
print " objectName: ", foo.objectName()
print " ownership when on QML side: ", ownership(foo)
app = QGuiApplication([])
view = QQuickView()
#not needed, indeed, as both context objects (and properties),
#and QObject descendants passed to QML side are perfectly accessible
#(that is, their properties and invokables / slots are)
#without any additional steps
#(instantiating types declaratively would require registering them first, though,
#and having properties of custom types requires metatypes to be declared)
#qmlRegisterType(GUIEntryPoint, 'GUIEntryPoint', 1, 0, 'GUIEntryPoint')
#qmlRegisterType(Dummy, 'Dummy', 1, 0, 'Dummy')
gep = GUIEntryPoint()
view.rootContext().setContextProperty('BE', gep)
view.setSource(QUrl.fromLocalFile("pyqt-ownership-test.qml"))
view.show()
app.exec_()
import QtQuick 2.1
Rectangle {
width: 300
height: 200
color: "red"
border.width: 10
Component.onCompleted: {
console.log("Component.onCompleted():")
//increase the number to 1000 to see GC stepping in
//while the loop is still running
//(notice how destruction is not being reported for some objects,
//for example, #249 (the one created last),
//but this looks like a Qt/QML logging issue)
for (var i = 0; i < 250; i++) {
console.log("#" + i)
var foo = BE.get_foo()
console.log(" foo: " + foo)
console.log(" foo.objectName: " + foo.objectName)
console.log(" foo.get_str(): " + foo.get_str())
console.log(" print_foo():")
BE.print_foo(foo)
}
}
}
Component.onCompleted():
#0
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x22903d0, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #0
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#1
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2290d20, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #1
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#2
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2291080, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #2
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#3
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x22911e0, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #3
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#4
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2291e90, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #4
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#5
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2292b70, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #5
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#6
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2293420, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #6
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#7
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2296e30, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #7
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#8
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x22983b0, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #8
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#9
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2299150, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #9
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#10
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x2299e60, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #10
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#11
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x229ab70, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #11
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
<...some lines skipped here...>
#245
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x23007b0, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #245
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#246
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x22fe4d0, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #246
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#247
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x22fc5c0, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #247
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#248
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x22fa730, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #248
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
#249
GUIEntryPoint.get_foo():
initial ownership: Cpp
foo: Dummy(0x22f57b0, "Foo")
foo.objectName: Foo
foo.get_str(): Dummy string #249
print_foo():
GUIEntryPoint.print_foo():
objectName: Foo
ownership when on QML side: Javascript
Dummy.__del__(), self.i: 214
Dummy destroyed
Dummy.__del__(), self.i: 213
Dummy destroyed
Dummy.__del__(), self.i: 212
Dummy destroyed
Dummy.__del__(), self.i: 211
Dummy destroyed
Dummy.__del__(), self.i: 210
Dummy destroyed
Dummy.__del__(), self.i: 209
Dummy destroyed
Dummy.__del__(), self.i: 208
Dummy destroyed
Dummy.__del__(), self.i: 207
Dummy destroyed
Dummy.__del__(), self.i: 206
Dummy destroyed
Dummy.__del__(), self.i: 205
Dummy destroyed
Dummy.__del__(), self.i: 204
Dummy destroyed
Dummy.__del__(), self.i: 203
Dummy destroyed
Dummy.__del__(), self.i: 202
Dummy destroyed
Dummy.__del__(), self.i: 201
Dummy destroyed
Dummy.__del__(), self.i: 200
Dummy destroyed
Dummy.__del__(), self.i: 199
Dummy destroyed
Dummy.__del__(), self.i: 198
Dummy destroyed
Dummy.__del__(), self.i: 197
Dummy destroyed
Dummy.__del__(), self.i: 196
Dummy destroyed
Dummy.__del__(), self.i: 195
Dummy destroyed
Dummy.__del__(), self.i: 194
Dummy destroyed
Dummy.__del__(), self.i: 193
Dummy destroyed
Dummy.__del__(), self.i: 192
Dummy destroyed
Dummy.__del__(), self.i: 191
Dummy destroyed
Dummy.__del__(), self.i: 190
Dummy destroyed
Dummy.__del__(), self.i: 189
Dummy destroyed
Dummy.__del__(), self.i: 188
Dummy destroyed
Dummy.__del__(), self.i: 187
Dummy destroyed
Dummy.__del__(), self.i: 186
Dummy destroyed
Dummy.__del__(), self.i: 185
Dummy destroyed
Dummy.__del__(), self.i: 184
Dummy destroyed
Dummy.__del__(), self.i: 183
Dummy destroyed
Dummy.__del__(), self.i: 182
Dummy destroyed
Dummy.__del__(), self.i: 181
Dummy destroyed
Dummy.__del__(), self.i: 180
Dummy destroyed
Dummy.__del__(), self.i: 179
Dummy destroyed
Dummy.__del__(), self.i: 178
Dummy destroyed
Dummy.__del__(), self.i: 177
Dummy destroyed
Dummy.__del__(), self.i: 176
Dummy destroyed
Dummy.__del__(), self.i: 175
Dummy destroyed
Dummy.__del__(), self.i: 174
Dummy destroyed
Dummy.__del__(), self.i: 173
Dummy destroyed
Dummy.__del__(), self.i: 172
Dummy destroyed
Dummy.__del__(), self.i: 171
Dummy destroyed
Dummy.__del__(), self.i: 170
Dummy destroyed
Dummy.__del__(), self.i: 169
Dummy destroyed
Dummy.__del__(), self.i: 168
Dummy destroyed
Dummy.__del__(), self.i: 167
Dummy destroyed
Dummy.__del__(), self.i: 166
Dummy destroyed
Dummy.__del__(), self.i: 165
Dummy destroyed
Dummy.__del__(), self.i: 164
Dummy destroyed
Dummy.__del__(), self.i: 163
Dummy destroyed
Dummy.__del__(), self.i: 162
Dummy destroyed
Dummy.__del__(), self.i: 161
Dummy destroyed
Dummy.__del__(), self.i: 160
Dummy destroyed
Dummy.__del__(), self.i: 159
Dummy destroyed
Dummy.__del__(), self.i: 158
Dummy destroyed
Dummy.__del__(), self.i: 157
Dummy destroyed
Dummy.__del__(), self.i: 156
Dummy destroyed
Dummy.__del__(), self.i: 155
Dummy destroyed
Dummy.__del__(), self.i: 154
Dummy destroyed
Dummy.__del__(), self.i: 153
Dummy destroyed
Dummy.__del__(), self.i: 152
Dummy destroyed
Dummy.__del__(), self.i: 151
Dummy destroyed
Dummy.__del__(), self.i: 150
Dummy destroyed
Dummy.__del__(), self.i: 149
Dummy destroyed
Dummy.__del__(), self.i: 148
Dummy destroyed
Dummy.__del__(), self.i: 147
Dummy destroyed
Dummy.__del__(), self.i: 146
Dummy destroyed
Dummy.__del__(), self.i: 145
Dummy destroyed
Dummy.__del__(), self.i: 144
Dummy destroyed
Dummy.__del__(), self.i: 143
Dummy destroyed
Dummy.__del__(), self.i: 142
Dummy destroyed
Dummy.__del__(), self.i: 141
Dummy destroyed
Dummy.__del__(), self.i: 140
Dummy destroyed
Dummy.__del__(), self.i: 139
Dummy destroyed
Dummy.__del__(), self.i: 138
Dummy destroyed
Dummy.__del__(), self.i: 137
Dummy destroyed
Dummy.__del__(), self.i: 136
Dummy destroyed
Dummy.__del__(), self.i: 135
Dummy destroyed
Dummy.__del__(), self.i: 134
Dummy destroyed
Dummy.__del__(), self.i: 133
Dummy destroyed
Dummy.__del__(), self.i: 132
Dummy destroyed
Dummy.__del__(), self.i: 131
Dummy destroyed
Dummy.__del__(), self.i: 130
Dummy destroyed
Dummy.__del__(), self.i: 129
Dummy destroyed
Dummy.__del__(), self.i: 128
Dummy destroyed
Dummy.__del__(), self.i: 127
Dummy destroyed
Dummy.__del__(), self.i: 126
Dummy destroyed
Dummy.__del__(), self.i: 125
Dummy destroyed
Dummy.__del__(), self.i: 124
Dummy destroyed
Dummy.__del__(), self.i: 123
Dummy destroyed
Dummy.__del__(), self.i: 122
Dummy destroyed
Dummy.__del__(), self.i: 121
Dummy destroyed
Dummy.__del__(), self.i: 120
Dummy destroyed
Dummy.__del__(), self.i: 119
Dummy destroyed
Dummy.__del__(), self.i: 118
Dummy destroyed
Dummy.__del__(), self.i: 117
Dummy destroyed
Dummy.__del__(), self.i: 116
Dummy destroyed
Dummy.__del__(), self.i: 115
Dummy destroyed
Dummy.__del__(), self.i: 114
Dummy destroyed
Dummy.__del__(), self.i: 113
Dummy destroyed
Dummy.__del__(), self.i: 112
Dummy destroyed
Dummy.__del__(), self.i: 111
Dummy destroyed
Dummy.__del__(), self.i: 110
Dummy destroyed
Dummy.__del__(), self.i: 109
Dummy destroyed
Dummy.__del__(), self.i: 108
Dummy destroyed
Dummy.__del__(), self.i: 107
Dummy destroyed
Dummy.__del__(), self.i: 106
Dummy destroyed
Dummy.__del__(), self.i: 105
Dummy destroyed
Dummy.__del__(), self.i: 104
Dummy destroyed
Dummy.__del__(), self.i: 103
Dummy destroyed
Dummy.__del__(), self.i: 102
Dummy destroyed
Dummy.__del__(), self.i: 101
Dummy destroyed
Dummy.__del__(), self.i: 100
Dummy destroyed
Dummy.__del__(), self.i: 99
Dummy destroyed
Dummy.__del__(), self.i: 98
Dummy destroyed
Dummy.__del__(), self.i: 97
Dummy destroyed
Dummy.__del__(), self.i: 96
Dummy destroyed
Dummy.__del__(), self.i: 95
Dummy destroyed
Dummy.__del__(), self.i: 94
Dummy destroyed
Dummy.__del__(), self.i: 93
Dummy destroyed
Dummy.__del__(), self.i: 92
Dummy destroyed
Dummy.__del__(), self.i: 91
Dummy destroyed
Dummy.__del__(), self.i: 90
Dummy destroyed
Dummy.__del__(), self.i: 89
Dummy destroyed
Dummy.__del__(), self.i: 88
Dummy destroyed
Dummy.__del__(), self.i: 87
Dummy destroyed
Dummy.__del__(), self.i: 86
Dummy destroyed
Dummy.__del__(), self.i: 85
Dummy destroyed
Dummy.__del__(), self.i: 84
Dummy destroyed
Dummy.__del__(), self.i: 83
Dummy destroyed
Dummy.__del__(), self.i: 82
Dummy destroyed
Dummy.__del__(), self.i: 81
Dummy destroyed
Dummy.__del__(), self.i: 80
Dummy destroyed
Dummy.__del__(), self.i: 79
Dummy destroyed
Dummy.__del__(), self.i: 78
Dummy destroyed
Dummy.__del__(), self.i: 77
Dummy destroyed
Dummy.__del__(), self.i: 76
Dummy destroyed
Dummy.__del__(), self.i: 75
Dummy destroyed
Dummy.__del__(), self.i: 74
Dummy destroyed
Dummy.__del__(), self.i: 73
Dummy destroyed
Dummy.__del__(), self.i: 72
Dummy destroyed
Dummy.__del__(), self.i: 71
Dummy destroyed
Dummy.__del__(), self.i: 70
Dummy destroyed
Dummy.__del__(), self.i: 69
Dummy destroyed
Dummy.__del__(), self.i: 68
Dummy destroyed
Dummy.__del__(), self.i: 67
Dummy destroyed
Dummy.__del__(), self.i: 66
Dummy destroyed
Dummy.__del__(), self.i: 65
Dummy destroyed
Dummy.__del__(), self.i: 64
Dummy destroyed
Dummy.__del__(), self.i: 63
Dummy destroyed
Dummy.__del__(), self.i: 62
Dummy destroyed
Dummy.__del__(), self.i: 61
Dummy destroyed
Dummy.__del__(), self.i: 60
Dummy destroyed
Dummy.__del__(), self.i: 59
Dummy destroyed
Dummy.__del__(), self.i: 58
Dummy destroyed
Dummy.__del__(), self.i: 57
Dummy destroyed
Dummy.__del__(), self.i: 56
Dummy destroyed
Dummy.__del__(), self.i: 55
Dummy destroyed
Dummy.__del__(), self.i: 54
Dummy destroyed
Dummy.__del__(), self.i: 53
Dummy destroyed
Dummy.__del__(), self.i: 52
Dummy destroyed
Dummy.__del__(), self.i: 51
Dummy destroyed
Dummy.__del__(), self.i: 50
Dummy destroyed
Dummy.__del__(), self.i: 49
Dummy destroyed
Dummy.__del__(), self.i: 48
Dummy destroyed
Dummy.__del__(), self.i: 47
Dummy destroyed
Dummy.__del__(), self.i: 46
Dummy destroyed
Dummy.__del__(), self.i: 45
Dummy destroyed
Dummy.__del__(), self.i: 44
Dummy destroyed
Dummy.__del__(), self.i: 43
Dummy destroyed
Dummy.__del__(), self.i: 42
Dummy destroyed
Dummy.__del__(), self.i: 41
Dummy destroyed
Dummy.__del__(), self.i: 40
Dummy destroyed
Dummy.__del__(), self.i: 39
Dummy destroyed
Dummy.__del__(), self.i: 38
Dummy destroyed
Dummy.__del__(), self.i: 37
Dummy destroyed
Dummy.__del__(), self.i: 36
Dummy destroyed
Dummy.__del__(), self.i: 35
Dummy destroyed
Dummy.__del__(), self.i: 34
Dummy destroyed
Dummy.__del__(), self.i: 33
Dummy destroyed
Dummy.__del__(), self.i: 32
Dummy destroyed
Dummy.__del__(), self.i: 31
Dummy destroyed
Dummy.__del__(), self.i: 30
Dummy destroyed
Dummy.__del__(), self.i: 29
Dummy destroyed
Dummy.__del__(), self.i: 28
Dummy destroyed
Dummy.__del__(), self.i: 27
Dummy destroyed
Dummy.__del__(), self.i: 26
Dummy destroyed
Dummy.__del__(), self.i: 25
Dummy destroyed
Dummy.__del__(), self.i: 24
Dummy destroyed
Dummy.__del__(), self.i: 23
Dummy destroyed
Dummy.__del__(), self.i: 22
Dummy destroyed
Dummy.__del__(), self.i: 21
Dummy destroyed
Dummy.__del__(), self.i: 20
Dummy destroyed
Dummy.__del__(), self.i: 19
Dummy destroyed
Dummy.__del__(), self.i: 18
Dummy destroyed
Dummy.__del__(), self.i: 17
Dummy destroyed
Dummy.__del__(), self.i: 16
Dummy destroyed
Dummy.__del__(), self.i: 15
Dummy destroyed
Dummy.__del__(), self.i: 14
Dummy destroyed
Dummy.__del__(), self.i: 13
Dummy destroyed
Dummy.__del__(), self.i: 12
Dummy destroyed
Dummy.__del__(), self.i: 11
Dummy destroyed
Dummy.__del__(), self.i: 10
Dummy destroyed
Dummy.__del__(), self.i: 9
Dummy destroyed
Dummy.__del__(), self.i: 8
Dummy destroyed
Dummy.__del__(), self.i: 7
Dummy destroyed
Dummy.__del__(), self.i: 6
Dummy destroyed
Dummy.__del__(), self.i: 5
Dummy destroyed
Dummy.__del__(), self.i: 4
Dummy destroyed
Dummy.__del__(), self.i: 3
Dummy destroyed
Dummy.__del__(), self.i: 2
Dummy destroyed
Dummy.__del__(), self.i: 1
Dummy destroyed
Dummy.__del__(), self.i: 0
Dummy destroyed
关于qt - QML 不拥有从 PyQt 插槽接收的对象的所有权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24558264/
我现在已经用 PyQt 做了几个项目,而且我越来越熟悉 Qt 采用的模型/ View 思想流派。我已经将它用于列表和表格 View 之类的东西,它们背后有一个自定义模型来显示和操作数据。我使用委托(d
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * class MainWindow(QMainWindow):
使用下面的示例代码(受 here 的严重影响),右键单击上下文菜单并没有真正正确对齐。 从屏幕截图中可以看出,生成的菜单在鼠标光标上方相当多的位置。我希望菜单的左上角与鼠标指针完全对齐。 有没有办法对
所以我创建了一个自定义上下文菜单,但我想根据某些值将树小部件的某些行中的某些项目灰显。如何禁用菜单上的项目? myUI.setContextMenuPolicy( Qt.CustomContextMe
是否可以禁用 QComboBox在 pyqt 中,就像我们可以在 Win Forms(C#) 中那样做,因为我在 QComboBox 中找不到任何选项手动的。我想启用 QcomboBox仅当管理员登录
我想将 QComboBox 与元组中的“键”和“值”一起使用,该元组类似于 django 模型中使用的元组。例如,我对一个人的性别有以下结构。 SEX_CHOICES = (('M', 'Male')
是否可以让 Altair 或 Vega(-Lite) 渲染到 PyQt 小部件,类似于支持多个后端的 Matplotlib?我知道我可以使用 Qt WebView 小部件来呈现带有 Vega 嵌入的网
在下面的示例代码中(受 here 的影响很大),我希望选择单击单元格的整行而不是单个单元格。如何更改代码以合并它? import re import operator import os import
我正在尝试禁用关闭“x”按钮,并且我认为通过将 DockWidgetFeature 设置为仅可移动和可 float 即可工作。 def CreateDockWidget (self): Pan
我已经按照 Yasin Uludag 的一些有用的在线教程来尝试使用 PyQt(或者更确切地说是 PySide)来创建一个简单的 TreeView ,但是我在使用工具提示时遇到了问题。在以下代码中,工
我正在尝试创建一个场景,我需要从 mousePressEvent 位置画线到最新的鼠标 moveposition 这意味着我需要调用 paintEvent 来自 mousePressEvent ,这可
是Python 3的组合和 PyQt 4受到推崇的?有没有其他选择? 最佳答案 我不明白为什么不,有一个 version available对于正常工作的 Python 3,如果你真的需要 Qt,唯一
我正在尝试显示从二进制文件中读取的图像数据(我编写了用于从文件中检索此数据并将其存储为图像以供 QImage() 使用的代码)。我想做的是将 slider 连接到图形 View 小部件,以便当您移动
我已经准备了很多关于如何在 python 和 pyqt 中将多个信号连接到同一个事件处理程序的帖子。例如,将多个按钮或组合框连接到同一功能。 许多示例展示了如何使用 QSignalMapper 执行此
我有一个 PyQt 主窗口,当用户按下某个按钮时,我需要从中获取一串用户输入。 这是我的用户输入窗口代码: class InputDialog(QtGui.QDialog): ''' t
编辑: 以下 buildout.cfg 用于构建 Qt、PyQt 和 SIP [buildout] parts = pyqt [pyqt] recipe = zc.recipe.cmmi ur
我目前正在开发一个应用程序,该应用程序可以使用 PyQt 访问 sqlalchemy 数据库并将其内容显示到 TableView 或其他一些小部件中。现在为了简单起见,我们只说这是一个电话簿,上面有姓
使用我现在拥有的代码,我可以成功地播放文件中的 .mp3 数据。但是我需要使用 QtCore.QBuffer(不是来自文件)播放相同的数据。当我使用文档的示例时,它会出现意外类型的 QBuffer 错
安装 sip 后,我在尝试安装 PyQt-x11-gpl-4.11 时不断收到这个可爱的错误消息。 mycommandline$ python configure-ng.py --verbose Qu
我正在为一个项目使用 PyQt。但并非突然间我收到一个错误: QPixmap: It is not safe to use pixmaps outside the GUI thread in PyQt
我是一名优秀的程序员,十分优秀!