Insert images example

Make two images, and put them in your current python folder:

qscintilla_logo.png:   

exco_logo.png:        

Make sure the file ImageScintilla.py is in the same folder (as explained on the Prepare image hack page). After that, the following code (placed in a file of your choice, for example MyIDE.py) should work:

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.Qsci import *
from ImageScintilla import ImageScintilla


myCodeSample = r"""#include <stdio.h>

/*
 * I want an image
 * right here =>
 */

int main()
{
    char arr[5] = {'h', 'e', 'l', 'l', 'o'};

    int i;

    for(i = 0; i < 5; i++) {
        printf(arr[i]);
    }

    return 0;

}

/*
 * Now another test image here =>
 */

/*
 * And another test image here, a bit farther in =>
 */

/*
 * !!! A deleted image will not be shown here !!! =>
 */
""".replace("\n", "\r\n")


class CustomMainWindow(QMainWindow):
    def __init__(self):
        super(CustomMainWindow, self).__init__()

        # Window setup
        # --------------

        # 1. Define the geometry of the main window
        self.setGeometry(300, 300, 800, 400)
        self.setWindowTitle("QScintilla Test")

        # 2. Create frame and layout
        self.__frm = QFrame(self)
        self.__frm.setStyleSheet("QWidget { background-color: #ffeaeaea }")
        self.__lyt = QVBoxLayout()
        self.__frm.setLayout(self.__lyt)
        self.setCentralWidget(self.__frm)
        self.__myFont = QFont("Consolas", 14, weight=QFont.Bold)
        self.__myFont.setPointSize(14)

        # 3. Place a button
        self.__btn = QPushButton("Qsci")
        self.__btn.setFixedWidth(50)
        self.__btn.setFixedHeight(50)
        self.__btn.clicked.connect(self.__btn_action)
        self.__btn.setFont(self.__myFont)
        self.__lyt.addWidget(self.__btn)

        # QScintilla editor setup
        # ------------------------

        # 1. Make instance of ImageScintilla class
        # (instead of QsciScintilla)
        self.__editor = ImageScintilla()
        self.__editor.setText(myCodeSample)
        self.__editor.setLexer(None)
        self.__editor.setUtf8(True)  # Set encoding to UTF-8
        self.__editor.setFont(self.__myFont)  # Can be overridden by lexer

        # 2. Image tests
        self.__editor.set_calculation_font(self.__myFont)

        img = [None, None, None, None]
        img[0] = self.__editor.add_image("qscintilla_logo.png", (19, 4),  (30, 30))
        img[1] = self.__editor.add_image("qscintilla_logo.png", (35, 21), (90, 75))
        img[2] = self.__editor.add_image("exco_logo.png",       (53, 25), (85, 85))
        img[3] = self.__editor.add_image("qscintilla_logo.png", (55, 30), (60, 60))
        self.__editor.delete_image(img[3])

        # 3. Add editor to layout
        self.__lyt.addWidget(self.__editor)

        self.show()

    ''''''

    def __btn_action(self):
        print("Hello World!")

    ''''''


''' End Class '''

if __name__ == '__main__':
    app = QApplication(sys.argv)
    QApplication.setStyle(QStyleFactory.create('Fusion'))
    myGUI = CustomMainWindow()

    sys.exit(app.exec_())

''''''