Python:qt4qlineedit TextCursor不会消失

2024-10-02 14:18:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个带有QLineEdit的对话框窗口,要在我的软件中插入数据,我用键盘上的TAB键从第一个QLineEdit切换到下一个

我想把背景颜色改成黄色,当焦点被调出时(焦点切换到另一个),它必须回到白色。为此,我在FocusInEventFocusOutEvent中插入了一个不同的StyleSheet。在

但我有个问题…

问题是当我把注意力集中在QlineEdit上时,它会起作用(背景颜色变为黄色),但如果我写了什么,我就切换到下一个QLineEdit。最后一个^{中的TextCursor没有消失,我在窗口中看到两个或更多的文本光标。在

*我省略了部分源代码(如=>;布局、数据库函数等),因为我认为它们与帮助我解决问题无关。在

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}");            

Tags: selfeventmove颜色defcolor焦点background
1条回答
网友
1楼 · 发布于 2024-10-02 14:18:26

问题是你的实现某个事件对光标行为很重要,你的代码已经中断了它。要修正它,请在你的代码成功运行后把旧的行为恢复过来

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

谨致问候

相关问题 更多 >