Reputation: 591
I am using the CodeEditor example from Qt, from this url. enter link description here
They put LineNumberArea and CodeEditor classes in the same file. I am trying to split them up into two separate header/source files. But I keep getting an "undefined reference" error. Here is my code:
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;
};
codeeditor.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() {
}
linenumbers.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;
};
linenumbers.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() {
};
And the errors that I am get when I run "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()
May someone please point out what I am doing wrong, I have been stick with this problem trying to figure it out for a while and haven't been able to find the problem, unfortunately.
Upvotes: 0
Views: 268
Reputation: 3493
Linker cannot find linenumbers.cpp
. It was promised by header all this functions, but could not find linenumbers.cpp
Possibly forgot to add linenumbers.cpp
to .pro file?
and #include "../headers/editor/codeeditor.h"
is not needed in linenumbers.h
Upvotes: 1