python - QLineEdit focus highlight in window with Qt.WA_TranslucentBackground
问题描述
I have a QWidget that is a borderless window with rounded corners. The background is drawn in the def paintEvent(self, event):
method. In addition I'm using self.setAttribute(Qt.WA_TranslucentBackground, True)
to ensure that my custom background is the window's background.
On the widget I add an QLineEdit
with a transparent background. Since I'm on Mac I also added self.setAttribute(Qt.WA_MacShowFocusRect, False)
to remove the blue highlight when the input field has focus.
However, when the QLineEdit
has focus it still has a focus highlight and this focus highlight is transparent showing whatever is behind the window:
As you can see also the cursor seems to have some highlighting that creates the same transparency.
How do I get rid of the focus highlight completely? Or is there a way to customly style the focus highlight so that I can give it resonable colors?
Code:
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QLineEdit, QApplication, QVBoxLayout, QLabel
from PyQt5.QtGui import QPainter, QFont, QBrush
class TransparentLineEdit(QLineEdit):
def __init__(self, parent=None):
super(TransparentLineEdit, self).__init__(parent)
font = QFont()
font.setFamily('Helvetica Neue')
font.setWeight(10)
font.setPointSize(32)
self.setFont(font)
self.setAttribute(Qt.WA_MacShowFocusRect, False)
self.setStyleSheet("""
QLineEdit{
background:transparent;
outline: 0px;
outline: none;
outline-style: none;
border: none;
}
""")
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# window style
winwidth = 400
winheight = 40
self.setWindowFlags(Qt.FramelessWindowHint | Qt.WindowStaysOnTopHint )
#| Qt.SplashScreen
# self.setWindowOpacity(0.75)
self.setAttribute(Qt.WA_TranslucentBackground, True) #100% transparent
#self.setAttribute(Qt.WA_MacOpaqueSizeGrip, True)
#self.setAttribute(Qt.WA_NoSystemBackground, True)
self.setGeometry(0, 0, winwidth, winheight)
pos = QApplication.desktop().screen().rect().center()-self.rect().center()
pos.setY(pos.y() - 100)
self.move(pos)
# window contents
MainLayout = QVBoxLayout()
MainLayout.setContentsMargins(10, 10, 10, 10)
InputField = TransparentLineEdit()
MainLayout.addWidget(QLabel('INPUT'))
MainLayout.addWidget(InputField)
self.setLayout(MainLayout)
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setRenderHint(QPainter.Antialiasing) # Anti-aliasing;
painter.setBrush(QBrush(Qt.darkGray))
painter.setPen(Qt.transparent)
bgrect = event.rect()
bgrect.setWidth(bgrect.width() -1)
bgrect.setHeight(bgrect.height() -1)
painter.drawRoundedRect (bgrect, 5, 5)
painter.end()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
win = MainWindow()
win.show()
sys.exit(app.exec_())
解决方案
推荐阅读
- python - 层 'meta-python' 取决于版本 >= 层 'core' 的 12
- flutter - Flutter 将函数传递给 TextEditingController(text)
- r - 在一个数据帧中的坐标处获取深度值,最接近另一个数据帧中的坐标
- python - 如何自动包装 Pylance 提供的 VSCode python intelliSence?
- android - 是否可以在 Android 中制作两个滑块 MotionLayout?
- terraform - Terraform 错误此值没有任何索引
- php - Redis / Homestead / Laravel 7 - 拒绝连接
- angular - Angular - 迭代变量并将其连接到字符串以调用函数
- php - 在 Craft CMS 3 中打开预览时出错
- laravel - Laravel:每月分期付款