PySide2 QComboBox项目过滤

时间:2018-11-17 06:49:16

标签: python qcombobox 3dsmax pyside2

我有问题。

我在3dsMax 2016中使用QComboBox实现了过滤功能。

现在我想在3dsMax 2018中实现过滤功能。

因此,我更改了代码。 PySide到PySide2。

过滤功能有效。但输入不可见。 这是我的代码。

from PySide2.QtWidgets import QComboBox, QApplication, QCompleter, QLineEdit
from PySide2.QtCore import Qt, QSortFilterProxyModel
from PySide2.QtGui import QStandardItemModel,QStandardItem

from PySide2 import QtWidgets, QtCore, QtGui
import MaxPlus

class SuperDuperText(QtWidgets.QLineEdit):
    def focusInEvent(self, event):
        MaxPlus.CUI.DisableAccelerators()

    def focusOutEvent(self, event):
        MaxPlus.CUI.EnableAccelerators()

class ExtendedCombo( QtWidgets.QComboBox ):

    def __init__( self,  parent = None):
        super( ExtendedCombo, self ).__init__( parent )

        self.setFocusPolicy( Qt.StrongFocus )
        self.setEditable(True)
        self.completer = QCompleter( self )

        self.completer.setCompletionMode( QCompleter.UnfilteredPopupCompletion )
        self.pFilterModel = QSortFilterProxyModel( self )
        self.pFilterModel.setFilterCaseSensitivity( Qt.CaseInsensitive )

        self.completer.setPopup( self.view() )
        self.setCompleter( self.completer )
        # SuperDuperText().textEdited[unicode].connect( self.pFilterModel.setFilterFixedString )
        # self.lineEdit().textEdited[unicode].connect( self.pFilterModel.setFilterFixedString )
        # print self.lineEdit().echoMode() is return Normal 

        self.lineEdit().textEdited[unicode].connect(
        self.pFilterModel.setFilterFixedString)
        self.completer.activated.connect(self.setTextIfCompleterIsClicked)

    def setModel( self, model ):
        super(ExtendedCombo, self).setModel( model )
        self.pFilterModel.setSourceModel( model )
        self.completer.setModel(self.pFilterModel)

    def setModelColumn( self, column ):
        self.completer.setCompletionColumn( column )
        self.pFilterModel.setFilterKeyColumn( column )
        super(ExtendedCombo, self).setModelColumn( column )

    def view( self ):
        return self.completer.popup()

    def index( self ):
        return self.currentIndex()

    def setTextIfCompleterIsClicked(self, text):
        if text:
             index = self.findText(text)
             self.setCurrentIndex(index)

    def focusInEvent(self, event):
         MaxPlus.CUI.DisableAccelerators()

    def focusOutEvent(self, event):
         MaxPlus.CUI.EnableAccelerators()


class SuperDuperUI(QtWidgets.QDialog):
    def __init__(self, parent=MaxPlus.GetQMaxMainWindow()):
        super(SuperDuperUI, self).__init__(parent)
        self.setWindowTitle("Sample UI")
        self.initUI()

    def initUI(self):
        mainLayout = QtWidgets.QVBoxLayout()

        maxScriptsDir = MaxPlus.PathManager.GetScriptsDir()
        testLabel = QtWidgets.QLabel("Your scripts dir is: " + maxScriptsDir)
        mainLayout.addWidget(testLabel)

        testBtn = QtWidgets.QPushButton("This does nothing.")
        mainLayout.addWidget(testBtn)

        testEdit = SuperDuperText()
        testEdit.setPlaceholderText("You can type in here if you like...")
        mainLayout.addWidget(testEdit)

        model = QStandardItemModel()

        for i, word in enumerate(['hola', 'adios', 'hello', 'good bye']):
            item = QStandardItem(word)
            model.setItem(i, 0, item)

        combo = ExtendedCombo()
        combo.setModel(model)
        combo.setModelColumn(0)
        mainLayout.addWidget(combo)

        self.setLayout(mainLayout)

if __name__ == "__main__":
    ui = SuperDuperUI()
    ui.show()

我认为QCombobox的QLineedit存在焦点问题。也许......

我试图将qcombobox的qlineedit重新定义为SuperDuperText,但失败了。

感谢所有阅读我的问题的人。

0 个答案:

没有答案