去除QListWidget
, QListView
, QTableWidget
, QTableView
等控件的虚线框有好几种方法。
网上看到的大多是这样的:
setFocusPolicy(Qt.NoFocus)
这样确实可以去掉虚线框,但缺点是没有焦点了app.setStyleSheet('*{outline:0px;}')
设置全局样式,好像有时候没有效果,可能还需要配合什么其他样式- 继承
QProxyStyle
代理样式重写drawPrimitive
方法对element == QStyle.PE_FrameFocusRect
进行return
返回不绘制操作
这里介绍第4种,使用 QStyledItemDelegate
取消其 State_HasFocus
状态即可
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2019年12月11日
@author: Irony
@site: http://127.0.0.1 https://github.com/892768447
@email: 892768447@qq.com
@file: 取消虚线框
@description:
"""
from PyQt5.QtWidgets import QStyledItemDelegate, QStyle, QListWidget,\
QTableWidget
__Author__ = 'Irony'
__Copyright__ = 'Copyright (c) 2019'
__Version__ = 1.0
class NoFocusItemDelegate(QStyledItemDelegate):
def paint(self, painter, option, index):
if option.state & QStyle.State_HasFocus:
# 取消虚线框
option.state = option.state ^ QStyle.State_HasFocus
super(NoFocusItemDelegate, self).paint(painter, option, index)
if __name__ == '__main__':
import sys
import cgitb
cgitb.enable(1, None, 5, '')
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
w = QTableWidget()
w.setItemDelegate(NoFocusItemDelegate(w))
w.setColumnCount(5)
w.setRowCount(5)
w.show()
sys.exit(app.exec_())
2 条评论
感谢博主分享!!OωO
原来如此