Python:Qt4 QLineEdit TextCursor不会消失

问题描述 投票:1回答:1

我有一个带有一些QLineEdits的Dialog窗口,用于在我的软件中插入数据,我在键盘上用TAB键从第一个QLineEdit切换到下一个

我希望背景将其颜色更改为(例如)黄色,当聚焦时(焦点切换到另一个)它必须回到白色,QLineEdit是聚焦的,。为此,我在StyleSheetFocusInEvent插入了一个不同的FocusOutEvent

但我有一个问题......

问题是当我专注于QlineEdit它工作(背景改变颜色为黄色),但如果我写东西,我切换到下一个QLineEdit。最后一个TextCursor中的QlineEdit并没有消失,我在窗口中查看了两个或更多的Text Cursors。

*我省略了部分源代码(Like => Layout,Database Functions等),因为我认为它们与帮助我解决问题无关。

from PyQt4 import QtGui,QtCore;

class AddWindow(QtGui.QDialog):

    def __init__(self):
        QtGui.QDialog.__init__(self);

        #Surname
        self.SurnameLabel=QtGui.QLabel("Surname:",self);
        self.SurnameLabel.move(5,20);

        self.SurnameBox=QtGui.QLineEdit(self);
        self.SurnameBox.move(5,35);
        self.SurnameBox.focusInEvent=self.OnSurnameBoxFocusIn;
        self.SurnameBox.focusOutEvent=self.OnSurnameBoxFocusOut;

        #Name
        self.NameLabel=QtGui.QLabel("Name:",self);
        self.NameLabel.move(150,20);

        self.NameBox=QtGui.QLineEdit(self);
        self.NameBox.move(150,35);
        self.NameBox.focusInEvent=self.OnNameBoxFocusIn;
        self.NameBox.focusOutEvent=self.OnNameBoxFocusOut;

    def OnSurnameBoxFocusIn(self,event):
        self.SurnameBox.setStyleSheet("QLineEdit {background-color:yellow}");

    def OnSurnameBoxFocusOut(self,event):
        self.SurnameBox.setStyleSheet("QLineEdit {background-color:white}");

    def OnNameBoxFocusIn(self,event):
        self.NameBox.setStyleSheet("QLineEdit {background-color:yellow}");

    def OnNameBoxFocusOut(self,event):
        self.NameBox.setStyleSheet("QLineEdit {background-color:white}");            
python qt qt4 cursor qlineedit
1个回答
0
投票

问题是你的工具有些事件对游标行为很重要,你的代码一直在打断它。为了解决这个问题,请在代码成功完成后将其恢复原状;

def OnSurnameBoxFocusIn(self,event):
    self.SurnameBox.setStyleSheet("QLineEdit {background-color:yellow}");
    QtGui.QLineEdit.focusInEvent(self.SurnameBox,event) # <- put back old behavior

def OnSurnameBoxFocusOut(self,event):
    self.SurnameBox.setStyleSheet("QLineEdit {background-color:white}");
    QtGui.QLineEdit.focusOutEvent(self.SurnameBox,event) # <- put back old behavior

def OnNameBoxFocusIn(self,event):
    self.NameBox.setStyleSheet("QLineEdit {background-color:yellow}");
    QtGui.QLineEdit.focusInEvent(self.NameBox,event) # <- put back old behavior

def OnNameBoxFocusOut(self,event):
    self.NameBox.setStyleSheet("QLineEdit {background-color:white}");
    QtGui.QLineEdit.focusOutEvent(self.NameBox,event) # <- put back old behavior

问候,

© www.soinside.com 2019 - 2024. All rights reserved.