2016-07-02 70 views
0

我有一個文本列表,如果用戶在QLineEdit中搜索文本,我會打印文本。 QLineEdit中有一個QCompleter。如何自動生成QLineEdit-QCompleter上限

問題是,因爲我們知道Texttext是不一樣的,但它對用戶是一樣的。所以如果用戶開始輸入Texttext,我想在QLineEdit中實時更改爲TEXT。所以無論何時用戶輸入一個字母,我都希望在QCompleter-QLineEdit中使其成爲大寫。我怎樣才能做到這一點?我有這個atm;

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。但無法弄清楚如何。

回答

2

QCompleter插件具有setCaseSensitivity屬性,需要一個QtCore.Qt.CaseSensitive/QtCore.Qt.CaseInsensitive或簡稱爲1或0(docs)。

The Qt documentaion「默認是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_())