2010-10-18 19 views
1

我遇到问题,QComboBox不允许我将编辑 文本更改为任何现有项目的不同情况。QComboBox替换编辑文本,如果案件不同于现有项目

示例代码如下。我想要做的是输入'one'到已包含'One'项目的组合 中,而 文本的副作用被更改为'One'。目前,当组合框失去焦点时,它会变回为'1',因为 。

禁用AutoCompletionCaseSensitivity的作品,但它有一方 没有用的效果(例如,不显示“one”的完成)。

我也试过重写QComboBox的focusOutEvent和 恢复正确的文本,但后来像复制粘贴的东西不要 工作。改变完成者也没有任何帮助。

事实上,组合框的行为方式对我的应用程序不利。如果 任何人有任何想法(或我错过了明显的东西),请让我知道 。

我在Ubuntu 10.04上使用Qt 4.6.2和PyQt 4.7.2,但在其他版本4.5以上的发行版/ Qt版本上有 。

感谢和问候

示例代码:

from PyQt4.QtGui import * 
from PyQt4.QtCore import SIGNAL, Qt 

class Widget(QWidget): 
    def __init__(self, parent=None): 
     super(Widget, self).__init__(parent) 
     combo = QComboBox() 
     combo.setEditable(True) 
     combo.addItems(['One', 'Two', 'Three']) 
     lineedit = QLineEdit() 

     layout = QVBoxLayout() 
     layout.addWidget(combo) 
     layout.addWidget(lineedit) 
     self.setLayout(layout) 

app = QApplication([]) 
widget = Widget() 
widget.show() 
app.exec_() 

回答

1
from PyQt4.QtGui import * 
from PyQt4.QtCore import SIGNAL, Qt, QEvent 


class MyComboBox(QComboBox): 
    def __init__(self): 
     QComboBox.__init__(self) 

    def event(self, event): 
     if event.type() == QEvent.KeyPress and event.key() == Qt.Key_Return: 
      self.addItem(self.currentText()) 

     return QComboBox.event(self, event) 

class Widget(QWidget): 
    def __init__(self, parent=None): 
     super(Widget, self).__init__(parent) 
     combo = MyComboBox() 
     combo.setEditable(True) 
     combo.addItems(['One', 'Two', 'Three']) 
     lineedit = QLineEdit() 

     layout = QVBoxLayout() 
     layout.addWidget(combo) 
     layout.addWidget(lineedit) 
     self.setLayout(layout) 

app = QApplication([]) 
widget = Widget() 
widget.show() 
app.exec_() 

与此唯一的问题是,它会允许添加重复你的组合框。 我尝试在if语句中添加self.findText(...),但即使Qt.MatchExactly | Qt.MatchCaseSensitive 也会匹配“bla”,“bLa”和“BLA”。 我想你会发现。

+0

谢谢,但它没有多大帮助。它不适合用户按Tab或点击任何其他小部件。 – concentricpuddle 2010-10-20 16:14:13

相关问题