欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

PyQt5 文本輸入框自動(dòng)補(bǔ)全QLineEdit的實(shí)現(xiàn)示例

 更新時(shí)間:2020年05月13日 10:10:46   作者:c_G-17  
這篇文章主要介紹了PyQt5 文本輸入框自動(dòng)補(bǔ)全QLineEdit的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、QCompleter類

自動(dòng)補(bǔ)全會(huì)用到的一個(gè)類

主要代碼

  def init_lineedit(self):
    # 增加自動(dòng)補(bǔ)全
    self.completer = QCompleter(items_list)
    # 設(shè)置匹配模式 有三種: Qt.MatchStartsWith 開頭匹配(默認(rèn)) Qt.MatchContains 內(nèi)容匹配 Qt.MatchEndsWith 結(jié)尾匹配
    self.completer.setFilterMode(Qt.MatchContains)
    # 設(shè)置補(bǔ)全模式 有三種: QCompleter.PopupCompletion(默認(rèn)) QCompleter.InlineCompletion  QCompleter.UnfilteredPopupCompletion
    self.completer.setCompletionMode(QCompleter.PopupCompletion) 
    # 給lineedit設(shè)置補(bǔ)全器
    self.lineedit.setCompleter(self.completer)


  def init_combobox(self):
    # 增加選項(xiàng)元素
    for i in range(len(items_list)):
      self.combobox.addItem(items_list[i])
    self.combobox.setCurrentIndex(-1)

    # 增加自動(dòng)補(bǔ)全
    self.completer = QCompleter(items_list)
    self.completer.setFilterMode(Qt.MatchContains)
    self.completer.setCompletionMode(QCompleter.PopupCompletion)
    self.combobox.setCompleter(self.completer)

完整代碼:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
################################################

items_list=["C","C++","Java","Python","JavaScript","C#","Swift","go","Ruby","Lua","PHP"]

################################################
class Widget(QWidget):
  def __init__(self, *args, **kwargs):
    super(Widget, self).__init__(*args, **kwargs)
    layout = QHBoxLayout(self)
    self.lineedit = QLineEdit(self, minimumWidth=200)
    self.combobox = QComboBox(self, minimumWidth=200)
    self.combobox.setEditable(True)

    layout.addWidget(QLabel("QLineEdit", self))
    layout.addWidget(self.lineedit)
    layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))

    layout.addWidget(QLabel("QComboBox", self))
    layout.addWidget(self.combobox)

    #初始化combobox
    self.init_lineedit()
    self.init_combobox()

    #增加選中事件
    self.combobox.activated.connect(self.on_combobox_Activate)

  def init_lineedit(self):
    # 增加自動(dòng)補(bǔ)全
    self.completer = QCompleter(items_list)
    # 設(shè)置匹配模式 有三種: Qt.MatchStartsWith 開頭匹配(默認(rèn)) Qt.MatchContains 內(nèi)容匹配 Qt.MatchEndsWith 結(jié)尾匹配
    self.completer.setFilterMode(Qt.MatchContains)
    # 設(shè)置補(bǔ)全模式 有三種: QCompleter.PopupCompletion(默認(rèn)) QCompleter.InlineCompletion  QCompleter.UnfilteredPopupCompletion
    self.completer.setCompletionMode(QCompleter.PopupCompletion)
    # 給lineedit設(shè)置補(bǔ)全器
    self.lineedit.setCompleter(self.completer)

  def init_combobox(self):
    # 增加選項(xiàng)元素
    for i in range(len(items_list)):
      self.combobox.addItem(items_list[i])
    self.combobox.setCurrentIndex(-1)

    # 增加自動(dòng)補(bǔ)全
    self.completer = QCompleter(items_list)
    self.completer.setFilterMode(Qt.MatchContains)
    self.completer.setCompletionMode(QCompleter.PopupCompletion)
    self.combobox.setCompleter(self.completer)

  def on_combobox_Activate(self, index):
    print(self.combobox.count())
    print(self.combobox.currentIndex())
    print(self.combobox.currentText())
    print(self.combobox.currentData())
    print(self.combobox.itemData(self.combobox.currentIndex()))
    print(self.combobox.itemText(self.combobox.currentIndex()))
    print(self.combobox.itemText(index))

