繁体   English   中英

使用 drawText() 放置带有坐标的文本

[英]Placing a text with coordinates with drawText()

我正在尝试放置带有坐标的文本,但我不知道如何引入参数。

我在网上搜索,只有 Qt.Allign ---- 的例子,如果我把参数放在文档中,我就会出错。 它是在 python 3.6 中使用 PyQt5 构建的

import sys
from PyQt5.QtCore import QTimer, Qt, QRectF
from PyQt5.QtWidgets import QWidget, QApplication, QPushButton, QVBoxLayout, QHBoxLayout
from PyQt5.QtGui import QColor, QFont, QPainter


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.stopwatch()

    def stopwatch(self):
        hbox = QHBoxLayout()
        self.setLayout(hbox)
        self.setWindowTitle("Example")
        self.setGeometry(400,400,400,200)
        self.formato = "0:00.0"
        self.show()

    def paintEvent(self, event):
        qp = QPainter()
        qp.begin(self)
        self.draw_rect(event, qp)
        qp.end()

    def draw_rect(self,event, qp):
        # Black Rectangle
        col = QColor("Black")
        col.setNamedColor("Black")
        qp.setPen(col)
        qp.setBrush(QColor("Black"))
        qp.drawRect(130,000,400,200)
        # formato
        qp.setPen(QColor("Green"))
        qp.setFont(QFont('Helvetica', 48))
        qp.drawText(event.rect(), QRect(50, 50, 50, 50),5 , self.formato)  # Problem 


app = QApplication(sys.argv)
w = Window()
sys.exit(app.exec_())

QPainter 提供了几种绘制文本的选项:

void drawText(const QPointF & position, const QString & text)
void drawText(const QPoint & position, const QString & text)
void drawText(const QRectF & rectangle, int flags, const QString & text, QRectF * boundingRect = 0)
void drawText(const QRect & rectangle, int flags, const QString & text, QRect * boundingRect = 0)
void drawText(int x, int y, const QString & text)
void drawText(int x, int y, int width, int height, int flags, const QString & text, QRect * boundingRect = 0)
void drawText(const QRectF & rectangle, const QString & text, const QTextOption & option = QTextOption())

在您的情况下,您应该使用:

qp.drawText(QPointF(50, 50), self.formato) # first option
qp.drawText(QPoint(50, 50), self.formato)  # second option
qp.drawText(50, 50, self.formato) # fifth option

注意:导致窗口关闭的错误是因为您传递的参数与任何表单都不对应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM