<legend id='p2asS'><style id='p2asS'><dir id='p2asS'><q id='p2asS'></q></dir></style></legend>
  • <tfoot id='p2asS'></tfoot>

          <bdo id='p2asS'></bdo><ul id='p2asS'></ul>
        <i id='p2asS'><tr id='p2asS'><dt id='p2asS'><q id='p2asS'><span id='p2asS'><b id='p2asS'><form id='p2asS'><ins id='p2asS'></ins><ul id='p2asS'></ul><sub id='p2asS'></sub></form><legend id='p2asS'></legend><bdo id='p2asS'><pre id='p2asS'><center id='p2asS'></center></pre></bdo></b><th id='p2asS'></th></span></q></dt></tr></i><div id='p2asS'><tfoot id='p2asS'></tfoot><dl id='p2asS'><fieldset id='p2asS'></fieldset></dl></div>

        <small id='p2asS'></small><noframes id='p2asS'>

        QLabel &amp;自动换行:如何以逗号为基础换行(与

        时间:2023-08-04
          • <small id='9pbJl'></small><noframes id='9pbJl'>

            <legend id='9pbJl'><style id='9pbJl'><dir id='9pbJl'><q id='9pbJl'></q></dir></style></legend><tfoot id='9pbJl'></tfoot>

                <tbody id='9pbJl'></tbody>
              • <i id='9pbJl'><tr id='9pbJl'><dt id='9pbJl'><q id='9pbJl'><span id='9pbJl'><b id='9pbJl'><form id='9pbJl'><ins id='9pbJl'></ins><ul id='9pbJl'></ul><sub id='9pbJl'></sub></form><legend id='9pbJl'></legend><bdo id='9pbJl'><pre id='9pbJl'><center id='9pbJl'></center></pre></bdo></b><th id='9pbJl'></th></span></q></dt></tr></i><div id='9pbJl'><tfoot id='9pbJl'></tfoot><dl id='9pbJl'><fieldset id='9pbJl'></fieldset></dl></div>
                  <bdo id='9pbJl'></bdo><ul id='9pbJl'></ul>
                • 本文介绍了QLabel &amp;自动换行:如何以逗号为基础换行(与空格)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试使用没有空格但用逗号分隔的文本制作多行 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.

                  以下触发器会触发每个行大小事件,因此这是一个成本高昂的解决方案.

                  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_()
                  

                  这篇关于QLabel &amp;自动换行:如何以逗号为基础换行(与空格)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何使用 PyQt5 运行 while 循环 下一篇:用画笔画画

                  相关文章

                  最新文章

                  <i id='Whrfm'><tr id='Whrfm'><dt id='Whrfm'><q id='Whrfm'><span id='Whrfm'><b id='Whrfm'><form id='Whrfm'><ins id='Whrfm'></ins><ul id='Whrfm'></ul><sub id='Whrfm'></sub></form><legend id='Whrfm'></legend><bdo id='Whrfm'><pre id='Whrfm'><center id='Whrfm'></center></pre></bdo></b><th id='Whrfm'></th></span></q></dt></tr></i><div id='Whrfm'><tfoot id='Whrfm'></tfoot><dl id='Whrfm'><fieldset id='Whrfm'></fieldset></dl></div>

                  <legend id='Whrfm'><style id='Whrfm'><dir id='Whrfm'><q id='Whrfm'></q></dir></style></legend>

                    • <bdo id='Whrfm'></bdo><ul id='Whrfm'></ul>

                    <small id='Whrfm'></small><noframes id='Whrfm'>

                    <tfoot id='Whrfm'></tfoot>