if __name__ == "__main__":
  app = QApplication(sys.argv)
  w = Widget()
  w.show()
  sys.exit(app.exec_())

二、QStandardItemModel類

最終效果

import sys

# from PyQt5.Qt import QCompleter
from PyQt5.Qt import QStandardItemModel
from PyQt5.QtCore import Qt
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtCore import pyqtSignal
from PyQt5.QtWidgets import QFrame
from PyQt5.QtWidgets import QLabel
from PyQt5.QtWidgets import QWidget
from PyQt5.QtWidgets import QLineEdit
from PyQt5.QtWidgets import QTextEdit
from PyQt5.QtWidgets import QCompleter
from PyQt5.QtWidgets import QSizePolicy
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QPushButton
from PyQt5.QtWidgets import QGridLayout
from PyQt5.QtWidgets import QApplication

from View import interface

class MainWindow(QMainWindow):

  def __init__(self):
    super(MainWindow,self).__init__(None)
    self.setWindowTitle("對(duì)金屬腐蝕性試驗(yàn)儀")
    self.initUI()

  def initUI(self):
    layout = QGridLayout()
    layout.setSpacing(10)
    self.loginLabel = QLabel("郵箱:")
    self.loginLabel.setAlignment(Qt.AlignRight)
    self.loginLabel.setStyleSheet("color:rgb(20,20,20,255);font-size:16px;font-weight:bold:text")

    self.loginTxt = QLineEdit()
    self.loginTxt.setText("admin")
    self.loginTxt.setPlaceholderText("User Name")
    self.loginTxt.setClearButtonEnabled(True)
    self.loginTxt.textChanged.connect(self.on_loginTxt_textChanged) #綁定槽函數(shù)
    self.m_model = QStandardItemModel(0, 1, self)
    m_completer = QCompleter(self.m_model, self)
    self.loginTxt.setCompleter(m_completer)
    m_completer.activated[str].connect(self.onTxtChoosed)


    self.pwdLabel = QLabel("密碼:")
    self.pwdLabel.setAlignment(Qt.AlignRight)
    self.pwdTxt = QLineEdit()
    self.pwdTxt.setContextMenuPolicy(Qt.NoContextMenu) #禁止復(fù)制粘貼
    self.pwdTxt.setPlaceholderText("Password")
    self.pwdTxt.setText("admin")
    self.pwdTxt.setEchoMode(QLineEdit.Password)
    self.pwdTxt.setClearButtonEnabled(True)
    self.registeredBtn = QPushButton("注冊")
    self.loginBtn = QPushButton("登陸")

    self.headLabel = QLabel("用戶登陸")
    self.headLabel.resize(300,30)
    self.headLabel.setAlignment(Qt.AlignCenter)
    self.headLabel.setStyleSheet("color:rgb(10,10,10,255);font-size:25px;font-weight:bold;font-family:Roman times;")

    self.headLabel.setSizePolicy(QSizePolicy.Expanding,QSizePolicy.Expanding)
    layout.addWidget(self.headLabel,0,0,1,2)
    policy = self.headLabel.sizePolicy()
    print(policy.verticalPolicy())
    policy.setVerticalPolicy(1)
    print(policy.verticalPolicy())
    # policy.setVerticalPolicy(1)
    layout.addWidget(self.loginLabel,1,0)
    layout.addWidget(self.loginTxt,1,1)
    layout.addWidget(self.pwdLabel,2,0)
    layout.addWidget(self.pwdTxt,2,1)
    layout.addWidget(self.registeredBtn,3,0)
    layout.addWidget(self.loginBtn,3,1)

    frame = QFrame(self)
    frame.setLayout(layout)
    self.setCentralWidget(frame)
    self.resize(300,150)

  def onTxtChoosed(self, txt):
    self.loginTxt.setText(txt)

  @pyqtSlot(str)
  def on_loginTxt_textChanged(self, text):
    if '@' in self.loginTxt.text():
      return

    emaillist = ["@163.com", "@qq.com", "@gmail.com", "@live.com", "@126.com", "@139.com"]
    self.m_model.removeRows(0, self.m_model.rowCount())
    for i in range(0, len(emaillist)):
      self.m_model.insertRow(0)
      self.m_model.setData(self.m_model.index(0, 0), text + emaillist[i])

