Issue
I want to overload QInputDialog
because I want to replace its QCombobox
with my own QCombobox
derivative. I checked the source-code of QInputDialog
and tried to overload its ensureCombobox()
. However, when I try something like:
class AutoCompleteInputDialog(QtGui.QInputDialog):
def __init__(self, *args, **kwargs):
self.ensureComboBox()
super(AutoCompleteInputDialog, self).__init__(*args, **kwargs)
def ensureComboBox(self):
print "ensureComboBox"
self.comboBox = AutoCompleteComboBox(self)
self.comboBox.hide()
self.comboBox.editTextChanged.connect(self.textChanged)
self.comboBox.currentIndexChanged.connect(self.textChanged)
AutoCompleteInputDialog.getItems(None, "test title", "test label", ["albatross 12", "tiger 12", "item 2", "item 3"])
ensureCombobox
is never called.
I tried also to define a static method that creates a QInputDialog
and sets its combobox. But it does not work either.
@staticmethod
def getItem(*args, **kwargs):
dialog = QtGui.QInputDialog()
dialog.comboBox = AutoCompleteComboBox(dialog)
return dialog.getItem(*args, **kwargs)
For completeness, the code of AutoCompleteCombobox
class AutoCompleteComboBox(QtGui.QComboBox):
def __init__(self, *args, **kwargs):
super(AutoCompleteComboBox, self).__init__(*args, **kwargs)
self.setEditable(True)
self.setInsertPolicy(self.NoInsert)
# self.comp = QtGui.QCompleter([""], self)
self.comp = CustomQCompleter([""], self)
self.comp.setCompletionMode(QtGui.QCompleter.PopupCompletion)
self.comp.setCaseSensitivity(QtCore.Qt.CaseInsensitive)
self.setCompleter(self.comp)#
self.setModel(["hallo babe", "world", "we", "are babe"])
def setModel(self, strList):
# self.comp.model().setStringList(strList)
self.clear()
self.insertItems(0, strList)
self.comp.setModel(self.model())
def focusInEvent(self, event):
self.clearEditText()
super(AutoCompleteComboBox, self).focusInEvent(event)
class CustomQCompleter(QtGui.QCompleter):
"""
copied from: http://stackoverflow.com/a/7767999/2156909
"""
def __init__(self, *args):#parent=None):
super(CustomQCompleter, self).__init__(*args)
self.local_completion_prefix = ""
self.source_model = None
def setModel(self, model):
self.source_model = model
super(CustomQCompleter, self).setModel(self.source_model)
def updateModel(self):
local_completion_prefix = self.local_completion_prefix
class InnerProxyModel(QtGui.QSortFilterProxyModel):
def filterAcceptsRow(self, sourceRow, sourceParent):
index0 = self.sourceModel().index(sourceRow, 0, sourceParent)
return local_completion_prefix.lower() in self.sourceModel().data(index0).lower()
proxy_model = InnerProxyModel()
proxy_model.setSourceModel(self.source_model)
super(CustomQCompleter, self).setModel(proxy_model)
def splitPath(self, path):
self.local_completion_prefix = path
self.updateModel()
return ""
Solution
OK, so the problem is that when you call QInputDialog, it sets up the sub-widgets in C++-land (or so it appears to me), so you cannot directly substitute in your favourite flavour. Therefore you need to go down the more typical route as shown:
class AutoCompleteInputDialog(QtGui.QDialog):
def __init__(self, *args, **kwargs):
super(AutoCompleteInputDialog, self).__init__(*args, **kwargs)
self.comboBox = AutoCompleteComboBox(self)
self.va = QtGui.QVBoxLayout(self)
self.va.addWidget(self.comboBox)
self.box = QtGui.QWidget(self)
self.ha = QtGui.QHBoxLayout(self)
self.va.addWidget(self.box)
self.box.setLayout(self.ha)
self.OK = QtGui.QPushButton("OK",self)
self.OK.setDefault(True)
self.cancel = QtGui.QPushButton("Cancel",self)
self.ha.addWidget(self.OK)
self.ha.addWidget(self.cancel)
self.OK.clicked.connect(self.accept)
self.cancel.clicked.connect(self.reject)
The resultant value is stored in acid.comboBox.currentText()
.
Answered By - mdurant
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.