编辑:代码是正确的代码,更多解释请看评论;)
我正在尝试使用 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 函数时,它可以正常工作,但不是在这里。
如果有人可以帮助我,那将是一种乐趣:)