if __name__ == '__main__':
  app = QApplication(sys.argv)
  mainWindow = MainWindow()
  mainWindow.show()
  mainWindow.activateWindow()
  mainWindow.raise_()
  app.exec_()
  del mainWindow
  del app

QStandardItemModel類為存儲(chǔ)自定義數(shù)據(jù)提供了一個(gè)通用模型。

QStandardItemModel可以用作標(biāo)準(zhǔn)Qt數(shù)據(jù)類型的存儲(chǔ)庫。它是模型/視圖類之一,是Qt的模型/視圖框架的一部分。

QStandardItemModel提供了一個(gè)經(jīng)典的基于項(xiàng)目的方法來處理模型。 QStandardItemModel中的項(xiàng)目由QStandardItem提供。

QStandardItemModel實(shí)現(xiàn)了QAbstractItemModel接口,這意味著該模型可用于在支持該接口的任何視圖(如QListView,QTableView和QTreeView以及您自己的自定義視圖)中提供數(shù)據(jù)。為了提高性能和靈活性,您可能希望子類QAbstractItemModel為不同類型的數(shù)據(jù)存儲(chǔ)庫提供支持。例如,QDirModel為底層文件系統(tǒng)提供了一個(gè)模型接口。

當(dāng)你想要一個(gè)列表或樹時(shí),你通常會(huì)創(chuàng)建一個(gè)空的QStandardItemModel并使用appendRow()向模型添加項(xiàng)目,使用item()來訪問項(xiàng)目。如果您的模型表示一個(gè)表格,您通常會(huì)將表格的維度傳遞給QStandardItemModel構(gòu)造函數(shù),并使用setItem()將項(xiàng)目放入表格中。您還可以使用setRowCount()和setColumnCount()來更改模型的尺寸。要插入項(xiàng)目,請使用insertRow()或insertColumn(),并刪除項(xiàng)目,請使用removeRow()或removeColumn()。

您可以使用setHorizontalHeaderLabels()和setVerticalHeaderLabels()來設(shè)置模型的標(biāo)題標(biāo)簽。

您可以使用findItems()在模型中搜索項(xiàng)目,并通過調(diào)用sort()對(duì)模型進(jìn)行排序。

調(diào)用clear()從模型中移除所有項(xiàng)目。

2.2 代碼理解

   self.loginTxt = QLineEdit()
    self.loginTxt.setText("admin")
    self.loginTxt.setPlaceholderText("User Name")
    self.loginTxt.setClearButtonEnabled(True)
0    self.loginTxt.textChanged.connect(self.on_loginTxt_textChanged) #綁定槽函數(shù)
1    self.m_model = QStandardItemModel(0, 1, self)
2    m_completer = QCompleter(self.m_model, self)
3    self.loginTxt.setCompleter(m_completer)
4    m_completer.activated[str].connect(self.onTxtChoosed)


  def onTxtChoosed(self, txt):
    self.loginTxt.setText(txt)

  @pyqtSlot(str)
  def on_loginTxt_textChanged(self, text):
    if '@' in self.loginTxt.text():
      return

    emaillist = ["@163.com", "@qq.com", "@gmail.com", "@live.com", "@126.com", "@139.com"]
    self.m_model.removeRows(0, self.m_model.rowCount())
    for i in range(0, len(emaillist)):
      self.m_model.insertRow(0)
      self.m_model.setData(self.m_model.index(0, 0), text + emaillist[i])

