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

    <bdo id='niPE4'></bdo><ul id='niPE4'></ul>

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

  1. <legend id='niPE4'><style id='niPE4'><dir id='niPE4'><q id='niPE4'></q></dir></style></legend>
    <tfoot id='niPE4'></tfoot>

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

      為 QListWidget 中的特定項目設(shè)置不同的顏色

      Set different color to specifc items in QListWidget(為 QListWidget 中的特定項目設(shè)置不同的顏色)
          <tbody id='BS7HB'></tbody>

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

            <legend id='BS7HB'><style id='BS7HB'><dir id='BS7HB'><q id='BS7HB'></q></dir></style></legend>
            • <bdo id='BS7HB'></bdo><ul id='BS7HB'></ul>
            • <tfoot id='BS7HB'></tfoot>

            • <i id='BS7HB'><tr id='BS7HB'><dt id='BS7HB'><q id='BS7HB'><span id='BS7HB'><b id='BS7HB'><form id='BS7HB'><ins id='BS7HB'></ins><ul id='BS7HB'></ul><sub id='BS7HB'></sub></form><legend id='BS7HB'></legend><bdo id='BS7HB'><pre id='BS7HB'><center id='BS7HB'></center></pre></bdo></b><th id='BS7HB'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='BS7HB'><tfoot id='BS7HB'></tfoot><dl id='BS7HB'><fieldset id='BS7HB'></fieldset></dl></div>
                本文介紹了為 QListWidget 中的特定項目設(shè)置不同的顏色的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                我有一個 QListWidget,我想為列表的每個項目添加邊框底部并為項目設(shè)置背景顏色,我想為特定項目設(shè)置不同的背景顏色.所以我使用 my_list.setStyleSheet("QListWidget::item {border-bottom: 1px solid red; background-color: blue;}") 并將背景顏色設(shè)置為我使用的特定項目 item.setBackground(QtGui.QColor("#E3E3E3"))

                問題是我設(shè)置不同顏色的特定項目沒有得到這個顏色.

                解決方案

                您不能在小部件上使用樣式表和樣式設(shè)置的組合——樣式表將覆蓋單個項目的任何設(shè)置.例如,以下代碼在每個項目上使用 setBackground 來更改背景顏色.

                從 PyQt5.QtGui 導(dǎo)入 *從 PyQt5.QtWidgets 導(dǎo)入 *顏色= ['#7fc97f','#beaed4','#fdc086','#ffff99','#386cb0','#f0027f','#bf5b17','#666666']類主窗口(QMainWindow):def __init__(self, *args, **kwargs):super(MainWindow, self).__init__(*args, **kwargs)w = QListWidget()對于范圍內(nèi)的 n (8):i = QListWidgetItem('%s' % n)i.setBackground(QColor(顏色[n]))w.addItem(i)self.setCentralWidget(w)自我展示()應(yīng)用程序 = QApplication([])w = 主窗口()app.exec_()

                結(jié)果輸出:

                但是,如果我們在結(jié)果中添加樣式表行(第二個只有底部邊框):

                很遺憾,無法設(shè)置項目的邊框和顏色.但是,您可以做的就是插入一個自定義小部件作為列表項,或者

                使用委托的方法是定義一個 paint 方法,該方法接受一個 QPainter 對象(您使用它來進(jìn)行實際繪圖),一個 option 參數(shù)包含項目的矩形(相對于父窗口小部件)和一個 index,您可以通過它檢索項目數(shù)據(jù).然后,您使用 QPainter 上的方法來繪制您的項目.

                在上面的示例中,我們使用它來傳遞項目標(biāo)簽(位置 Qt.DisplayRole)和十六進(jìn)制顏色(位置 Qt.DisplayRole+1).ItemDisplayRole 的名稱文檔列出了其他定義的數(shù)據(jù)角色",但在大多數(shù)情況下,您可以忽略這些.

                I have a QListWidget and I want to add border bottom to each item of the list and set a background color to items and I want to set to specific items a different background color. So I used my_list.setStyleSheet("QListWidget::item { border-bottom: 1px solid red; background-color: blue;}") and to set background color to specific items I used item.setBackground(QtGui.QColor("#E3E3E3"))

                The problem is the specif items that I set a different color don't get this color.

                解決方案

                You can't use a combination of stylesheets and style settings on a widget — the stylesheet will override any settings on individual items. For example, the following code uses setBackground on each item to change the background colour.

                from PyQt5.QtGui import *
                from PyQt5.QtWidgets import *
                
                colors = [ '#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666']
                
                class MainWindow(QMainWindow):
                
                    def __init__(self, *args, **kwargs):
                        super(MainWindow, self).__init__(*args, **kwargs)
                
                        w = QListWidget()
                        for n in range(8):
                            i = QListWidgetItem('%s' % n)
                            i.setBackground( QColor(colors[n]) )
                            w.addItem(i)
                
                        self.setCentralWidget(w)
                
                        self.show()
                
                
                app = QApplication([])
                w = MainWindow()
                app.exec_()
                

                The resulting output:

                However, if we add the stylesheet line in the result is (and second with only the bottom border):

                Unfortunately, there is no way to set the border and the colour for the items. However, what you can do is either insert a custom widget as the list item, or use an item delegate to draw the item. This gives you complete control over appearance, however you have to handle drawing yourself. Below is an example of doing this with a custom delegate:

                from PyQt5.QtCore import *
                from PyQt5.QtGui import *
                from PyQt5.QtWidgets import *
                
                colors = [ '#7fc97f', '#beaed4', '#fdc086', '#ffff99', '#386cb0', '#f0027f', '#bf5b17', '#666666']
                
                
                class MyDelegate(QItemDelegate):
                    def __init__(self, parent=None, *args):
                        QItemDelegate.__init__(self, parent, *args)
                
                    def paint(self, painter, option, index):
                        painter.save()
                
                        # set background color
                        painter.setPen(QPen(Qt.NoPen))
                        if option.state & QStyle.State_Selected:
                            # If the item is selected, always draw background red
                            painter.setBrush(QBrush(Qt.red))
                        else:
                            c = index.data(Qt.DisplayRole+1) # Get the color
                            painter.setBrush(QBrush(QColor(c)))
                
                        # Draw the background rectangle            
                        painter.drawRect(option.rect)
                
                        # Draw the bottom border
                        # option.rect is the shape of the item; top left bottom right
                        # e.g. 0, 0, 256, 16 in the parent listwidget
                        painter.setPen(QPen(Qt.red))        
                        painter.drawLine(option.rect.bottomLeft(), option.rect.bottomRight())
                
                        # Draw the text
                        painter.setPen(QPen(Qt.black))
                        text = index.data(Qt.DisplayRole)
                        # Adjust the rect (to pad)
                        option.rect.setLeft(5)
                        option.rect.setRight(option.rect.right()-5)
                        painter.drawText(option.rect, Qt.AlignLeft, text)
                
                        painter.restore()
                
                
                class MainWindow(QMainWindow):
                
                    def __init__(self, *args, **kwargs):
                        super(MainWindow, self).__init__(*args, **kwargs)
                
                        de = MyDelegate(self)
                        w = QListWidget()
                        w.setItemDelegate(de)
                        for n in range(8):
                            s = '%s' % n
                            i = QListWidgetItem()
                            i.setData(Qt.DisplayRole, s) # The label
                            i.setData(Qt.DisplayRole+1, colors[n]) # The color
                            w.addItem(i)
                
                        self.setCentralWidget(w)
                
                        self.show()
                
                
                app = QApplication([])
                w = MainWindow()
                app.exec_()
                

                Which gives the following output:

                The approach with delegates is to define a paint method, which accepts a QPainter object (with which you do the actual drawing), an option parameter containing the rectangle of the item (relative to the parent widget) and an index through which you can retrieve the item data. You then use the methods on the QPainter to draw your item.

                In the example above we use this to pass in both the item label (at position Qt.DisplayRole) and the color in hex (at position Qt.DisplayRole+1). The name docs for ItemDisplayRole list the other defined data 'roles', but for most purposes you can ignore these.

                這篇關(guān)于為 QListWidget 中的特定項目設(shè)置不同的顏色的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(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 啟動后進(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)我向左或向右滾動時,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時顯示進(jìn)度條?)
                  <tbody id='qGY0E'></tbody>

                  1. <small id='qGY0E'></small><noframes id='qGY0E'>

                        <tfoot id='qGY0E'></tfoot>
                        • <bdo id='qGY0E'></bdo><ul id='qGY0E'></ul>

                        • <legend id='qGY0E'><style id='qGY0E'><dir id='qGY0E'><q id='qGY0E'></q></dir></style></legend>
                          <i id='qGY0E'><tr id='qGY0E'><dt id='qGY0E'><q id='qGY0E'><span id='qGY0E'><b id='qGY0E'><form id='qGY0E'><ins id='qGY0E'></ins><ul id='qGY0E'></ul><sub id='qGY0E'></sub></form><legend id='qGY0E'></legend><bdo id='qGY0E'><pre id='qGY0E'><center id='qGY0E'></center></pre></bdo></b><th id='qGY0E'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qGY0E'><tfoot id='qGY0E'></tfoot><dl id='qGY0E'><fieldset id='qGY0E'></fieldset></dl></div>
                          主站蜘蛛池模板: 综合一区| 亚洲欧美日韩系列 | 国产一区二区精品在线 | 一级午夜aaa免费看三区 | 久久精品久久久久久 | 四虎成人av| 国产极品车模吞精高潮呻吟 | 欧美在线一区二区三区 | 日日摸夜夜添夜夜添精品视频 | 亚洲视频在线一区 | 一区观看 | 国产免费一区二区三区 | 亚洲成人av一区二区 | 成人av一区 | 久久久久久久亚洲精品 | 性高湖久久久久久久久 | 亚洲成av人影片在线观看 | 韩国毛片视频 | 黄色毛片在线观看 | 一区二区三区久久久 | 91精品国产综合久久福利软件 | 超碰91在线 | 涩涩视频在线播放 | 国产精品国产成人国产三级 | 四虎影音 | 国产精品一级 | www.国产精品 | 国产乱码精品一区二区三区忘忧草 | 国产欧美精品一区二区三区 | 国产精品亚洲综合 | 福利片在线 | 久久av一区 | 亚洲人人舔人人 | 中文字幕欧美一区 | 成年男女免费视频网站 | 欧美日韩在线播放 | 一级片免费观看 | 国产精品成人在线 | 国产一区二区免费 | 国产成人一区二区三区精 | 国产精品久久国产精品 |