我有一个带有一些QLineEdit
s的Dialog窗口,用于在我的软件中插入数据,我在键盘上用TAB键从第一个QLineEdit
切换到下一个
我希望背景将其颜色更改为(例如)黄色,当聚焦时(焦点切换到另一个)它必须回到白色,QLineEdit
是聚焦的,。为此,我在StyleSheet
和FocusInEvent
插入了一个不同的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}");
问题是你的工具有些事件对游标行为很重要,你的代码一直在打断它。为了解决这个问题,请在代码成功完成后将其恢复原状;
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
问候,