码迷,mamicode.com
首页 > 其他好文 > 详细

pyqt5的QCompleter自动补全 使用模板

时间:2020-05-03 13:12:01      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:print   增加   string   实现   rom   count   int   tar   show   

使用QCompleter类,就可以实现自动补全功能,效果图如下:
技术图片

技术图片

对应的代码很简单

  1.  
    def init_lineedit(self):
  2.  
    # 增加自动补全
  3.  
    self.completer = QCompleter(items_list)
  4.  
    # 设置匹配模式 有三种: Qt.MatchStartsWith 开头匹配(默认) Qt.MatchContains 内容匹配 Qt.MatchEndsWith 结尾匹配
  5.  
    self.completer.setFilterMode(Qt.MatchContains)
  6.  
    # 设置补全模式 有三种: QCompleter.PopupCompletion(默认) QCompleter.InlineCompletion QCompleter.UnfilteredPopupCompletion
  7.  
    self.completer.setCompletionMode(QCompleter.PopupCompletion)
  8.  
    # 给lineedit设置补全器
  9.  
    self.lineedit.setCompleter(self.completer)
  10.  
     
  11.  
     
  12.  
    def init_combobox(self):
  13.  
    # 增加选项元素
  14.  
    for i in range(len(items_list)):
  15.  
    self.combobox.addItem(items_list[i])
  16.  
    self.combobox.setCurrentIndex(-1)
  17.  
     
  18.  
    # 增加自动补全
  19.  
    self.completer = QCompleter(items_list)
  20.  
    self.completer.setFilterMode(Qt.MatchContains)
  21.  
    self.completer.setCompletionMode(QCompleter.PopupCompletion)
  22.  
    self.combobox.setCompleter(self.completer)

【如下代码,完全复制,直接运行,即可使用】

    1.  
      import sys
    2.  
      from PyQt5.QtWidgets import *
    3.  
      from PyQt5.QtGui import *
    4.  
      from PyQt5.QtCore import *
    5.  
      ################################################
    6.  
       
    7.  
      items_list=["C","C++","Java","Python","JavaScript","C#","Swift","go","Ruby","Lua","PHP"]
    8.  
       
    9.  
      ################################################
    10.  
      class Widget(QWidget):
    11.  
      def __init__(self, *args, **kwargs):
    12.  
      super(Widget, self).__init__(*args, **kwargs)
    13.  
      layout = QHBoxLayout(self)
    14.  
      self.lineedit = QLineEdit(self, minimumWidth=200)
    15.  
      self.combobox = QComboBox(self, minimumWidth=200)
    16.  
      self.combobox.setEditable(True)
    17.  
       
    18.  
       
    19.  
      layout.addWidget(QLabel("QLineEdit", self))
    20.  
      layout.addWidget(self.lineedit)
    21.  
      layout.addItem(QSpacerItem(20, 20, QSizePolicy.Expanding, QSizePolicy.Minimum))
    22.  
       
    23.  
      layout.addWidget(QLabel("QComboBox", self))
    24.  
      layout.addWidget(self.combobox)
    25.  
       
    26.  
       
    27.  
      #初始化combobox
    28.  
      self.init_lineedit()
    29.  
      self.init_combobox()
    30.  
       
    31.  
       
    32.  
      #增加选中事件
    33.  
      self.combobox.activated.connect(self.on_combobox_Activate)
    34.  
       
    35.  
       
    36.  
       
    37.  
      def init_lineedit(self):
    38.  
      # 增加自动补全
    39.  
      self.completer = QCompleter(items_list)
    40.  
      # 设置匹配模式 有三种: Qt.MatchStartsWith 开头匹配(默认) Qt.MatchContains 内容匹配 Qt.MatchEndsWith 结尾匹配
    41.  
      self.completer.setFilterMode(Qt.MatchContains)
    42.  
      # 设置补全模式 有三种: QCompleter.PopupCompletion(默认) QCompleter.InlineCompletion QCompleter.UnfilteredPopupCompletion
    43.  
      self.completer.setCompletionMode(QCompleter.PopupCompletion)
    44.  
      # 给lineedit设置补全器
    45.  
      self.lineedit.setCompleter(self.completer)
    46.  
       
    47.  
       
    48.  
       
    49.  
      def init_combobox(self):
    50.  
      # 增加选项元素
    51.  
      for i in range(len(items_list)):
    52.  
      self.combobox.addItem(items_list[i])
    53.  
      self.combobox.setCurrentIndex(-1)
    54.  
       
    55.  
      # 增加自动补全
    56.  
      self.completer = QCompleter(items_list)
    57.  
      self.completer.setFilterMode(Qt.MatchContains)
    58.  
      self.completer.setCompletionMode(QCompleter.PopupCompletion)
    59.  
      self.combobox.setCompleter(self.completer)
    60.  
       
    61.  
       
    62.  
      def on_combobox_Activate(self, index):
    63.  
      print(self.combobox.count())
    64.  
      print(self.combobox.currentIndex())
    65.  
      print(self.combobox.currentText())
    66.  
      print(self.combobox.currentData())
    67.  
      print(self.combobox.itemData(self.combobox.currentIndex()))
    68.  
      print(self.combobox.itemText(self.combobox.currentIndex()))
    69.  
      print(self.combobox.itemText(index))
    70.  
       
    71.  
       
    72.  
       
    73.  
       
    74.  
       
    75.  
       
    76.  
      if __name__ == "__main__":
    77.  
      app = QApplication(sys.argv)
    78.  
      w = Widget()
    79.  
      w.show()
    80.  
      sys.exit(app.exec_())

pyqt5的QCompleter自动补全 使用模板

标签:print   增加   string   实现   rom   count   int   tar   show   

原文地址:https://www.cnblogs.com/nickshen/p/12821285.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!