0-將文本改變信號(hào)連接到on_loginTxt_textChanged 函數(shù)處理

  • 構(gòu)建一個(gè)0行一列的新項(xiàng)目模型。self.m_model = QStandardItemModel(0, 1, self)
  • 用給定的父對(duì)象,構(gòu)造一個(gè)補(bǔ)全(完成)對(duì)象,該對(duì)象提供來自指定模型的完成對(duì)象,這里就是self.m_model. m_completer = QCompleter(self.m_model, self)
  • 將我們想要自動(dòng)補(bǔ)全、完成的文本輸入框?qū)ο笤O(shè)置關(guān)聯(lián)上面創(chuàng)建的 補(bǔ)全(完成對(duì)象)
  • QCompleter.activated;如果文本框的當(dāng)前項(xiàng)目發(fā)生更改,則會(huì)發(fā)出兩個(gè)信號(hào)currentIndexChanged()和activated()。無論以編程方式或通過用戶交互完成更改,currentIndexChanged()總是被發(fā)射,而只有當(dāng)更改是由用戶交互引起時(shí)才activated() 。highlighted()信號(hào)在用戶突出顯示組合框彈出列表中的項(xiàng)目時(shí)發(fā)出。所有三個(gè)信號(hào)都有兩個(gè)版本,一個(gè)帶有str參數(shù),另一個(gè)帶有int參數(shù)。如果用戶選擇或突出顯示一個(gè)圖像,則只會(huì)發(fā)出int信號(hào)。每當(dāng)可編輯組合框的文本發(fā)生改變時(shí),editTextChanged()信號(hào)就會(huì)發(fā)出。所以講activated信號(hào)連接到用戶選擇文本處理函數(shù)上

參考連接

到此這篇關(guān)于PyQt5 文本輸入框自動(dòng)補(bǔ)全QLineEdit的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)PyQt5 文本輸入框自動(dòng)補(bǔ)全內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 探究Python多進(jìn)程編程下線程之間變量的共享問題

    探究Python多進(jìn)程編程下線程之間變量的共享問題

    這篇文章主要介紹了探究Python多進(jìn)程編程下線程之間變量的共享問題,多進(jìn)程編程是Python學(xué)習(xí)進(jìn)階中的重要知識(shí),需要的朋友可以參考下
    2015-05-05
  • python base64 decode incorrect padding錯(cuò)誤解決方法

    python base64 decode incorrect padding錯(cuò)誤解決方法

    這篇文章主要介紹了python base64 decode incorrect padding錯(cuò)誤解決方法,本文使用把string補(bǔ)齊等號(hào)的方法解決了這個(gè)錯(cuò)誤,需要的朋友可以參考下
    2015-01-01
  • Python MD5加密實(shí)例詳解

    Python MD5加密實(shí)例詳解

    這篇文章主要介紹了Python MD5加密實(shí)例詳解的相關(guān)資料,這里提供實(shí)現(xiàn)方法及實(shí)例,需要的朋友可以參考下
    2017-08-08
  • win32com操作word之Application&Documents接口學(xué)習(xí)

    win32com操作word之Application&Documents接口學(xué)習(xí)

    這篇文章主要為大家介紹了win32com操作word之Application&Documents接口學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Django在Model保存前記錄日志實(shí)例

    Django在Model保存前記錄日志實(shí)例

    這篇文章主要介紹了Django在Model保存前記錄日志實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • 如何使用Python代碼創(chuàng)建表格

    如何使用Python代碼創(chuàng)建表格

    如果能夠?qū)⑽覀兊臒o序數(shù)據(jù)快速組織成更易讀的格式,對(duì)于數(shù)據(jù)分析非常有幫助,下面這篇文章主要介紹了關(guān)于如何使用Python代碼創(chuàng)建表格的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • python對(duì)驗(yàn)證碼降噪的實(shí)現(xiàn)示例代碼

    python對(duì)驗(yàn)證碼降噪的實(shí)現(xiàn)示例代碼

    這篇文章主要介紹了python對(duì)驗(yàn)證碼降噪的實(shí)現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • Ubuntu16安裝CUDA(9.1)和cuDNN的實(shí)現(xiàn)步驟(圖文)

    Ubuntu16安裝CUDA(9.1)和cuDNN的實(shí)現(xiàn)步驟(圖文)

    本文主要介紹了Ubuntu16安裝CUDA(9.1)和cuDNN,文中通過圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-07-07
  • Python字符串逆序的實(shí)現(xiàn)方法【一題多解】

    Python字符串逆序的實(shí)現(xiàn)方法【一題多解】

    今天小編就為大家分享一篇關(guān)于Python字符串逆序的實(shí)現(xiàn)方法【一題多解】,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • python實(shí)現(xiàn)俄羅斯方塊游戲

    python實(shí)現(xiàn)俄羅斯方塊游戲

    這篇文章主要為大家介紹了python實(shí)現(xiàn)俄羅斯方塊游戲的詳細(xì)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06

最新評(píng)論