我正在使用来自 Qt 的 CodeEditor 示例,来自这个 url。 在此处输入链接描述
他们将 LineNumberArea 和 CodeEditor 类放在同一个文件中。我试图将它们分成两个单独的头文件/源文件。但我不断收到“未定义的引用”错误。这是我的代码:
代码编辑器.h
#include <QPlainTextEdit>
#include <QObject>
#include "../headers/editor/linenumbers.h"
QT_BEGIN_NAMESPACE
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
class LineNumbers;
QT_END_NAMESPACE
class CodeEditor : public QPlainTextEdit {
Q_OBJECT
public:
explicit CodeEditor(QWidget *parent = 0);
virtual ~CodeEditor();
protected:
void resizeEvent(QResizeEvent *event);
private:
LineNumbers *lineNumberArea;
};
代码编辑器.cpp
#include <QtGui>
#include "../headers/editor/linenumbers.h"
#include "../headers/editor/codeeditor.h"
CodeEditor::CodeEditor(QWidget *parent) : QPlainTextEdit( parent ) {
lineNumberArea = new LineNumbers(this);
connect(this, SIGNAL(blockCountChanged(int)) , lineNumberArea, SLOT(updateLineNumberAreaWidth(int)));
connect(this, SIGNAL(updateRequest(QRect,int)), lineNumberArea, SLOT(updateLineNumberArea(QRect,int)));
connect(this, SIGNAL(cursorPositionChanged()) , lineNumberArea, SLOT(highlightCurrentLine()));
lineNumberArea->updateLineNumberAreaWidth(0);
lineNumberArea->highlightCurrentLine();
}
void CodeEditor::resizeEvent(QResizeEvent *e) {
QPlainTextEdit::resizeEvent(e);
QRect cr = contentsRect();
lineNumberArea->setGeometry(QRect(cr.left(), cr.top(), lineNumberArea->lineNumberAreaWidth(), cr.height()));
};
CodeEditor::~CodeEditor() {
}
行号.h
#include <QtGui/QWidget>
#include "../headers/editor/codeeditor.h"
QT_BEGIN_NAMESPACE
class QPaintEvent;
class QResizeEvent;
class QSize;
class QWidget;
QT_END_NAMESPACE
class LineNumbers : public QWidget {
public:
explicit LineNumbers(QPlainTextEdit *codeEditor);
virtual ~LineNumbers();
void lineNumberAreaPaintEvent(QPaintEvent *event);
int lineNumberAreaWidth();
QSize sizeHint() const;
protected:
void paintEvent(QPaintEvent *event);
public slots:
void updateLineNumberAreaWidth(int newBlockCount);
void highlightCurrentLine();
void updateLineNumberArea(const QRect &, int);
private:
QPlainTextEdit *codeEditor;
};
行号.cpp
#include <QtGui>
#include "../headers/editor/linenumbers.h"
LineNumbers::LineNumbers(QPlainTextEdit *editor) : QWidget( editor ) {
codeEditor = editor;
};
int LineNumbers::lineNumberAreaWidth() {
int digits = 2;
int max = qMax(1, codeEditor->blockCount());
while (max >= 10) {
max /= 10;
++digits;
}
int space = 3 + fontMetrics().width(QLatin1Char('9')) * digits;
return space;
};
void LineNumbers::updateLineNumberAreaWidth(int /* newBlockCount */) {
codeEditor->setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
}
void LineNumbers::updateLineNumberArea(const QRect &rect, int dy) {
if(dy) {
this->scroll(0, dy);
} else {
this->update(0, rect.y(), this->width(), rect.height());
}
if(rect.contains(codeEditor->viewport()->rect())) {
updateLineNumberAreaWidth(0);
}
};
void LineNumbers::highlightCurrentLine() {
QList<QTextEdit::ExtraSelection> extraSelections;
if(!codeEditor->isReadOnly()) {
QTextEdit::ExtraSelection selection;
QColor lineColor = QColor("#E1E1E1");
selection.format.setBackground(lineColor);
selection.format.setProperty(QTextFormat::FullWidthSelection, true);
selection.cursor = codeEditor->textCursor();
selection.cursor.clearSelection();
extraSelections.append(selection);
};
codeEditor->setExtraSelections(extraSelections);
};
void LineNumbers::lineNumberAreaPaintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.fillRect(event->rect(), QColor("#CACACA"));
QTextBlock block = codeEditor->firstVisibleBlock();
int blockNumber = block.blockNumber();
int top = (int) codeEditor->blockBoundingGeometry(block).translated(codeEditor->contentOffset()).top();
int bottom = top + (int) codeEditor->blockBoundingRect(block).height();
while (block.isValid() && top <= event->rect().bottom()) {
if (block.isVisible() && bottom >= event->rect().top()) {
QString number = QString::number(blockNumber + 1);
painter.setPen(Qt::black);
painter.drawText(0, top, this->width(), fontMetrics().height(),
Qt::AlignCenter, number);
}
block = block.next();
top = bottom;
bottom = top + (int) codeEditor->blockBoundingRect(block).height();
++blockNumber;
}
};
QSize sizeHint() const {
return QSize(lineNumberAreaWidth(), 0);
};
void paintEvent(QPaintEvent *event) {
lineNumberAreaPaintEvent(event);
};
LineNumbers::~LineNumbers() {
};
以及运行“make”时遇到的错误
codeeditor.o: In function `CodeEditor::CodeEditor(QWidget*)':
undefined reference to `LineNumbers::LineNumbers(QPlainTextEdit*)
undefined reference to `LineNumbers::updateLineNumberAreaWidth(int)
undefined reference to `LineNumbers::highlightCurrentLine()
codeeditor.o: In function `CodeEditor::resizeEvent(QResizeEvent*)
undefined reference to `LineNumbers::lineNumberAreaWidth()
可能有人请指出我做错了什么,不幸的是,我一直坚持这个问题试图解决这个问题并且无法找到问题。