首页 > 解决方案 > 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 QLineEditwith 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: enter image description here
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_())

标签: pythonpyqt5qlineedit

解决方案


推荐阅读