1

我遇到了 QComboBox 的问题,它不允许我将编辑文本更改为任何不同大小写的现有项目。

示例代码如下。我想做的是在已经包含项目“一”的组合框中输入“一”,而不会将文本更改为“一”的副作用。目前,一旦组合框失去焦点,它就会变回“One”。

禁用 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_()
4

1 回答 1

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_()

唯一的问题是它允许向您的组合框添加重复项。我尝试将 self.findText(...) 添加到 if 语句,但甚至Qt.MatchExactly | Qt.MatchCaseSensitive 会匹配“bla”、“bLa”和“BLA”。我想你会发现的。

于 2010-10-18T22:46:00.833 回答