【问题标题】:How to use the autocomplete feature with QScintilla?如何在 QScintilla 中使用自动完成功能?
【发布时间】:2014-04-20 03:01:09
【问题描述】:

我找到了这个演示 autocompletion-using-pyqt4-and-qscintilla

但是,有时会导致段错误。

这个演示正确吗?

【问题讨论】:

  • 我尝试了演示 - 它对我来说很好,我看不出代码有什么问题。您是否以某种方式修改了代码?你要怎么做才能让它出现段错误?你在什么平台上,你使用的是什么版本的 QScintilla 和 PyQt?
  • thx,仍然。我在另一台机器上测试了这个问题,也很好。我安装了许多版本的python。我猜这个问题是由错误的pythonpath...orz ....引起的。
  • 如果我不得不猜测,我会说这很可能是一个 unicode 问题。非 ASCII 字符旁边的自动完成是否会导致您出现段错误?
  • 我不这么认为,我在每行的第一位输入单词。我重新安装我的操作系统(ubuntu kylin 14.04 beta1 64bit),然后用apt-get安装所有东西,仍然得到段错误!但是,在我的 Windows 32 位机器上,它运行良好,或者...

标签: python autocomplete pyqt qscintilla


【解决方案1】:

要回答题为“如何使用 QScintilla 中的自动完成功能”中的问题,我可以举一个例子:

# ---------------------------------------------- #
#          PYQT QSCINTILLA AUTOCOMPLETE          #
#                  EXAMPLE                       #
# ---------------------------------------------- #

"""
Initialization
"""
# Import the PyQt5 module with some of the GUI widgets
import PyQt5.QtWidgets
# Import the QScintilla module
import PyQt5.Qsci
# Import Python's sys module needed to get the
# application arguments
import sys

# Create the main PyQt application object
application = PyQt5.QtWidgets.QApplication(sys.argv)

# Create a QScintila editor instance
editor = PyQt5.Qsci.QsciScintilla()
# Set the initial text
editor.setText("Inicialization example.")
# Append some text
editor.append("\nAdded text.")

"""
Customization - GENERAL
"""
# Disable the lexer
editor.setLexer(None)
# Set the encoding to UTF-8
editor.setUtf8(True)
# Set the End-Of-Line character to Unix style ('\n')
editor.setEolMode(PyQt5.Qsci.QsciScintilla.EolUnix)
# Make End-Of-Line characters visible
editor.setEolVisibility(True)
# Set the zoom factor, the factor is in points.
editor.zoomTo(4)

"""
Customization - LINE WRAPPING
"""
# Set the text wrapping mode to word wrap
editor.setWrapMode(PyQt5.Qsci.QsciScintilla.WrapWord)
# Set the text wrapping mode visual indication
editor.setWrapVisualFlags(PyQt5.Qsci.QsciScintilla.WrapFlagByText)
# Set the text wrapping to indent the wrapped lines
editor.setWrapIndentMode(PyQt5.Qsci.QsciScintilla.WrapIndentSame)

"""
Customization - EDGE MARKER
"""
# Set the edge marker's position and set it to color the background
# when a line goes over the limit of 50 characters
editor.setEdgeMode(PyQt5.Qsci.QsciScintilla.EdgeBackground)
editor.setEdgeColumn(50)
edge_color = caret_fg_color = PyQt5.QtGui.QColor("#ff00ff00")
editor.setEdgeColor(edge_color)
# Add a long line that will display the edge marker coloring
editor.append("\nSome long line that will display the edge marker's functionality.")

"""
Customization - INDENTATION
"""
# Set indentation with spaces instead of tabs
editor.setIndentationsUseTabs(False)
# Set the tab width to 4 spaces
editor.setTabWidth(4)
# Set tab indent mode, see the 3.3.4 chapter in QSciDocs
# for a detailed explanation
editor.setTabIndents(True)
# Set autoindentation mode to maintain the indentation
# level of the previous line (the editor's lexer HAS
# to be disabled)
editor.setAutoIndent(True)
# Make the backspace jump back to the tab width guides 
# instead of deleting one character, but only when
# there are ONLY whitespaces on the left side of the
# cursor
editor.setBackspaceUnindents(True)
# Set indentation guides to be visible
editor.setIndentationGuides(True)

"""
Customization - CARET (the blinking cursor indicator)
"""
# Set the caret color to red
caret_fg_color = PyQt5.QtGui.QColor("#ffff0000")
editor.setCaretForegroundColor(caret_fg_color)
# Enable and set the caret line background color to slightly transparent blue
editor.setCaretLineVisible(True)
caret_bg_color = PyQt5.QtGui.QColor("#7f0000ff")
editor.setCaretLineBackgroundColor(caret_bg_color)
# Set the caret width of 4 pixels
editor.setCaretWidth(4)

"""
Customization - AUTOCOMPLETION (Partially usable without a lexer)
"""
# Set the autocompletions to case INsensitive
editor.setAutoCompletionCaseSensitivity(False)
# Set the autocompletion to not replace the word to the right of the cursor
editor.setAutoCompletionReplaceWord(False)
# Set the autocompletion source to be the words in the
# document
editor.setAutoCompletionSource(PyQt5.Qsci.QsciScintilla.AcsDocument)
# Set the autocompletion dialog to appear as soon as 1 character is typed
editor.setAutoCompletionThreshold(1)

# For the QScintilla editor to properly process events we
# need to add it to a QMainWindow object.
main_window = PyQt5.QtWidgets.QMainWindow()
# Set the central widget of the main window to 
# be the editor
main_window.setCentralWidget(editor)
# Resize the main window and show it on the screen
main_window.resize(800, 600)
main_window.show()

# Execute the application
application.exec_()

此示例取自网站 https://qscintilla.com/,您可以在其中找到更多关于使用 QScintilla 的通用文档(非常适合初学者)。

【讨论】:

    猜你喜欢
    • 2017-07-08
    • 2014-04-10
    • 1970-01-01
    • 2022-08-15
    • 2015-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多