简体   繁体   English

Qscintilla2自动完成和自动识别

[英]Qscintilla2 AutoCompletion and AutoIdentation

I implemented this class in my project: 我在我的项目中实现了该类:

class ScriptEditorTextBox(QsciScintilla):

def __init__(self, parent):
    QsciScintilla.__init__(self)

    #Lexer
    lexer = QsciLexerPython()

    #AutoCompletion
    api = Qsci.QsciAPIs(lexer)
    api.add('aLongString')
    api.add('aLongerString')
    api.add('aDifferentString')
    api.add('sOmethingElse')
    api.prepare()

    self.setLexer(lexer)
    self.setAutoCompletionThreshold(1)
    self.setAutoCompletionSource(QsciScintilla.AcsAPIs)

    #LineHighlight
    self.setCaretLineVisible(True)
    self.setCaretLineBackgroundColor(QColor("gainsboro"))

    #AutoIndentation
    self.setAutoIndent(True)
    self.setIndentationGuides(True)
    self.setIndentationsUseTabs(True)
    self.setIndentationWidth(4)

    #Margins
    self.setMarginsBackgroundColor(QColor("gainsboro"))
    self.setMarginsFont(QFont("Consolas", 9, 87)) 
    self.setMarginLineNumbers(1, True)
    self.setMarginLineNumbers(2, False)
    self.setMarginWidth(1, QString().setNum(10))
    self.setMarginWidth(2, 10)
    self.connect(self, SIGNAL("linesChanged()"), self._linesChanged)

def _linesChanged(self):
    width = QString().setNum(self.lines() * 10)
    self.setMarginWidth(1, width)

Everything starts fine, but when I'm pressing enter after : it just does not auto indent. 一切都开始正常,但是当我按下after之后:不会自动缩进。 And, also no auto-completion (but I don't even know what should it autocomplete). 而且,也没有自动完成(但我什至不知道它应该自动完成)。

I'll be very grateful for any suggestions. 如有任何建议,我将不胜感激。

Both problems are caused by failing to keep a proper reference to the lexer. 这两个问题都是由于未能正确引用词法分析器而引起的。

The example code will work if you do this: 如果执行此操作,示例代码将起作用:

    lexer = QsciLexerPython(self)

or this: 或这个:

    self.lexer = QsciLexerPython()
    ...
    api = Qsci.QsciAPIs(self.lexer)
    ...
    self.setLexer(self.lexer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM