c++ - 如何使用 QScintilla 和 C++ 使用自动完成功能?

标签 c++ qt autocomplete scintilla qscintilla

我想创建一个源代码编辑器,并且需要创建一个自动完成功能。我一直在寻找示例代码,但什么也没找到。 :( 提前致谢。

最佳答案

最近在以下网站上描述了 QScintilla API:

https://qscintilla.com/

它非常适合初学者。我相信它可以帮助你!

这是一个自动完成示例,取自网站:

# ---------------------------------------------- #
#          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_()

关于c++ - 如何使用 QScintilla 和 C++ 使用自动完成功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329818/

相关文章:

c++ - boost::make_shared 失败但 std::make_shared 工作

c++ - 我的队列数据结构实现错误

c++ - 为小部件存储画家有意义吗?

javascript - 滚动时自动完成保持为 "FIXED"(所有浏览器)

c++ - 哪些高级语言可以轻松与/C++ 交互?

c++ - 将 CDialog 分成两个不同背景的区域(里面的目标图片)

Qt 的独特模式

c++ - Qt:将文本添加到文件中

Bootstrap 模式中的 jQueryUI 自动完成显示在模式下方

javascript - 在 MacO 上禁用 chrome、safari 等 html 输入的自动标点符号