python - 如何在标签中同时显示图片和文字?(PyQt)
问题描述
我有一个标签(L1),我想在 L1 中显示图片和文字。然后,我在 L1 中新建一个布局,并在布局中添加一个标签 (L2)。我在 L1 的paintEvent 中绘制文本。L2 可以左右移动,L2 东西会覆盖文本。一个演示是:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class MyLabel(QLabel):
def __init__(self):
super(MyLabel, self).__init__()
layout = QHBoxLayout()
self.setLayout(layout)
label = QLabel('test')
label.setStyleSheet("background-color: rgb(255,255,255)")
layout.addWidget(label)
def paintEvent(self, QPaintEvent):
super(MyLabel, self).paintEvent(QPaintEvent)
pos = QPoint(50, 50)
painter = QPainter(self)
painter.drawText(pos, 'hello,world')
class Window(QWidget):
def __init__(self):
super(Window, self).__init__()
layout = QHBoxLayout(self)
self.label = MyLabel()
layout.addWidget(self.label)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())
“你好,世界”被覆盖了。我怎样才能始终显示文本?
解决方案
I've seen a few tutorials (and even SE posts) where a QLabel is used as a canvas for drawing things. If you start digging into the Qt5 docs, they suggest that using QGraphicsView/QGraphicsScene system is a good way to draw on a canvas. I won't lie: there is some overhead to figure out how to use everything together, but the system is pretty powerful and you can make pretty graphics this way from all kinds of primitives.
If you need something small and simple, using QLabel and PaintEvent is not bad. But if your app is going to do lots of graphics, I would recommend QGraphicsView.
Here's a simple example to draw two pieces of text on a QGraphicsView. For the first, we don't set the position, so it defaults to (0,0) the top left hand corner. The second is drawn at (50,50)
from PyQt5.QtWidgets import (QApplication, QMainWindow, QWidget,
QGraphicsView, QGraphicsScene,
QGraphicsSimpleTextItem, QGridLayout)
import sys
class DemoApp(QMainWindow):
def __init__(self, parent=None):
super(DemoApp, self).__init__()
# set up the layout for the MainWindow.
grid_layout = QGridLayout()
self.graphicsView = QGraphicsView()
grid_layout.addWidget(self.graphicsView)
widget = QWidget()
widget.setLayout(grid_layout)
self.setCentralWidget(widget)
scene = QGraphicsScene()
self.graphicsView.setScene(scene)
mytext1 = QGraphicsSimpleTextItem('the first label')
scene.addItem(mytext1)
mytext2 = QGraphicsSimpleTextItem('the second label')
scene.addItem(mytext2)
mytext2.setPos(50,50)
app = QApplication(sys.argv)
demo_app = DemoApp(None)
demo_app.show()
sys.exit(app.exec_())
There are a few examples for QGraphicsView that ship with PyQt5, most notably "dragdroprobot.py"
推荐阅读
- javascript - 在 Angular 中生成 HTML 到自定义 PDF
- r - 在 Windows 上的 R 版本 4.0.1 上安装 R Markdown 时出现错误消息
- java - 为什么我的数据只在读取时刷新到数据库?
- apache-spark - 没有流时的Spark结构化流窗口
- arrays - 喜欢 - 不像 MongoDb 中的系统来查找用户是否喜欢?
- git - qt.qpa.plugin 的 Git Difftool 问题:在“”中找不到 Qt 平台插件“cocoa”
- python - 仅针对一个文件的 Python 命令未找到错误
- angular - spyOn(document, 'getElementById') 升级到 angular 8/"@types/jasmine": "^3.4.0" 后抛出错误
- docker - WORKDIR 作为 VOLUME
- node.js - Parallelshell 模块未在 npm 上运行。可以做什么?