久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

  1. <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>

    1. <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 class="qwawimqqmiuu" id='9pbJl'><tfoot id='9pbJl'></tfoot><dl id='9pbJl'><fieldset id='9pbJl'></fieldset></dl></div>
        <bdo id='9pbJl'></bdo><ul id='9pbJl'></ul>
    2. QLabel &amp;自動換行:如何以逗號為基礎換行(與

      QLabel amp; Word Wrap : How to break line base on a comma (vs space)(QLabel amp;自動換行:如何以逗號為基礎換行(與空格))
      <legend id='p2asS'><style id='p2asS'><dir id='p2asS'><q id='p2asS'></q></dir></style></legend>
    3. <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 class="qwawimqqmiuu" id='p2asS'><tfoot id='p2asS'></tfoot><dl id='p2asS'><fieldset id='p2asS'></fieldset></dl></div>

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

                <tbody id='p2asS'></tbody>
                本文介紹了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模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                <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 class="qwawimqqmiuu" 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>
                            <tbody id='Whrfm'></tbody>

                        1. 主站蜘蛛池模板: 欧美在线一区二区三区 | 国内精品久久久久久 | 91av在线不卡 | 天堂资源视频 | 天天玩夜夜操 | 日本中文字幕在线视频 | 国产最好的av国产大片 | 成人欧美一区二区三区白人 | 成人av影院| 久久久久国产一区二区三区四区 | 欧美日韩一区二区在线 | 国产午夜精品福利 | 日韩精品一区二区在线观看 | 少妇性l交大片免费一 | 欧美一级二级视频 | 中文字幕av在线一二三区 | 午夜欧美 | 国产wwwcom| 美国av毛片| 日韩中文字幕在线观看视频 | 国产欧美一区二区三区在线看 | 天天操天天拍 | 伊人免费网 | 欧美一区二区三区四区视频 | 日韩久久精品 | 国产成人精品久久 | 欧美另类视频 | 美女黄网 | 免费在线看黄 | 国产一区 | 中文字幕高清 | 久久午夜精品 | 亚洲嫩草 | 欧美二区三区 | 网色| 91精品国产综合久久精品 | 香蕉视频黄色 | 国产成人精品999在线观看 | 国产在线观看不卡一区二区三区 | 91精品国产手机 | 久久国 |