久久久久久久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;自動(dòng)換行:如何以逗號(hào)為基礎(chǔ)換行(與

      QLabel amp; Word Wrap : How to break line base on a comma (vs space)(QLabel amp;自動(dòng)換行:如何以逗號(hào)為基礎(chǔ)換行(與空格))
      <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;自動(dòng)換行:如何以逗號(hào)為基礎(chǔ)換行(與空格)的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我正在嘗試使用沒有空格但用逗號(hào)分隔的文本制作多行 QLabel.例如:'貓、狗、兔子、火車、汽車、飛機(jī)、奶酪、肉、門、窗'

                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'

                我發(fā)現(xiàn)使用 setWordWrap 可以實(shí)現(xiàn)多行,但它會(huì)根據(jù)空格中斷.

                I have found that multiline is possible with setWordWrap but it breaks based on spaces.

                如何根據(jù)逗號(hào)換行?

                這是一個(gè)代碼示例:

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

                推薦答案

                一種方法是根據(jù) QLabel 大小編輯文本.

                One way of doing it would be to edit the text according to QLabel size.

                以下觸發(fā)器會(huì)觸發(fā)每個(gè)行大小事件,因此這是一個(gè)成本高昂的解決方案.

                The following triggers on every line size event, making this a costly solution.

                首先我們添加一個(gè)信號(hào):

                First we add a signal :

                class MainWindow(QMainWindow):
                    resized = QtCore.pyqtSignal()
                

                然后我們用一個(gè)方法連接信號(hào),設(shè)置 wordwrap 為 False 并添加自定義調(diào)整大小事件,每次標(biāo)簽獲得新大小時(shí)觸發(fā):

                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 處理大小變化并觸發(fā) 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)
                

                最后我們有一個(gè) add_spaces 方法,它計(jì)算最大行長(zhǎng)度并以逗號(hào)分隔.

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

                這篇關(guān)于QLabel &amp;自動(dòng)換行:如何以逗號(hào)為基礎(chǔ)換行(與空格)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                相關(guān)文檔推薦

                How to bind a function to an Action from Qt menubar?(如何將函數(shù)綁定到 Qt 菜單欄中的操作?)
                PyQt progress jumps to 100% after it starts(PyQt 啟動(dòng)后進(jìn)度躍升至 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 刻度標(biāo)簽設(shè)置在固定位置,以便當(dāng)我向左或向右滾動(dòng)時(shí),yaxis 刻度標(biāo)簽應(yīng)該可見
                `QImage` constructor has unknown keyword `data`(`QImage` 構(gòu)造函數(shù)有未知關(guān)鍵字 `data`)
                Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時(shí)顯示進(jìn)度條?)
                <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. 主站蜘蛛池模板: 综合国产| 欧美精品一区二区三区四区五区 | 精品96久久久久久中文字幕无 | 国产精品久久久久9999鸭 | 蜜桃在线视频 | 婷婷综合久久 | 亚洲国产精品成人无久久精品 | 精品久久久久久亚洲综合网站 | 欧美性视频在线播放 | 国产在线看片 | 久久国产综合 | 免费黄色av | 日韩一区二区在线看 | 久久91精品 | 精品日韩在线 | 91亚洲视频在线 | 超碰在线人人干 | 99pao成人国产永久免费视频 | 国产精品综合一区二区 | 欧美videosex性极品hd | 国产区高清 | 国产大片黄色 | 久久lu | 国产精品久久久久久久久久久久久 | 日韩免费一区二区 | 免费国产一区二区 | 日日日操| 天天看夜夜 | 精品一区二区久久久久久久网站 | 欧美精产国品一二三区 | 手机在线一区二区三区 | 日本午夜一区 | 精品国产一区二区三区久久久四川 | 欧亚av在线 | 欧美在线一区二区三区 | 午夜一区二区三区在线观看 | 欧美精品一区在线发布 | 亚洲精品免费视频 | 一区二区精品 | 国产美女自拍视频 | 日韩一级免费电影 |