EDIT: LE CODE EST LE BON CODE, POUR PLUS D’EXPLICATIONS, CONSULTEZ LES COMMENTAIRES;)
J’essaie de faire un auto-compléteur pour les parenthèses avec la bibliothèque Qt et Qscintilla mais je ne sais pas pourquoi, mon programme n’entre pas dans la fonction keyPressEvent.
#include "mainwindow.h" #include #include #include #include #include MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) { initializeUI(); initializeEditor(); editor->installEventFilter(this); } MainWindow::~MainWindow() { } void MainWindow::initializeUI() { setWindowTitle("My Code Editor"); setMenuBar(new QMenuBar(this)); QMenu *fileMenu = menuBar()->addMenu("&File"); fileMenu->addAction("&Open", this, SLOT(onOpen()), QKeySequence::Open); editor = new QsciScintilla(this); setCentralWidget(editor); } void MainWindow::onOpen() { QSsortingng filename = QFileDialog::getOpenFileName(this, tr("Open .html file"), QDir::currentPath(), "HTML (*.html *.css *.php)"); if (filename.isEmpty()) return; QFile file(filename); if (!file.open(QIODevice::ReadOnly)) return; editor->setText(file.readAll()); file.close(); } void MainWindow::initializeEditor() { initializeFont(); initializeMargin(); initializeCaretLine(); initializeLexer(); initializeFolding(); } void MainWindow::initializeFont() { QFont font("Consolas", 12); font.setFixedPitch(true); editor->setFont(font); } void MainWindow::initializeMargin() { QFontMesortingcs fontmesortingcs = QFontMesortingcs(editor->font()); editor->setMarginsFont(editor->font()); editor->setMarginWidth(0, fontmesortingcs.width(QSsortingng::number(editor->lines())) + 6); editor->setMarginLineNumbers(0, false); editor->setMarginsBackgroundColor(QColor("#cccccc")); connect(editor, SIGNAL(textChanged()), this, SLOT(onTextChanged())); } void MainWindow::onTextChanged() { QFontMesortingcs fontmesortingcs = editor->fontMesortingcs(); editor->setMarginWidth(0, fontmesortingcs.width(QSsortingng::number(editor->lines())) + 6); } void MainWindow::initializeLexer() { QsciLexerHTML *lexer = new QsciLexerHTML(); lexer->setDefaultFont(editor->font()); //lexer->setFoldComments(true); editor->setLexer(lexer); } void MainWindow::initializeCaretLine() { editor->setCaretLineVisible(true); editor->setCaretLineBackgroundColor(QColor("#ffe4e4")); } void MainWindow::initializeFolding() { QsciScintilla::FoldStyle state = static_cast((!editor->folding()) * 5); if (!state) editor->foldAll(false); editor->setFolding(state); } bool MainWindow::eventFilter(QObject *obj, QEvent *e) { if(e->type() == QEvent::KeyPress) { QKeyEvent *keyEvent = static_cast(e); switch (keyEvent->key()) { case 40: editor->insert("()"); break; case 60: editor->insert(""); break; default: return QObject::eventFilter(obj, e); } return true; } return QObject::eventFilter(obj, e); }
Et il y a mon fichier d’en-tête:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include #include #include #include #include
Lorsque j’utilise la fonction keyPressEvent sans la bibliothèque Qscintilla, cela fonctionne bien, mais pas ici.
Si quelqu’un peut m’aider, ce sera un plaisir 🙂