有可能因为问这么简单的问题而大吼大叫,但我一直在网上寻找答案,这个特殊的案例似乎被广泛避免,而且文档含糊不清:
是否可以使用xml.etree.ElementTree.write()在元素的属性中写入非字符串值?我总是得到:
TypeError: cannot serialize 0 (type int)
Run Code Online (Sandbox Code Playgroud)
当我尝试这样的事情:
root = ET.Element('Tasks')
d = {'priority': 1, 'status': 0, 'name': 'new task', 'index': 0}
d = ET.SubElement(root, 'Settings', attrib=d)
tree = ET.ElementTree(root)
tree.write('/tmp/xmlTest')
Run Code Online (Sandbox Code Playgroud)
通过遍历相应的字典并首先将所有值转换为字符串,我已经多次解决这个问题,但这感觉不对,在我再次讨论它之前我想知道它应该如何正确地完成以至于不能得到它习惯了坏习惯.所以任何见解都会受到高度赞赏.
干杯,坦率地说
我想知道如何根据它的最大宽度/高度最好地截断QLabel中的文本.传入的文本可以是任何长度,但为了保持整洁的布局,我想截断长字符串以填充最大空间量(小部件的最大宽度/高度).
例如:
'A very long string where there should only be a short one, but I can't control input to the widget as it's a user given value'
Run Code Online (Sandbox Code Playgroud)
会成为:
'A very long string where there should only be a short one, but ...'
Run Code Online (Sandbox Code Playgroud)
基于当前字体所需的空间.
我怎样才能做到最好?
这是我所追求的一个简单示例,虽然这是基于字数,而不是可用空间:
import sys
from PySide.QtGui import *
from PySide.QtCore import *
def truncateText(text):
maxWords = 10
words = text.split(' ')
return ' '.join(words[:maxWords]) + ' ...'
app = QApplication(sys.argv)
mainWindow = QWidget()
layout = QHBoxLayout() …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用自定义 QStandardItem 在两个 QListView 之间进行拖放操作。除了这份文件之外,我在网上找不到我需要的信息,这对我有一点帮助,但现在我被卡住了。
当我使用QStandardItem来保存我的数据时,从一个QListView 拖放到另一个QListView工作正常,但是当我使用自定义项目时,我遇到了麻烦,因为接收模型/视图在删除自定义项目时会创建一个QStandardItem。
理想情况下,我可以告诉接收模型使用我的自定义项目作为默认项目,否则就去做吧,但我想这不会那么容易?!似乎除了在放下时创建QStandardItem而不是我的自定义项目之外,一切都是开箱即用的,所以我希望我不必重新发明(拖放)轮子只是为了让那一部分正确? !
如果我必须重新发明轮子并实现视图的dropEvent以手动附加传入的项目,我就会遇到另一个奇怪的问题。这是我的测试代码(包括一些用于解码我在网上找到的丢弃数据的代码):
from PySide import QtCore, QtGui
class MyItem(QtGui.QStandardItem):
'''This is the item I'd like to drop into the view'''
def __init__(self, parent=None):
super(MyItem, self).__init__(parent)
self.testAttr = 'test attribute value'
class ReceivingView(QtGui.QListView):
'''Custom view to show the problem - i.e. the dropEvent produces a QStandardItem rather than MyItem'''
def __init__(self, parent=None):
super(ReceivingView, self).__init__(parent)
def decode_data(self, bytearray): …
Run Code Online (Sandbox Code Playgroud) 我试图在QLabel中显示一些HTML代码.虽然QLabel正确呈现html,但超链接实际上不起作用,图像链接只产生丢失的图标图片而不是显示图像本身.我猜这是QLabel的限制,我可能需要诉诸QWebView,但只是想检查我是否遗漏了什么?!
这是一个例子:
import sys
from PySide.QtGui import *
app = QApplication([])
label = QLabel()
label.setText('''<p><a href="http://www.google.com">"Go to Google"</a></p>
<p><img src="http://www.google.co.nz/logos/2012/field_hockey-2012-hp.jpg"/></p>
<p><span style="font-size: 17px;"><br /></span></p>''')
label.show()
sys.exit(app.exec_())
Run Code Online (Sandbox Code Playgroud)