1

我有一个文本列表,如果用户在 中搜索文本QLineEdit,我会打印该文本。里面有一个QCompleterQLineEdit

问题是,正如我们所知Texttext不一样,但对用户来说是一样的。因此,如果用户开始输入Texttext,我想将其更改为TEXT实时QLineEdit。所以每当用户输入一个字母时,我想把它变成大写QCompleter- QLineEdit。我怎样才能做到这一点?我有这个自动取款机;

from PyQt5.QtWidgets import QApplication,QPushButton,QMainWindow,QLabel,QLineEdit,QCompleter
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import QPoint
import sys

class cssden(QMainWindow):
    def __init__(self):
        super().__init__()
        self.mwidget = QMainWindow(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        #size
        self.setFixedSize(600,400)

        #LINE EDIT QCOMPLETER
        self.label = QLineEdit(self)
        self.label.setGeometry(100,100,300,30)
        self.label.setStyleSheet("color: red;"
                                "font: bold 15pt 'Arial';")

        self.t = ["Hello","hi","Hey"]
        self.label.setCompleter(QCompleter(self.t, self))

        #BUTTON
        self.buton = QPushButton(self)
        self.buton.setText("Click")
        self.buton.setGeometry(200,140,90,50)

        self.buton.clicked.connect(self.hangiButon)

        #SET LABEL
        self.set_label = QLabel(self)
        self.set_label.setGeometry(100,300,900,100)
        self.set_label.setStyleSheet("color: green;"
                                    "font: bold 18pt 'Times New Roman';")
        self.show()


    def hangiButon(self):
        print(self.label.text(), self.t.index(self.label.text())+1)
        self.set_label.setText("Pressed to --> {}.".format(self.label.text().rstrip()))

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        delta = QPoint (event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()

app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 2px solid rgb(20,20,20)}")

ex = cssden()
sys.exit(app.exec_())

所以如果我按下h我想看到所有的词,不仅hi而且我想h立即改变它。但无法弄清楚如何。

4

1 回答 1

6

QCompleter部件有一个setCaseSensitivity属性,它采用QtCore.Qt.CaseSensitive/QtCore.Qt.CaseInsensitive或简单地采用 1 或 0 ( docs )。

Qt 文档“默认是 Qt::CaseSensitive”。

将属性更改为不区分大小写的匹配:

self.t = ["Hello","hi","Hey"]
my_completer = QCompleter(self.t, self)
my_completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
# my_completer.setCaseSensitivity(0)
self.label.setCompleter(my_completer)

要将用户输入更改为大写,您可以添加一个更改文本的方法:

def to_upper(self, txt):
    self.label.setText(txt.upper())

然后可以将其连接到以下事件self.label.textChanged

self.label.textChanged.connect(self.to_upper)

把它放在一起:

from PyQt5.QtWidgets import QApplication,QPushButton,QMainWindow,QLabel,QLineEdit,QCompleter
from PyQt5 import QtCore, QtWidgets, QtGui
from PyQt5.QtCore import QPoint
import sys

class cssden(QMainWindow):
    def __init__(self):
        super().__init__()
        self.mwidget = QMainWindow(self)
        self.setWindowFlags(QtCore.Qt.FramelessWindowHint)

        #size
        self.setFixedSize(600,400)

        #LINE EDIT QCOMPLETER
        self.label = QLineEdit(self)
        self.label.setGeometry(100,100,300,30)
        self.label.setStyleSheet("color: red;"
                                "font: bold 15pt 'Arial';")

        self.label.textChanged.connect(self.to_upper)

        self.t = ["Hello","hi","Hey"]
        my_completer = QCompleter(self.t, self)
        #my_completer.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
        my_completer.setCaseSensitivity(0)
        self.label.setCompleter(my_completer)

        #BUTTON
        self.buton = QPushButton(self)
        self.buton.setText("Click")
        self.buton.setGeometry(200,140,90,50)

        self.buton.clicked.connect(self.hangiButon)

        #SET LABEL
        self.set_label = QLabel(self)
        self.set_label.setGeometry(100,300,900,100)
        self.set_label.setStyleSheet("color: green;"
                                    "font: bold 18pt 'Times New Roman';")
        self.show()

    def to_upper(self, txt):
        self.label.setText(txt.upper())

    def hangiButon(self):
        print(self.label.text(), self.t.index(self.label.text())+1)
        self.set_label.setText("Pressed to --> {}.".format(self.label.text().rstrip()))

    def mousePressEvent(self, event):
        self.oldPos = event.globalPos()

    def mouseMoveEvent(self, event):
        delta = QPoint (event.globalPos() - self.oldPos)
        self.move(self.x() + delta.x(), self.y() + delta.y())
        self.oldPos = event.globalPos()

app = QApplication(sys.argv)
app.setStyleSheet("QMainWindow{background-color: rgb(30,30,30);border: 2px solid rgb(20,20,20)}")

ex = cssden()
sys.exit(app.exec_())
于 2016-07-03T00:44:08.170 回答