问题描述
我正在尝试使用没有空格但用逗号分隔的文本制作多行 qlabel.例如:'猫、狗、兔子、火车、汽车、飞机、奶酪、肉、门、窗'
i am trying to make a multi line qlabel with a text without space but delimited by comma. ex : 'cat,dog,rabbit,train,car,plane,cheese,meat,door,window'
我发现使用 setwordwrap 可以实现多行,但它会根据空格中断.
i have found that multiline is possible with setwordwrap but it breaks based on spaces.
如何根据逗号换行?
这是一个代码示例:
from pyside2.qtwidgets import * class mainwindow(qmainwindow): def __init__(self, *args, **kwargs): super(mainwindow, self).__init__(*args, **kwargs) self.setgeometry(500,100,50,100) line = qlabel() line.setmaximumwidth(150) line.settext('cat,dog,rabbit,train,car,plane,cheese,meat,door,window') line.setwordwrap(true) self.setcentralwidget(line) self.show() if __name__ == '__main__': app = qapplication([]) window = mainwindow() app.exec_()
推荐答案
一种方法是根据 qlabel 大小编辑文本.
one way of doing it would be to edit the text according to qlabel size.
以下触发器会触发每个行大小事件,因此这是一个成本高昂的百家乐凯发k8的解决方案.
the following triggers on every line size event, making this a costly solution.
首先我们添加一个信号:
first we add a signal :
class mainwindow(qmainwindow): resized = qtcore.pyqtsignal()
然后我们用一个方法连接信号,设置 wordwrap 为 false 并添加自定义调整大小事件,每次标签获得新大小时触发:
then we connect signal with a method ,set wordwrap to false and add custom resize event that triggers every time label gets a new size:
self.line.setwordwrap(false) self.line.resizeevent = self.on_resize_event self.resized.connect(self.add_spaces)
on_resize_event 处理大小变化并触发 add_spaces :
on_resize_event which handles size changes and triggers add_spaces :
def on_resize_event(self, event): if not self._flag: self._flag = true self.resized.emit() qtcore.qtimer.singleshot(100, lambda: setattr(self, "_flag", false)) print(f"resized line: {self.line.size()}") return super(mainwindow, self).resizeevent(event)
最后我们有一个 add_spaces 方法,它计算最大行长度并以逗号分隔.
lastly we have a add_spaces method which calculates maximum line length and splits on comma.
def add_spaces(self): size = self.line.size() text = self.mystring result_string = "" temp_label = qlabel() temp_text = "" #split the chunks by delimiter chunks = text.split(",") for i,chunk in enumerate(chunks): temp_text = chunk ","; if len(chunks) > i 1: temp_label.settext(temp_text chunks[i 1] ",") width = temp_label.fontmetrics().boundingrect(temp_label.text()).width() if width >= size.width(): result_string = temp_text " " temp_text = "" else: result_string = temp_text self.line.settext(result_string)
完整代码:
from pyqt5 import qtcore from pyqt5.qtwidgets import qmainwindow, qlabel, qapplication class mainwindow(qmainwindow): resized = qtcore.pyqtsignal() def __init__(self, *args, **kwargs): super(mainwindow, self).__init__(*args, **kwargs) self._flag = false self.line = qlabel() self.line.setstylesheet("background-color: grey;color:white;") self.line.setmaximumwidth(300) self.line.setminimumwidth(20) self.mystring = 'cat,dog,rabbit,train,car,plane,cheese,meat,door,window,very,long,list of words,list of words,word,word,word,list of word,word,list of word,list of word' self.line.settext(self.mystring) self.setcentralwidget(self.line) self.line.setwordwrap(false) self.line.resizeevent = self.on_resize_event self.resized.connect(self.add_spaces) self.show() self.add_spaces() def on_resize_event(self, event): if not self._flag: self._flag = true self.resized.emit() qtcore.qtimer.singleshot(100, lambda: setattr(self, "_flag", false)) print(f"resized line: {self.line.size()}") return super(mainwindow, self).resizeevent(event) def add_spaces(self): size = self.line.size() text = self.mystring result_string = "" temp_label = qlabel() temp_text = "" #split the chunks by delimiter chunks = text.split(",") for i,chunk in enumerate(chunks): temp_text = chunk ","; if len(chunks) > i 1: temp_label.settext(temp_text chunks[i 1] ",") width = temp_label.fontmetrics().boundingrect(temp_label.text()).width() if width >= size.width(): result_string = temp_text " " temp_text = "" else: result_string = temp_text self.line.settext(result_string) if __name__ == '__main__': app = qapplication([]) window = mainwindow() app.exec_()