本文整理汇总了Python中PyQt5.QtWidgets.QGraphicsTextItem.setHtml方法的典型用法代码示例。如果您正苦于以下问题:Python QGraphicsTextItem.setHtml方法的具体用法?Python QGraphicsTextItem.setHtml怎么用?Python QGraphicsTextItem.setHtml使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类PyQt5.QtWidgets.QGraphicsTextItem
的用法示例。
在下文中一共展示了QGraphicsTextItem.setHtml方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: createImage
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsTextItem import setHtml [as 别名]
def createImage(self, transform):
if self.type == DemoTextItem.DYNAMIC_TEXT:
return None
sx = min(transform.m11(), transform.m22())
sy = max(transform.m22(), sx)
textItem = QGraphicsTextItem()
textItem.setHtml(self.text)
textItem.setTextWidth(self.textWidth)
textItem.setFont(self.font)
textItem.setDefaultTextColor(self.textColor)
textItem.document().setDocumentMargin(2)
w = textItem.boundingRect().width()
h = textItem.boundingRect().height()
image = QImage(int(w * sx), int(h * sy),
QImage.Format_ARGB32_Premultiplied)
image.fill(QColor(0, 0, 0, 0).rgba())
painter = QPainter(image)
painter.scale(sx, sy)
style = QStyleOptionGraphicsItem()
textItem.paint(painter, style, None)
return image
示例2: AnimatedClock
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsTextItem import setHtml [as 别名]
class AnimatedClock(QGraphicsView):
def __init__(self, parent=None):
QGraphicsView.__init__(self, parent)
self.updateSecs = 0.5
# Border
self.setLineWidth(0)
self.setFrameShape(QtWidgets.QFrame.NoFrame)
# Size
sizePolicy = QSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
sizePolicy.setHeightForWidth(True)
self.setSizePolicy(sizePolicy)
# No scrollbars
self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# Scene
self.scene = QGraphicsScene()
self.setScene(self.scene)
self.setBackgroundBrush(QColor("black"))
# Text of clock
self.textItem = QGraphicsTextItem()
self.textItem.color = QColor(QColor("black"))
self.textItem.setFont(QFont("Segoe UI", 80))
self.textItem.setDefaultTextColor(QColor("white"))
self.textItem.setHtml("")
self.textItem.setZValue(20)
self.scene.addItem(self.textItem)
# Start ticking
self.start()
def sizeHint(self):
return QSize(300, 150)
def start(self):
self.updateTimer = QTimer()
self.updateTimer.setInterval(self.updateSecs * 990)
self.updateTimer.timeout.connect(self.updateClock)
self.updateTimer.start()
print("Animated clock - starting")
def stop(self):
if self.updateTimer != None:
self.updateTimer.stop()
print("Animated clock - stopping")
def updateClock(self):
localtime = time.localtime()
timeString = time.strftime("%H:%M:%S", localtime)
self.textItem.setHtml(timeString)
width = self.frameGeometry().width()
self.textItem.setFont(QFont("Segoe UI", width / 8))
self.textItem.update()
def heightForWidth(self, width):
return width * .32
def keyPressEvent(self, event): #QKeyEvent
event.ignore()
示例3: showImage
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsTextItem import setHtml [as 别名]
def showImage(self):
(newImg, newImgInfo) = self.loadImage()
# return PicItem(Pixmap(QPixmap(newImg)), -1, -1, xFactor, yFactor, newImgInfo)
self.scene.clear()
imgSz = newImgInfo.imgSize
self.setSceneRect(QRectF(0,0,imgSz.width(), imgSz.height()))
pixMap = QPixmap.fromImage(newImg)
# # pixMap.setWidth(self.width())
pixMapItem = self.scene.addPixmap(pixMap)
# pixMapItem.setPos(50,50)
# self.fitInView(QRectF(0, 0, self.width(), self.height()), Qt.KeepAspectRatio)
# Add caption
caption = QGraphicsTextItem()
caption.setDefaultTextColor(QColor(255,255,255))
caption.setPos(0, self.height()*0.94)
caption.setFont(QFont("Segoe UI", 30))
caption.setTextWidth(self.width())
# caption.setPos(100, 100)
# caption.setTextWidth(1500)
# if newImgInfo.createDate is not None:
# caption.setPlainText(newImgInfo.createDate.format());
# else:
# caption.setPlainText("Image is called bananas");
# print("Tags", newImgInfo.tags)
# tagStr = ""
# for tag in newImgInfo.tags:
# if tag != "Duplicate":
# tagStr += (", " if len(tagStr) != 0 else "") + tag
# if tagStr == "":
# tagStr = "NO TAGS"
# captionStr = '<h1 style="text-align:center;width:100%">' + tagStr + '</h1>'
# if newImgInfo.createDate is not None:
# print(newImgInfo.createDate.format())
# captionStr += '<BR><h2>' + newImgInfo.createDate.format() + '</h2>'
captionStr = ""
try:
if newImgInfo.rating is not None:
for i in range(newImgInfo.rating):
captionStr += "★"
for i in range(5-newImgInfo.rating):
captionStr += "☆"
if newImgInfo.mainDate is not None:
if len(captionStr) != 0:
captionStr += " "
captionStr += newImgInfo.mainDate.strftime("%d %b %Y")
except Exception as excp:
print("StaticPhotos: Cannot set caption")
captionStr = '<div style="background-color:#000000;text-align: right;padding-right:10dp;">' + captionStr + "</div>"
print(captionStr)
caption.setHtml(captionStr)
self.scene.addItem(caption)
self.scene.update()
示例4: buildDiagram
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsTextItem import setHtml [as 别名]
def buildDiagram(self):
"""
Public method to build the class shapes of the package diagram.
The algorithm is borrowed from Boa Constructor.
"""
self.allClasses = {}
initlist = glob.glob(os.path.join(self.package, '__init__.*'))
if len(initlist) == 0:
ct = QGraphicsTextItem(None)
self.scene.addItem(ct)
ct.setHtml(
self.tr("The directory <b>'{0}'</b> is not a package.")
.format(self.package))
return
modules = self.__buildModulesDict()
if not modules:
ct = QGraphicsTextItem(None)
self.scene.addItem(ct)
ct.setHtml(
self.tr(
"The package <b>'{0}'</b> does not contain any modules.")
.format(self.package))
return
# step 1: build all classes found in the modules
classesFound = False
for modName in list(modules.keys()):
module = modules[modName]
for cls in list(module.classes.keys()):
classesFound = True
self.__addLocalClass(cls, module.classes[cls], 0, 0)
if not classesFound:
ct = QGraphicsTextItem(None)
self.scene.addItem(ct)
ct.setHtml(
self.tr(
"The package <b>'{0}'</b> does not contain any classes.")
.format(self.package))
return
# step 2: build the class hierarchies
routes = []
nodes = []
for modName in list(modules.keys()):
module = modules[modName]
todo = [module.createHierarchy()]
while todo:
hierarchy = todo[0]
for className in list(hierarchy.keys()):
cw = self.__getCurrentShape(className)
if not cw and className.find('.') >= 0:
cw = self.__getCurrentShape(className.split('.')[-1])
if cw:
self.allClasses[className] = cw
if cw and cw.noAttrs != self.noAttrs:
cw = None
if cw and not (cw.external and
(className in module.classes or
className in module.modules)
):
if className not in nodes:
nodes.append(className)
else:
if className in module.classes:
# this is a local class (defined in this module)
self.__addLocalClass(
className, module.classes[className],
0, 0)
elif className in module.modules:
# this is a local module (defined in this module)
self.__addLocalClass(
className, module.modules[className],
0, 0, True)
else:
self.__addExternalClass(className, 0, 0)
nodes.append(className)
if hierarchy.get(className):
todo.append(hierarchy.get(className))
children = list(hierarchy.get(className).keys())
for child in children:
if (className, child) not in routes:
routes.append((className, child))
del todo[0]
# step 3: build the subpackages
subpackages = self.__buildSubpackagesDict()
for subpackage in sorted(subpackages.keys()):
self.__addPackage(subpackage, subpackages[subpackage], 0, 0)
nodes.append(subpackage)
self.__arrangeClasses(nodes, routes[:])
self.__createAssociations(routes)
self.umlView.autoAdjustSceneSize(limit=True)
示例5: buildDiagram
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsTextItem import setHtml [as 别名]
def buildDiagram(self):
"""
Public method to build the class shapes of the class diagram.
The algorithm is borrowed from Boa Constructor.
"""
import Utilities.ModuleParser
self.allClasses = {}
self.allModules = {}
try:
extensions = Preferences.getPython("PythonExtensions") + \
Preferences.getPython("Python3Extensions") + ['.rb']
module = Utilities.ModuleParser.readModule(
self.file, extensions=extensions, caching=False)
except ImportError:
ct = QGraphicsTextItem(None)
ct.setHtml(
self.tr("The module <b>'{0}'</b> could not be found.")
.format(self.file))
self.scene.addItem(ct)
return
if self.file not in self.allModules:
self.allModules[self.file] = []
routes = []
nodes = []
todo = [module.createHierarchy()]
classesFound = False
while todo:
hierarchy = todo[0]
for className in hierarchy:
classesFound = True
cw = self.__getCurrentShape(className)
if not cw and className.find('.') >= 0:
cw = self.__getCurrentShape(className.split('.')[-1])
if cw:
self.allClasses[className] = cw
if className not in self.allModules[self.file]:
self.allModules[self.file].append(className)
if cw and cw.noAttrs != self.noAttrs:
cw = None
if cw and not (cw.external and
(className in module.classes or
className in module.modules)):
if cw.scene() != self.scene:
self.scene.addItem(cw)
cw.setPos(10, 10)
if className not in nodes:
nodes.append(className)
else:
if className in module.classes:
# this is a local class (defined in this module)
self.__addLocalClass(
className, module.classes[className], 0, 0)
elif className in module.modules:
# this is a local module (defined in this module)
self.__addLocalClass(
className, module.modules[className], 0, 0, True)
else:
self.__addExternalClass(className, 0, 0)
nodes.append(className)
if hierarchy.get(className):
todo.append(hierarchy.get(className))
children = list(hierarchy.get(className).keys())
for child in children:
if (className, child) not in routes:
routes.append((className, child))
del todo[0]
if classesFound:
self.__arrangeClasses(nodes, routes[:])
self.__createAssociations(routes)
self.umlView.autoAdjustSceneSize(limit=True)
else:
ct = QGraphicsTextItem(None)
ct.setHtml(self.tr(
"The module <b>'{0}'</b> does not contain any classes.")
.format(self.file))
self.scene.addItem(ct)
示例6: QApplication
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsTextItem import setHtml [as 别名]
import sys
app = QApplication(sys.argv)
stickMan = StickMan()
stickMan.setDrawSticks(False)
textItem = QGraphicsTextItem()
textItem.setHtml(
'<font color="white"><b>Stickman</b>'
"<p>"
"Tell the stickman what to do!"
"</p>"
"<p><i>"
'<li>Press <font color="purple">J</font> to make the stickman jump.</li>'
'<li>Press <font color="purple">D</font> to make the stickman dance.</li>'
'<li>Press <font color="purple">C</font> to make him chill out.</li>'
'<li>When you are done, press <font color="purple">Escape</font>.</li>'
"</i></p>"
"<p>If he is unlucky, the stickman will get struck by lightning, and never jump, dance or chill out again."
"</p></font>"
)
w = textItem.boundingRect().width()
stickManBoundingRect = stickMan.mapToScene(stickMan.boundingRect()).boundingRect()
textItem.setPos(-w / 2.0, stickManBoundingRect.bottom() + 25.0)
scene = QGraphicsScene()
scene.addItem(stickMan)
scene.addItem(textItem)
scene.setBackgroundBrush(Qt.black)
示例7: buildDiagram
# 需要导入模块: from PyQt5.QtWidgets import QGraphicsTextItem [as 别名]
# 或者: from PyQt5.QtWidgets.QGraphicsTextItem import setHtml [as 别名]
def buildDiagram(self):
"""
Public method to build the modules shapes of the diagram.
"""
initlist = glob.glob(os.path.join(self.packagePath, '__init__.*'))
if len(initlist) == 0:
ct = QGraphicsTextItem(None)
ct.setHtml(
self.tr(
"The directory <b>'{0}'</b> is not a Python package.")
.format(self.package))
self.scene.addItem(ct)
return
shapes = {}
p = 10
y = 10
maxHeight = 0
sceneRect = self.umlView.sceneRect()
modules = self.__buildModulesDict()
sortedkeys = sorted(modules.keys())
externalMods = []
packageList = self.shortPackage.split('.')
packageListLen = len(packageList)
for module in sortedkeys:
impLst = []
for i in modules[module].imports:
if i.startswith(self.package):
n = i[len(self.package) + 1:]
else:
n = i
if i in modules:
impLst.append(n)
elif self.showExternalImports:
impLst.append(n)
if n not in externalMods:
externalMods.append(n)
for i in list(modules[module].from_imports.keys()):
if i.startswith('.'):
dots = len(i) - len(i.lstrip('.'))
if dots == 1:
n = i[1:]
i = n
else:
if self.showExternalImports:
n = '.'.join(
packageList[:packageListLen - dots + 1] +
[i[dots:]])
else:
n = i
elif i.startswith(self.package):
n = i[len(self.package) + 1:]
else:
n = i
if i in modules:
impLst.append(n)
elif self.showExternalImports:
impLst.append(n)
if n not in externalMods:
externalMods.append(n)
classNames = []
for cls in list(modules[module].classes.keys()):
className = modules[module].classes[cls].name
if className not in classNames:
classNames.append(className)
shape = self.__addModule(module, classNames, 0.0, 0.0)
shapeRect = shape.sceneBoundingRect()
shapes[module] = (shape, impLst)
pn = p + shapeRect.width() + 10
maxHeight = max(maxHeight, shapeRect.height())
if pn > sceneRect.width():
p = 10
y += maxHeight + 10
maxHeight = shapeRect.height()
shape.setPos(p, y)
p += shapeRect.width() + 10
else:
shape.setPos(p, y)
p = pn
for module in externalMods:
shape = self.__addModule(module, [], 0.0, 0.0)
shapeRect = shape.sceneBoundingRect()
shapes[module] = (shape, [])
pn = p + shapeRect.width() + 10
maxHeight = max(maxHeight, shapeRect.height())
if pn > sceneRect.width():
p = 10
y += maxHeight + 10
maxHeight = shapeRect.height()
shape.setPos(p, y)
p += shapeRect.width() + 10
else:
shape.setPos(p, y)
p = pn
rect = self.umlView._getDiagramRect(10)
sceneRect = self.umlView.sceneRect()
if rect.width() > sceneRect.width():
#.........这里部分代码省略.........