PyQt5组合框 当鼠标悬停在列表视图上时不同的边框颜色
在这篇文章中,我们将看到如何在鼠标悬停在组合框的列表视图中设置不同的边框颜色,列表视图是项目视图,即所有项目都是可见的。当我们按下组合框的向下箭头时,列表视图被打开。我们可以在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 : 8px 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())
输出:
当列表视图被打开时
当鼠标悬停在它上面时