Created
July 25, 2017 11:39
-
-
Save csaez/56aa2a8635f397a17c3b488ebd5c2a74 to your computer and use it in GitHub Desktop.
Multi QCompleter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
import sys | |
from PySide import QtGui | |
DELIMITERS = r'[,_\s]' | |
class MultiCompleter(QtGui.QCompleter): | |
def pathFromIndex(self, index): | |
path = super(MultiCompleter, self).pathFromIndex(index) | |
text = self.widget().text() | |
split = re.split(DELIMITERS, text)[-1] | |
if len(split) == len(text): | |
return path | |
if len(split): | |
string_without_split = text[:-len(split)] | |
else: | |
string_without_split = text | |
return string_without_split + path | |
def splitPath(self, path): | |
split = re.split(DELIMITERS, path)[-1] | |
return [split] | |
def updateFields(widget, text): | |
fields = re.split(DELIMITERS, text) | |
widget.clear() | |
widget.addItems(fields) | |
if __name__ == "__main__": | |
app = QtGui.QApplication(sys.argv) | |
d = QtGui.QDialog() | |
d.setWindowTitle("Naming Editor") | |
layout = QtGui.QVBoxLayout() | |
tokens = MultiCompleter(["description", "side", "type"]) | |
rule = QtGui.QLineEdit() | |
rule.setCompleter(tokens) | |
layout.addWidget(rule) | |
fields = QtGui.QListWidget() | |
fields.setEnabled(False) | |
rule.textChanged.connect(lambda x: updateFields(fields, x)) | |
layout.addWidget(fields) | |
d.setLayout(layout) | |
d.show() | |
sys.exit(app.exec_()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment