📌  相关文章
📜  PyQt5 Combo Box – 鼠标悬停在列表视图上时的不同边框颜色

📅  最后修改于: 2022-05-13 01:55:47.470000             🧑  作者: Mango

PyQt5 Combo Box – 鼠标悬停在列表视图上时的不同边框颜色

在本文中,我们将看到如何在鼠标悬停时为组合框的列表视图设置不同的边框颜色,列表视图是项目视图,即所有项目都可见。当我们按下组合框的向下箭头时,列表视图被打开。我们可以在setView方法的帮助下随时更改视图对象。

有两种方法可以通过创建我们自己的视图并更改其边框然后将其添加到组合框来做到这一点,但这是一个漫长的过程,因为我们必须将所有项目添加到其中,另一种方法是更改样式表默认视图,即列表视图。

下面是样式表代码——
代码 1

QListView::hover
{
border : 8px solid;
border-color-top : red;
border-color-right : green;
border-color-bottom : blue;
border-color-left : yellow;
}

或者

代码 2

QListView::hover
{
border : 5px solid;
border-color : red green blue yellow;
}

下面是实现

# importing libraries
from PyQt5.QtWidgets import * 
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import * 
from PyQt5.QtCore import * 
import sys
  
  
class Window(QMainWindow):
  
    def __init__(self):
        super().__init__()
  
        # setting title
        self.setWindowTitle("Python ")
  
        # setting geometry
        self.setGeometry(100, 100, 600, 400)
  
        # calling method
        self.UiComponents()
  
        # showing all the widgets
        self.show()
  
    # method for widgets
    def UiComponents(self):
        # creating a check-able combo box object
        self.combo_box = QComboBox(self)
  
        # setting geometry of combo box
        self.combo_box.setGeometry(200, 150, 150, 80)
  
        # making combo box editable
        self.combo_box.setEditable(True)
  
        # geek list
        geek_list = ["Sayian", "Super Sayian", "Super Sayian 2", "Super Sayian B"]
  
        # adding list of items to combo box
        self.combo_box.addItems(geek_list)
  
        # setting style sheet
        # adding different border color to the list view when mouse hover
        self.combo_box.setStyleSheet("QListView"
                                     "{"
                                     "border : 8px solid black;"
                                     "}"
                                     "QListView::hover"
                                     "{"
                                     "border : 8px solid;"
                                     "border-color : red green blue yellow;"
                                     "}")
  
  
# create pyqt5 app
App = QApplication(sys.argv)
  
# create the instance of our Window
window = Window()
  
window.show()
  
# start the app
sys.exit(App.exec())

输出 :

打开列表视图时

当鼠标悬停在它上面时