【问题标题】:QComboBox replacing edit text if case differs from existing item如果大小写与现有项目不同,则 QComboBox 替换编辑文本
【发布时间】:2011-04-26 19:49:37
【问题描述】:

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

示例代码如下。我想做的是在组合中输入“一个” 盒子已经包含项目“一”,没有副作用 文本被更改为“一”。目前它已改回“一”为 一旦组合框失去焦点。

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

【问题讨论】:

    标签: python qt4 pyqt pyqt4 qcombobox


    【解决方案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”。 我想你会发现的。

    【讨论】:

    • 谢谢,但这并没有多大帮助。它不适合用户按 Tab 或单击任何其他小部件。
    猜你喜欢
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2020-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-16
    • 2022-01-09
    相关资源
    最近更新 更多