如何让QLineEdit-QCompleter自动上线
How to make QLineEdit-QCompleter upperlines automatically
我有一个文本列表,如果用户在 QLineEdit
中搜索文本,我会打印该文本。 QLineEdit
.
中有一个QCompleter
问题是,我们知道 Text
和 text
不一样,但对用户来说是一样的。因此,如果用户开始输入 Text
或 text
,我想在 QLineEdit
中实时将其更改为 TEXT
。因此,每当用户键入一个字母时,我想在 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
。但是想不通。
QCompleter
小部件有一个 setCaseSensitivity
属性 接受 QtCore.Qt.CaseSensitive
/ QtCore.Qt.CaseInsensitive
或简单的 1 或 0 (docs ).
The Qt documentaion 表示 "The default is 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_())
我有一个文本列表,如果用户在 QLineEdit
中搜索文本,我会打印该文本。 QLineEdit
.
QCompleter
问题是,我们知道 Text
和 text
不一样,但对用户来说是一样的。因此,如果用户开始输入 Text
或 text
,我想在 QLineEdit
中实时将其更改为 TEXT
。因此,每当用户键入一个字母时,我想在 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
。但是想不通。
QCompleter
小部件有一个 setCaseSensitivity
属性 接受 QtCore.Qt.CaseSensitive
/ QtCore.Qt.CaseInsensitive
或简单的 1 或 0 (docs ).
The Qt documentaion 表示 "The default is 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_())