简体   繁体   English

pyQGIS中的下划线

[英]Underline in pyQGIS

I'm currently working on some university project on Qgis and I have to use Python to generate maps.我目前正在从事一些关于 Qgis 的大学项目,我必须使用 Python 来生成地图。

I would like to know if there is a way to underline str (like shown on the screenshot).我想知道是否有办法在 str 下划线(如屏幕截图所示)。

Text rounded in red is the text I want to underline红色四舍五入的文字是我要加下划线的文字

Thank you in advance for your help !预先感谢您的帮助 !

I finally managed to use another solution that I found on pyQgis's doc.我终于设法使用了我在 pyQgis 的文档中找到的另一个解决方案。 Here it is:这里是:

# Sous-titre

subtitle = QgsLayoutItemLabel(layout)
subtitle.setMode(QgsLayoutItemLabel.Mode.ModeHtml)
subtitle.setText("<u>Coordonnées GPS :</u> " + (str(y_camping)) + " , " + (str(x_camping))+ " | <u>Téléphone :</u> "+str(tel_camping))
subtitle.setFont(b17)
subtitle.adjustSizeToText()
layout.addLayoutItem(subtitle)
subtitle.attemptMove(QgsLayoutPoint(5, 19, QgsUnitTypes.LayoutMillimeters))

The fact is that I just transform the label in html then the text contained in it can be underlined using < u >< /u > !事实是,我只是在 html 中转换标签,然后可以使用 < u ></u > 为其中包含的文本加下划线!

Indeed with QGIS it is particular... Maybe it exists other methods, but I do that:事实上,对于 QGIS,它是特别的......也许它存在其他方法,但我这样做:

First, you will have to separate the text to underline, and the other text.首先,您必须将要下划线的文本与其他文本分开。

Then use QtGui.QFont to custom your font, and apply it to the wanted text, instead the former setFont.然后使用QtGui.QFont自定义您的字体,并将其应用于想要的文本,而不是以前的 setFont。

Example:例子:

...
from PyQt5 import *
...
myTitleBoldFont=QtGui.QFont("Verdana", 27)
myTitleBoldFont.setUnderline(True)
...
TextCustom.setFont(myBoldFont)

Do not forget to import PyQt5 .不要忘记导入PyQt5

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

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