KeyPressEvent不适用于Qscintilla Library(Qt)

时间:2016-11-20 15:46:48

标签: c++ qt qscintilla

编辑:该代码是正确的代码,更多的解释见评论;)

我正在尝试使用Qt和Qscintilla库对括号进行自动完成,但我不知道为什么,我的程序不会进入keyPressEvent函数。

#include "mainwindow.h"
#include <QMenuBar>
#include <QFileDialog>
#include <Qsci/qscilexerhtml.h>

#include <QKeyEvent>
#include <QTextEdit>

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()
{
    QString 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()
{
    QFontMetrics fontmetrics = QFontMetrics(editor->font());
    editor->setMarginsFont(editor->font());
    editor->setMarginWidth(0, fontmetrics.width(QString::number(editor->lines())) + 6);
    editor->setMarginLineNumbers(0, false);
    editor->setMarginsBackgroundColor(QColor("#cccccc"));

    connect(editor, SIGNAL(textChanged()), this, SLOT(onTextChanged()));
}

void MainWindow::onTextChanged()
{
    QFontMetrics fontmetrics = editor->fontMetrics();
    editor->setMarginWidth(0, fontmetrics.width(QString::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<QsciScintilla::FoldStyle>((!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<QKeyEvent *>(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);
}

还有我的头文件:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <Qsci/qsciscintilla.h>
#include <QTextEdit>
#include <QString>
#include <map>

class MainWindow : public QMainWindow
{
   Q_OBJECT

public:
   MainWindow(QWidget *parent = 0);
   ~MainWindow();
   void initializeUI();
   void initializeEditor();
   void initializeLexer();
   void initializeFolding();
   void initializeMargin();
   void initializeCaretLine();
   void initializeFont();

protected:
   bool eventFilter(QObject *obj, QEvent *e);

public slots:
   void onTextChanged();
   void onOpen();

private:
   QsciScintilla *editor;
};

#endif // MAINWINDOW_H

当我使用没有Qscintilla库的keyPressEvent函数时,它可以正常工作但不在这里。

如果有人可以帮助我,那将是一种乐趣:)

0 个答案:

没有答案