4

I've found this demo autocompletion-using-pyqt4-and-qscintilla

however, it would cause segment fault sometimes.

Is this demo correct?

ekhumoro
  • 115,249
  • 20
  • 229
  • 336
dameng
  • 508
  • 7
  • 12
  • 1
    I tried the demo - it works fine for me, and I can see nothing wrong with the code. Have you modified the code in some way? What do you have to do to make it segfault? What plaform are you on, and what versions of QScintilla and PyQt are you using? – ekhumoro Mar 14 '14 at 19:09
  • thx,still.I test the problem on the other machine, works well too.I install many version of python. I guess the problem is caused by wrong pythonpath...orz.... – dameng Mar 15 '14 at 15:04
  • If I had to guess, I would say it's most likely to be a unicode issue. Does auto-complete next to non-ascii characters cause segfaults for you? – ekhumoro Mar 15 '14 at 19:08
  • I don't think so, I type words at the first place of each line.I reinstall my os(ubuntu kylin 14.04 beta1 64bit ),and and install all the things with apt-get, still get seg fault! However,on my windows 32bit machine, it works well,orz... – dameng Mar 16 '14 at 14:37

1 Answers1

5

To answer the question in your title "How to use the autocompletion feature in QScintilla", I can give you the following example:

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

This example is taken from the website https://qscintilla.com/ , where you can find much more general documentation on using QScintilla (very beginner-friendly).

K.Mulier
  • 8,069
  • 15
  • 79
  • 141